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;
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
public function __construct($name, $title, $restrictedField, $restrictedTable, $value = "", $maxLength = null,
|
|
|
|
$validationURL = null, $restrictedRegex = null ){
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->maxLength = $maxLength;
|
|
|
|
|
|
|
|
$this->restrictedField = $restrictedField;
|
|
|
|
|
|
|
|
$this->restrictedTable = $restrictedTable;
|
|
|
|
|
|
|
|
$this->validateURL = $validationURL;
|
|
|
|
|
|
|
|
$this->restrictedRegex = $restrictedRegex;
|
|
|
|
|
|
|
|
parent::__construct($name, $title, $value);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$url = Convert::raw2att( $this->validateURL );
|
|
|
|
|
2008-04-06 06:08:45 +02:00
|
|
|
if($this->restrictedRegex)
|
2012-09-26 23:34:00 +02:00
|
|
|
$restrict = "<input type=\"hidden\" class=\"hidden\" name=\"{$this->name}Restricted\" id=\"" . $this->id()
|
|
|
|
. "RestrictedRegex\" value=\"{$this->restrictedRegex}\" />";
|
2007-07-19 12:40:28 +02:00
|
|
|
|
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(),
|
2012-03-08 16:32:03 +01:00
|
|
|
'tabindex' => $this->getAttribute('tabindex'),
|
2008-04-06 06:08:45 +02:00
|
|
|
'maxlength' => ($this->maxLength) ? $this->maxLength : null
|
|
|
|
);
|
|
|
|
|
2012-11-15 02:19:57 +01:00
|
|
|
return FormField::create_tag('input', $attributes);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function validate( $validator ) {
|
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 ) ) {
|
2012-09-26 23:34:00 +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;
|
|
|
|
}
|
|
|
|
}
|