PHP code validator
In Field validation 7.x-2.x, a PHP code validator is included. It is superpowerful, but also very dangerous.
Field validation is mainly used by site builders and developers. After you complete the site, you could disable field validation UI module and all validation rules still work correctly.
Here are some examples of using PHP code validator.
if($this->value < 100){
$this->set_error();
}
Also, we could pass some tokens to error message
if($this->value < 100){
$token = array('[test]' => '3.14',);
$this->set_error($token);
}
It is very simple.
we could get the current value using
"$this->value, "
could set error the message using
"$this->set_error();"
Could set the error message through a token
"$this->set_error($token);"
Variables available in PHP code:
$this->entity_type ;
$this->entity ;
$this->field;
$this->instance;
$this->langcode;
$this->items ;
$this->delta;
$this->item ;
$this->value ;
$this->rule ;
For most use cases, if we need to set an error, just call "$this->set_error();". If it does not work as you expected, you could call "form_set_error()" directly.
The code for validate() of PHP code is very simple,
public function validate() {
return eval($this->rule->settings['data']);
}
Developer should set_error by themselves.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion