2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
|
|
|
* Text field that automatically checks that the value entered is unique for the given
|
|
|
|
* set of fields in a given set of tables
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class AjaxUniqueTextField extends TextField {
|
|
|
|
|
|
|
|
protected $restrictedField;
|
|
|
|
protected $restrictedTable;
|
|
|
|
// protected $restrictedMessage;
|
|
|
|
protected $validateURL;
|
|
|
|
|
|
|
|
protected $restrictedRegex;
|
|
|
|
|
|
|
|
function __construct($name, $title, $restrictedField, $restrictedTable, $value = "", $maxLength = null, $validationURL = null, $restrictedRegex = null ){
|
|
|
|
$this->maxLength = $maxLength;
|
|
|
|
|
|
|
|
$this->restrictedField = $restrictedField;
|
|
|
|
|
|
|
|
$this->restrictedTable = $restrictedTable;
|
|
|
|
|
|
|
|
$this->validateURL = $validationURL;
|
|
|
|
|
|
|
|
$this->restrictedRegex = $restrictedRegex;
|
|
|
|
|
|
|
|
parent::__construct($name, $title, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Field() {
|
2009-03-10 23:08:52 +01:00
|
|
|
Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/UniqueFields.js");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$this->jsValidation();
|
|
|
|
|
|
|
|
$url = Convert::raw2att( $this->validateURL );
|
|
|
|
|
2008-04-06 06:08:45 +02:00
|
|
|
if($this->restrictedRegex)
|
2007-07-19 12:40:28 +02:00
|
|
|
$restrict = "<input type=\"hidden\" class=\"hidden\" name=\"{$this->name}Restricted\" id=\"" . $this->id() . "RestrictedRegex\" value=\"{$this->restrictedRegex}\" />";
|
|
|
|
|
2008-04-06 06:08:45 +02:00
|
|
|
$attributes = array(
|
|
|
|
'type' => 'text',
|
2008-10-21 22:58:11 +02:00
|
|
|
'class' => 'text' . ($this->extraClass() ? $this->extraClass() : ''),
|
2008-04-06 06:08:45 +02:00
|
|
|
'id' => $this->id(),
|
2011-10-29 13:07:40 +02:00
|
|
|
'name' => $this->getName(),
|
2008-04-09 13:21:22 +02:00
|
|
|
'value' => $this->Value(),
|
2008-09-17 01:14:31 +02:00
|
|
|
'tabindex' => $this->getTabIndex(),
|
2008-04-06 06:08:45 +02:00
|
|
|
'maxlength' => ($this->maxLength) ? $this->maxLength : null
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->createTag('input', $attributes);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function jsValidation() {
|
|
|
|
$formID = $this->form->FormName();
|
|
|
|
$id = $this->id();
|
|
|
|
$url = Director::absoluteBaseURL() . $this->validateURL;
|
|
|
|
|
|
|
|
if($this->restrictedRegex) {
|
|
|
|
$jsCheckFunc = <<<JS
|
|
|
|
Element.removeClassName(this, 'invalid');
|
|
|
|
var match = this.value.match(/{$this->restrictedRegex}/);
|
|
|
|
if(match) {
|
|
|
|
Element.addClassName(this, 'invalid');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
JS;
|
|
|
|
} else {
|
|
|
|
$jsCheckFunc = "return true;";
|
|
|
|
}
|
|
|
|
|
|
|
|
$jsFunc = <<<JS
|
|
|
|
Behaviour.register({
|
|
|
|
'#$id' : {
|
|
|
|
onkeyup: function() {
|
2011-03-02 05:14:47 +01:00
|
|
|
var self = this;
|
2007-07-19 12:40:28 +02:00
|
|
|
if(this.checkValid()) {
|
2011-03-02 05:14:47 +01:00
|
|
|
jQuery.ajax({
|
|
|
|
'url': '{$url}?ajax=1&{$this->name}=' + encodeURIComponent(this.value),
|
2007-07-19 12:40:28 +02:00
|
|
|
method: 'get',
|
2011-03-02 05:14:47 +01:00
|
|
|
success: function(response) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(response.responseText == 'ok')
|
2011-03-02 05:14:47 +01:00
|
|
|
Element.removeClassName(self, 'inuse');
|
2007-07-19 12:40:28 +02:00
|
|
|
else {
|
2011-03-02 05:14:47 +01:00
|
|
|
Element.addClassName(self, 'inuse');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-02 05:14:47 +01:00
|
|
|
}
|
|
|
|
error: function(response) {
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
checkValid: function() {
|
|
|
|
$jsCheckFunc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
|
|
|
Requirements::customScript($jsFunc, 'func_validateAjaxUniqueTextField');
|
|
|
|
|
|
|
|
//return "\$('$formID').validateCurrency('$this->name');";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-03-06 02:21:47 +01:00
|
|
|
function validate( $validator ) {
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-02-19 23:46:30 +01:00
|
|
|
$result = DB::query(sprintf(
|
2008-11-23 01:31:06 +01:00
|
|
|
"SELECT COUNT(*) FROM \"%s\" WHERE \"%s\" = '%s'",
|
2008-02-19 23:46:30 +01:00
|
|
|
$this->restrictedTable,
|
|
|
|
$this->restrictedField,
|
|
|
|
Convert::raw2sql($this->value)
|
|
|
|
))->value();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if( $result && ( $result > 0 ) ) {
|
2007-10-25 04:47:45 +02:00
|
|
|
$validator->validationError( $this->name, _t('Form.VALIDATIONNOTUNIQUE', "The value entered is not unique") );
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|