Hi everybody. I'm trying to test this module for validating my form fields. In the case of adding a php code rule isn´t working to me.
I'm tryng this in my project code and a fresh installation.
Supposse this example for a fresh installation.

I add a rule for a text field (suppose custom_field) in article content type with this options:
Entity type: node
Bundle name: Article
Field name: custom field
Column: value
Validator: php code

Php Code:

if ($this->value == 'abc')
   $this->set_error()

Custom error message: validation error in custom field.

Save and test but isn't work. I don't see any validation message or error in reports.
Any rule that I added with php code validator wasn´t working. Any idea what I'm doing wrong?
Thanks, regards.

Comments

g089h515r806’s picture

change your code to:

if ($this->value == 'abc'){
   $this->set_error();
} 
drupso’s picture

Changing to this code not works. Any idea?
Thanks

drupso’s picture

Issue summary: View changes

r1

Dev8.addweb’s picture

Issue summary: View changes

Hi,

I cracked it, here's the confirm solution.

Issue you are facing may happen due to your disabled field validation UI module and/OR validation rules. If not then please refer below example:

if($this->value == 'abc'){
$token = array('[mytest]' => 'abc');
$this->set_error($token);
}

We could pass some tokens to error message.

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);"

You can also use available variables:
$this->entity_type ;
$this->entity ;
$this->field;
$this->instance;
$this->langcode;
$this->items ;
$this->delta;
$this->item ;
$this->value ;
$this->rule ;

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 best way to use you could visit it through "$this->entity->field_another_field_against" rather then you use 'abc' hardcode value.

But need to check first that your $this->value contain as you defined into IF conditional statement (i.e. 'abc').

Hope this helps!

Thanks

Dev8.addweb’s picture

Assigned: Unassigned » Dev8.addweb
Status: Active » Closed (fixed)
Issue tags: +PHP
atanu2k10’s picture

I used this code to validate the dete of death is not before the date of birth..
if ($this->value < $this->entity->field_dobirth["und"][0]["value"]) {
$this->set_error();
{

But it doesn't work at all. variables r correct.