2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
Requirements::javascript("sapphire/javascript/UniqueFields.js");
|
|
|
|
|
|
|
|
$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',
|
|
|
|
'class' => $this->extraClass() . " text",
|
|
|
|
'id' => $this->id(),
|
2008-04-09 13:21:22 +02:00
|
|
|
'name' => $this->Name(),
|
|
|
|
'value' => $this->Value(),
|
2008-04-06 06:08:45 +02:00
|
|
|
'tabindex' => $this->getTabIndexHTML(),
|
|
|
|
'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() {
|
|
|
|
if(this.checkValid()) {
|
|
|
|
new Ajax.Request('{$url}?ajax=1&{$this->name}=' + encodeURIComponent(this.value), {
|
|
|
|
method: 'get',
|
|
|
|
onSuccess: function(response) {
|
|
|
|
console.debug(this);
|
|
|
|
if(response.responseText == 'ok')
|
|
|
|
Element.removeClassName(this, 'inuse');
|
|
|
|
else {
|
|
|
|
Element.addClassName(this, 'inuse');
|
|
|
|
}
|
|
|
|
}.bind(this),
|
|
|
|
onFailure: function(response) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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(
|
|
|
|
"SELECT COUNT(*) FROM `%s` WHERE `%s` = '%s'",
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|