i am not a programmer so i would like to seek help for using the php code validator.

I have 2 fields:

field_price_range (a term reference field)

field_price (an integer field)

for example, there are 2 price range (i.e. $0-99 and $100-500 )

if the user choose $0-99, how can I validate the field_price value to be within 0-99?

Thank you very much for your kind help!!!

Comments

g089h515r806’s picture

try with following code:

//123 means term $0-99
if($this->entity->field_price_range['und'][0][tid] == 123){
  if($this->value > 99 || $this->value < 0){
    $this->set_error();
  }
}

//124 means term $100-500 
if($this->entity->field_price_range['und'][0][tid] == 124){
  if($this->value > 500 || $this->value < 100){
    $this->set_error();
  }
}
adamtong’s picture

Thank you very much. it works but there is a error message:

Notice: Use of undefined constant tid - assumed 'tid' in eval() (line 2 of /mydrupal/sites/all/modules/field_validation/plugins/validator/field_validation_php_validator.inc(21) : eval()'d code).

how can I get rid of it?

jkingsnorth’s picture

Issue summary: View changes

Judging by the error message and the fact that the code is still working, I think you're just missing the single quote marks around 'tid', so:

//123 means term $0-99
if($this->entity->field_price_range['und'][0]['tid'] == 123){
  if($this->value > 99 || $this->value < 0){
    $this->set_error();
  }
}
//124 means term $100-500
if($this->entity->field_price_range['und'][0]['tid'] == 124){
  if($this->value > 500 || $this->value < 100){
    $this->set_error();
  }
}

I hope this fixes the issue

jkingsnorth’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.