Created
November 15, 2016 10:55
-
-
Save svandragt/e937ae033b78d35117f908abd6711273 to your computer and use it in GitHub Desktop.
AjaxUniqueTextField Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyDataObject extends DataObject | |
{ | |
public function getCMSFields() | |
{ | |
$fields = parent::getCMSFields(); | |
// Place a unique value constraint MyDataObjects table, MyCode column | |
$myAjax = new MyAjaxUniqueTextField('MyCode', 'My Code', 'MyCode', 'MyDataObject', "", null, null, null, $this->ID); | |
$fields->replaceField('MyCode', $myAjax); | |
return $fields; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyAjaxUniqueTextField extends AjaxUniqueTextField | |
{ | |
public function __construct($name, $title, $restrictedField, $restrictedTable, $value = "", $maxLength = null, $validationURL = null, $restrictedRegex = null, $recordID = null) | |
{ | |
// assign record | |
$this->recordID = $recordID; | |
parent::__construct($name, $title, $restrictedField, $restrictedTable, $value, $maxLength, | |
$validationURL, $restrictedRegex); | |
} | |
public function validate($validator) | |
{ | |
// validate against db records | |
$result = DB::query(sprintf( | |
"SELECT COUNT(*) FROM \"%s\" WHERE \"%s\" = '%s' AND \"ID\" != ' | |
%s' ", | |
$this->restrictedTable, | |
$this->restrictedField, | |
Convert::raw2sql($this->value), | |
$this->recordID | |
))->value(); | |
if ($result && ($result > 0)) { | |
$validator->validationError($this->name, | |
_t('Form.VALIDATIONNOTUNIQUE', "The value entered is not unique")); | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment