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 );
if( $this->restrictedRegex )
$restrict = "name}Restricted\" id=\"" . $this->id() . "RestrictedRegex\" value=\"{$this->restrictedRegex}\" />";
if($this->maxLength) {
return "class."\" text\" type=\"text\" id=\"" . $this->id() . "\" name=\"{$this->name}\" value=\"" . $this->attrValue() . "\" maxlength=\"$this->maxLength\" />$restrict";
} else {
return "class."\" text\" type=\"text\" id=\"" . $this->id() . "\" name=\"{$this->name}\" value=\"" . $this->attrValue() . "\" />$restrict";
}
}
function jsValidation() {
$formID = $this->form->FormName();
$id = $this->id();
$url = Director::absoluteBaseURL() . $this->validateURL;
if($this->restrictedRegex) {
$jsCheckFunc = <<restrictedRegex}/);
if(match) {
Element.addClassName(this, 'invalid');
return false;
}
return true;
JS;
} else {
$jsCheckFunc = "return true;";
}
$jsFunc = <<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');";
}
function validate( $validator ) {
$result = DB::query(sprintf(
"SELECT COUNT(*) FROM `%s` WHERE `%s` = '%s'",
$this->restrictedTable,
$this->restrictedField,
Convert::raw2sql($this->value)
))->value();
if( $result && ( $result > 0 ) ) {
$validator->validationError( $this->name, _t('Form.VALIDATIONNOTUNIQUE', "The value entered is not unique") );
return false;
}
return true;
}
}
?>