PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/forms/AjaxUniqueTextField.php

http://github.com/silverstripe/sapphire
PHP | 127 lines | 93 code | 26 blank | 8 comment | 10 complexity | ca2a383db9e33b62132417c73689b4f9 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, CC-BY-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Text field that automatically checks that the value entered is unique for the given
  4. * set of fields in a given set of tables
  5. * @package forms
  6. * @subpackage fields-formattedinput
  7. */
  8. class AjaxUniqueTextField extends TextField {
  9. protected $restrictedField;
  10. protected $restrictedTable;
  11. // protected $restrictedMessage;
  12. protected $validateURL;
  13. protected $restrictedRegex;
  14. function __construct($name, $title, $restrictedField, $restrictedTable, $value = "", $maxLength = null, $validationURL = null, $restrictedRegex = null ){
  15. $this->maxLength = $maxLength;
  16. $this->restrictedField = $restrictedField;
  17. $this->restrictedTable = $restrictedTable;
  18. $this->validateURL = $validationURL;
  19. $this->restrictedRegex = $restrictedRegex;
  20. parent::__construct($name, $title, $value);
  21. }
  22. function Field() {
  23. Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
  24. Requirements::javascript(SAPPHIRE_DIR . "/javascript/UniqueFields.js");
  25. $this->jsValidation();
  26. $url = Convert::raw2att( $this->validateURL );
  27. if($this->restrictedRegex)
  28. $restrict = "<input type=\"hidden\" class=\"hidden\" name=\"{$this->name}Restricted\" id=\"" . $this->id() . "RestrictedRegex\" value=\"{$this->restrictedRegex}\" />";
  29. $attributes = array(
  30. 'type' => 'text',
  31. 'class' => 'text' . ($this->extraClass() ? $this->extraClass() : ''),
  32. 'id' => $this->id(),
  33. 'name' => $this->getName(),
  34. 'value' => $this->Value(),
  35. 'tabindex' => $this->getTabIndex(),
  36. 'maxlength' => ($this->maxLength) ? $this->maxLength : null
  37. );
  38. return $this->createTag('input', $attributes);
  39. }
  40. function jsValidation() {
  41. $formID = $this->form->FormName();
  42. $id = $this->id();
  43. $url = Director::absoluteBaseURL() . $this->validateURL;
  44. if($this->restrictedRegex) {
  45. $jsCheckFunc = <<<JS
  46. Element.removeClassName(this, 'invalid');
  47. var match = this.value.match(/{$this->restrictedRegex}/);
  48. if(match) {
  49. Element.addClassName(this, 'invalid');
  50. return false;
  51. }
  52. return true;
  53. JS;
  54. } else {
  55. $jsCheckFunc = "return true;";
  56. }
  57. $jsFunc = <<<JS
  58. Behaviour.register({
  59. '#$id' : {
  60. onkeyup: function() {
  61. var self = this;
  62. if(this.checkValid()) {
  63. jQuery.ajax({
  64. 'url': '{$url}?ajax=1&{$this->name}=' + encodeURIComponent(this.value),
  65. method: 'get',
  66. success: function(response) {
  67. if(response.responseText == 'ok')
  68. Element.removeClassName(self, 'inuse');
  69. else {
  70. Element.addClassName(self, 'inuse');
  71. }
  72. }
  73. error: function(response) {
  74. }
  75. });
  76. }
  77. },
  78. checkValid: function() {
  79. $jsCheckFunc
  80. }
  81. }
  82. });
  83. JS;
  84. Requirements::customScript($jsFunc, 'func_validateAjaxUniqueTextField');
  85. //return "\$('$formID').validateCurrency('$this->name');";
  86. }
  87. function validate( $validator ) {
  88. $result = DB::query(sprintf(
  89. "SELECT COUNT(*) FROM \"%s\" WHERE \"%s\" = '%s'",
  90. $this->restrictedTable,
  91. $this->restrictedField,
  92. Convert::raw2sql($this->value)
  93. ))->value();
  94. if( $result && ( $result > 0 ) ) {
  95. $validator->validationError( $this->name, _t('Form.VALIDATIONNOTUNIQUE', "The value entered is not unique") );
  96. return false;
  97. }
  98. return true;
  99. }
  100. }
  101. ?>