作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com
我们来看一下field_validation_validator文件,这是一个抽象类,其它的字段验证器都继承自这个类。
abstract class field_validation_validator {
// Numbers we make calculations on.
protected $entity_type;
protected $entity;
protected $field;
protected $instance;
protected $langcode;
protected $items;
protected $delta;
protected $item;
protected $value;
protected $rule;
protected $errors;
/**
* Save arguments locally.
*/
function __construct($entity_type, $entity, $field, $instance, $langcode, $items, $delta, $item, $rule, $errors) {
$this->entity_type = $entity_type;
$this->entity = $entity;
$this->field = $field;
$this->instance = $instance;
$this->langcode = $langcode;
$this->items = $items;
$this->delta = $delta;
$this->item = $item;
$this->value = $item[$rule->col];
$this->rule = $rule;
$this->errors = $errors;
}
/**
* Validate field.
*/
public function validate() {}
/**
* Provide settings option
*/
function settings_form(&$form, &$form_state) {
$form['settings']['data'] = array(
'#title' => t('Config data'),
'#description' => t("Config data."),
'#type' => 'textfield',
//'#default_value' => $this->options['link_to_user'],
'#default_value' => '',
);
}
/**
* Return error message string for the validation rule.
*/
public function error_message() {
$error_message = $this->rule->error_message;
return $error_message;
}
/**
* Return error element for the validation rule.
*/
public function error_element() {
$error_element = $this->rule->field_name.']['.$this->langcode.']['.$this->delta.']['.$this->rule->col;
return $error_element;
}
}
它包含一个构造函数,四个成员函数;我们向构造函数里面传递了多个变量,$entity_type, $entity, $field, $instance, $langcode, $items, $delta, $item, $rule, $errors,这些都是与字段验证相关的。除了前面讲到了validate和settings_form两个成员函数以外,这里还包含了error_message,error_element两个成员函数,分别用来获取要设置的错误消息,获取错误消息所在的元素对象。由于field_validation_validator是一个对象,所以在模块的info文件里面,我们将它注册了一下:
files[] = field_validation_validator.inc
files[] = plugins/validator/field_validation_min_length_validator.inc
Views里面是都注册了的,这里我也学习它的做法,把包含类的文件都注册一下。这样可以实现类的缓加载。