We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Uniqueness with combination of a set of fields

Hi guys,

Could anyone explain to me how I might validate the uniqueness of a combination of a set of fields? The documentation at https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Validator_Uniqueness.html mentions the possibility to do this, but it doesn't offer an example.

Would it be as simple as doing something like this (?):

$this->validate(new Uniqueness(array(
    'field' => 'id, email'
)));
if ($this->validationHasFailed() == true) {
    return false;
}

I'll be trying that one out sometime tonight, but if anyone reads this in the mean time and knows the answer, I'd be grateful if you'd share your knowledge with me. Thanks!

My guess is you should pass multiple fields as an array, not a comma separated list. You're probably trying to avoid this - but you can of course put in separate validation rules for each field.



98.9k

It must work this way:

$this->validate(new Uniqueness(array(
    'field' => array('id', 'email')
)));
if ($this->validationHasFailed() == true) {
    return false;
}