I have a couple of columns (ip, provider_id) for which I want combinations of values to always be unique. Therefore, I am trying to build a custom validation function. But I am having issues grabbing on the value of the secondary field. This is my code so far in the model:
public $validate = array(
'ip' => array(
'rule' => array('uniqueClick', 'provider_id'),
'message' => 'The click is not unique.'
)
);
public function uniqueClick ($ip, $field) {
$count = $this->find('count', array('conditions' => array('ip' => $ip, 'provider_id' => $field)));
// echo $field;
return $count == 0;
}
So the problem is that when I am testing what value is loaded into $field, it's just 'provider_id', a string. I was hoping it would contain the value of the 'provider_id' field. Does anyone know how to grab that value (and all other secondary model field values if necessary) and send it to the custom validation function?
My reading in the CookBook and people who've discussed similar problems seemed to suggest this solution would work, but not for me unfortunately.
Thanks in advance!
Cake is definitely behaving the way it's supposed to there. The second paramameter that you pass in that 'rule' array is meant to be passed as a static value.
However, your provider_id should be available in $this->data['MyModel']['provider_id']
So you should be able to forget about that second parameter completely, and do:
Hope that helps!
To complement Joshua's answer, the validation array should be build like this:
you can also use my rule with is able to work with as many fields as you need
try http://www.dereuromark.de/2011/10/07/maximum-power-for-your-validation-rules/ and https://github.com/dereuromark/tools/blob/master/Model/MyModel.php#L930
so basically the same than you tried:
You can directly go with this solution without passing anything in validation rules except the custom unique function.
$this->data[$this->alias]['provider_id'] automatically gets the value of the provider_id.