Skip to content

Instantly share code, notes, and snippets.

@lerni
Last active February 22, 2018 12:14
Show Gist options
  • Save lerni/47a22120d72a486024479c53bc0ee02d to your computer and use it in GitHub Desktop.
Save lerni/47a22120d72a486024479c53bc0ee02d to your computer and use it in GitHub Desktop.
UniqueTextField SilverStripe
<?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