Last active
February 22, 2018 12:14
-
-
Save lerni/47a22120d72a486024479c53bc0ee02d to your computer and use it in GitHub Desktop.
UniqueTextField SilverStripe
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 UniqueTextField extends TextField { | |
public function Type() { | |
return 'unique text'; | |
} | |
protected $restrictedTable; | |
protected $restrictedField; | |
public function __construct($name, $title, $restrictedTable, $restrictedField, $value = "") { | |
$this->restrictedTable = $restrictedTable; | |
$this->restrictedField = $restrictedField; | |
parent::__construct($name, $title, $value); | |
} | |
function validate($validator) { | |
$this->value = trim($this->value); | |
$ClassName = $this->restrictedTable; | |
$exists = $ClassName::get()->filter($this->restrictedField, $this->value)->count(); | |
if($exists > 1) { | |
$validator->validationError( | |
$this->name, | |
_t('UniqueTextField.UNIQUEVALIDATION', "Es existiert bereits ein Element mit dem selben Text"), | |
"validation" | |
); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment