1 <?php
2 3 4 5 6 7
8 class AjaxUniqueTextField extends TextField {
9
10 protected $restrictedField;
11 protected $restrictedTable;
12
13 protected $validateURL;
14
15 protected $restrictedRegex;
16
17 function __construct($name, $title, $restrictedField, $restrictedTable, $value = "", $maxLength = null, $validationURL = null, $restrictedRegex = null ){
18 $this->maxLength = $maxLength;
19
20 $this->restrictedField = $restrictedField;
21
22 $this->restrictedTable = $restrictedTable;
23
24 $this->validateURL = $validationURL;
25
26 $this->restrictedRegex = $restrictedRegex;
27
28 parent::__construct($name, $title, $value);
29 }
30
31 function Field() {
32 Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
33 Requirements::javascript(SAPPHIRE_DIR . "/javascript/UniqueFields.js");
34
35 $this->jsValidation();
36
37 $url = Convert::raw2att( $this->validateURL );
38
39 if($this->restrictedRegex)
40 $restrict = "<input type=\"hidden\" class=\"hidden\" name=\"{$this->name}Restricted\" id=\"" . $this->id() . "RestrictedRegex\" value=\"{$this->restrictedRegex}\" />";
41
42 $attributes = array(
43 'type' => 'text',
44 'class' => 'text' . ($this->extraClass() ? $this->extraClass() : ''),
45 'id' => $this->id(),
46 'name' => $this->Name(),
47 'value' => $this->Value(),
48 'tabindex' => $this->getTabIndex(),
49 'maxlength' => ($this->maxLength) ? $this->maxLength : null
50 );
51
52 return $this->createTag('input', $attributes);
53 }
54
55 function jsValidation() {
56 $formID = $this->form->FormName();
57 $id = $this->id();
58 $url = Director::absoluteBaseURL() . $this->validateURL;
59
60 if($this->restrictedRegex) {
61 $jsCheckFunc = <<<JS
62 Element.removeClassName(this, 'invalid');
63 var match = this.value.match(/{$this->restrictedRegex}/);
64 if(match) {
65 Element.addClassName(this, 'invalid');
66 return false;
67 }
68
69 return true;
70 JS;
71 } else {
72 $jsCheckFunc = "return true;";
73 }
74
75 $jsFunc = <<<JS
76 Behaviour.register({
77 '#$id' : {
78 onkeyup: function() {
79 if(this.checkValid()) {
80 new Ajax.Request('{$url}?ajax=1&{$this->name}=' + encodeURIComponent(this.value), {
81 method: 'get',
82 onSuccess: function(response) {
83 if(response.responseText == 'ok')
84 Element.removeClassName(this, 'inuse');
85 else {
86 Element.addClassName(this, 'inuse');
87 }
88 }.bind(this),
89 onFailure: function(response) {
90
91 }
92 });
93 }
94 },
95
96 checkValid: function() {
97 $jsCheckFunc
98 }
99 }
100 });
101 JS;
102 Requirements::customScript($jsFunc, 'func_validateAjaxUniqueTextField');
103
104
105
106 }
107
108 function validate( $validator ) {
109
110 $result = DB::query(sprintf(
111 "SELECT COUNT(*) FROM \"%s\" WHERE \"%s\" = '%s'",
112 $this->restrictedTable,
113 $this->restrictedField,
114 Convert::raw2sql($this->value)
115 ))->value();
116
117 if( $result && ( $result > 0 ) ) {
118 $validator->validationError( $this->name, _t('Form.VALIDATIONNOTUNIQUE', "The value entered is not unique") );
119 return false;
120 }
121
122 return true;
123 }
124 }
125 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-