In Phalcon 2.1 Phalcon\Mvc\Model\Validation is deprecated.
How can check Uniqueness by two fields in phalcon 2.1?
// Sample code in Phalcon 2.0.x
public function validation()
{
$this->validate(new Uniqueness(array(
'field' => array('email', 'name')
)));
}
You have to pass the model as an option: https://github.com/phalcon/cphalcon/blob/2.1.x/phalcon/validation/validator/uniqueness.zep#L87
'model' => $this
Also, for questions like this you can use the forum: https://forum.phalconphp.com/
@andresgutierrez
I did not understand how it should be done.
The code that I used to validate:
public function validation()
{
$validator = new Validation();
$validator->add('reference', new Validation\Validator\Uniqueness([
'model' => $this,
'message' => 'Duplicate Reference Code',
'cancelOnFail' => true
]));
return $this->validate($validator);
}
How do I specify unique constraint for multiple columns. for example reference + userId columns?
What should be changed?
This is not implemented in phalcon 2.1 now.
My workaround:
public function validation()
{
$validator = new Validation();
$selfId = ($this->id) ?: 0;
$count = self::count([
'email = ?0 AND name = ?1 AND id != ?2',
'bind' => [$this->email, $this->name, $selfId]
]);
if ($count) {
$this->appendMessage(new Message('Duplicate !'));
return false;
}
// other validations
return $this->validate($validator);
}
(updated: add $selfId to exclude validating itself when update the record)
So it's time to do PR :)
Yes, I noticed this was broken for us too. With the new model one cannot set multi-field uniqueness checks anymore.
I have some ideas for how it would look. Perhaps we could decide on how we would like it to be first.
$validator->add(null, new Validation\Validator\Uniqueness([
'model' => $this,
'fields' => ['field1', 'field2']
]));
$validator->add(['field1'], ['field2'], new Validation\Validator\Uniqueness([
'model' => $this
]));
$validator->add('field1', new Validation\Validator\Uniqueness([
'model' => $this,
'also' => ['field2', 'field3']
]));
It would be great to have this worked out before Phalcon 2.1 goes into 3 years of LTS. If we don't address this now then we could be stuck with a weird setup for multiple fields for the next 5 years.@sergeyklay @andresgutierrez Please spend just a few minutes to think about this before releasing 2.1.
Ok so sure there are tons of bugs on the issue tracker that need to be addressed but I think that many of those can be fixed in minor changes over the next 3 years. This is different because its a regression in functionality and there is no super obvious way to implement it.
@dschissler absolutely agree.
@dschissler @DimazzzZ @sergeyklay @andresgutierrez
Well im looking into it. But it will require to allow just passing array of fields to add method. Im gonna do some PR to this in few days.
Just as i see in old validator if we was passing an array it was builiding condition just with two parameters. Is it gonna be the same if we just gonna do for in fields we are passing as array in add method and do uniquness for each item in this array ? I think yes but im not sure if the logic will be same ?
What about just:
$validator->add(['field1', 'field2'], new Validation\Validator\Uniqueness([
'model' => $this // also this will be removed beacause we can access validation->getEntity(), of course it will access model if getEntity will return null
]));
?
Well also i will fix some problems if i found any. Not sure about this here:
https://github.com/phalcon/cphalcon/blob/2.1.x/phalcon/validation/validator/exclusionin.zep#L64 Is it right that we checking type before setting it ? I think this check should be done after setting strict ?
@Jurigag Could you please create PR?
However that syntax could be confusing, it could be:
@andresgutierrez this syntax is the same as in the old implementation of Phalcon 1.3.*. As for me it means exactly "Add this validator to these fields at the same time".
Well actually in old validation it was working diffrent - fields were in uniqueness validator. And there were query etc build for 2 fields. Now with this PR i changed it https://github.com/phalcon/cphalcon/pull/11826 - bascially if we pass array of fields to any validator it's just doing loop in those fields and adding validator for field like it would be added as single field. It's working also for all validators, not only Uniqueness. Just check PR there.
In old validation this loop was just done in uniqueness validator. Now it's done in validation when adding it, result should be the same - advantage is that we can use it with any validator(i really like it beacause for me having like 20 fields with presenceof for example is weird). Im guessing with this PR i done this will be slightly slower but less memory usage i guess.
@bluetec @dschissler Could you please check now 2.1.x? See #11826
I'm building it now and I'll work with it tomorrow.
It works for me so probably this should be closed. @Jurigag is working on an internal cleanup but it won't affect users.
@dschissler done, added CombinedFieldsValidator
Sweet. I saw it. That solution turned out to be very easy and nice.
Well, can I close this?
I guess.
Yes. Its totally addressed.
馃憤
Most helpful comment
Yes, I noticed this was broken for us too. With the new model one cannot set multi-field uniqueness checks anymore.