Last updated April 17, 2012. Created by g089h515r806 on April 17, 2012.
Edited by shamio. Log in to edit this page.
In field validation 7.x-2.x, PHP code validator is included. It is superpowerfull, but also very dangerous.
Field validation is mainly used by site build, developer.After you complete the site, you could disable field validation UI module.And all validation rules still work correctly.
Here is 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 current value using
"$this->value, "
could set error message using
"$this->set_error();"
Could set token through
"$this->set_error($token);"
Variable we could visit 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 case, if we need set an error, just call "$this->set_error();", it will work correctly. If it does not work as your expectation, 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.
Comments
PHP code verification against another field
Hi, I would like to know how can I retrieve the value of another field and check it against the value of this field. I have two date fields and I want to make sure the date on the current field is later than the date on the other date field on the same form. Thank you!
Marcos Buarque de Hollanda
Spel - www.spelweb.com.br
yes, you could dot it in this
yes, you could do it in this way:
if($this->value < {$this->entity->field_another_field_against}['und'][0]['value']){$this->set_error();
}
You could visit it through "$this->entity->field_another_field_against".
Chinese drupal tutorials Think in Drupal
Many thanks!
It works!!
Marcos Buarque de Hollanda
Spel - www.spelweb.com.br
Thank for this. I was looking
Thank you for this. I was looking for the exact same answer.
Limit combined length of fields
For some reason I can't get this code work for me. I need to check the combined length of two long text fields (e.g. introduction and methods) and give an error message if it is above a certain number. I thought that I could do it with this module, but I can't find any way to actually access the value of the other field.
Any help would be greatly appreciated!
This code works for me but
This code works for me but without {}:
if($this->value < $this->entity->field_another_field_against['und'][0]['value']){$this->set_error();
}
www.clevermade.pl
www.gotowaniebezstresu.pl
If $this is part of a
If $this is part of a collection, how do I check the value of a field that is outside the collection?
How do I check $this->entity->parents->another_field ?