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

Is this a bug of Phalcon\Mvc\Model\Validator\PresenceOf?

I use it like this:

    $this->validate(new PresenceOf(array('field'=>'name', 'message'=>'客户名称不能为空')));
    $this->validate(new Uniqueness(array('field'=>'name', 'message'=>'客户名称不可重复')));

But the message is "name is required" when name is empty.Why? The message is right when name exists. Please help me,thanks!



58.4k

Hi

To print message you need setup it https://docs.phalcon.io/en/latest/reference/flash.html. Then in controller validity it



11.2k

Hello, i just tested here and it is working OK.

A question, are you sure that those validations are inside the public function validation() on your model ?



2.3k
edited Dec '14

I'm sure,thank you! public function validation() { $this->validate(new PresenceOf(array('field'=>'name', 'message'=>'客户名称不能为空'))); $this->validate(new Uniqueness(array('field'=>'name', 'message'=>'客户名称不可重复'))); $this->validate(new PresenceOf(array('field'=>'phone', 'message'=>'手机号码不能为空'))); $this->validate(new Uniqueness(array('field'=>'phone', 'message'=>'手机号码必须唯一'))); if ($this->validationHasFailed() == true) { return false; } }

Hello, i just tested here and it is working OK.

A question, are you sure that those validations are inside the public function validation() on your model ?



2.3k

Yes, I have use it in controller.thanks

Hi

To print message you need setup it https://docs.phalcon.io/en/latest/reference/flash.html. Then in controller validity it



2.3k

Yes, I have use it in controller.thanks

Hi

To print message you need setup it https://docs.phalcon.io/en/latest/reference/flash.html. Then in controller validity it



11.2k

Just tested here. My model isn't even calling the validation() function. It seems phalcon first execute the automatic PresenceOf validation based on the NotNull fields on database and then calls the model validation() event



11.2k
edited Dec '14

Just took a look at the models source code. That happens because Phalcon checks if there are NotNull fields and validates them BEFORE it executes the custom validation() function.

You can disable this behavior if you dont like it at your DI by using the following code:

    \Phalcon\Mvc\Model::setup(array(
        'notNullValidations' => false
    ));

But be aware that it will disable the automatic NotNull validations on ALL models, so you'll have to write a presenceOf() validation for each NotNull field on each and every model



2.3k

Thank you very much! I set the field to "null" in database,the problem is OK!

Just tested here. My model isn't even calling the validation() function. It seems phalcon first execute the automatic PresenceOf validation based on the NotNull fields on database and then calls the model validation() event



11.2k
Accepted
answer

Check my last answer. It explains the reason you were getting those messages and how to disable that feature (althrough i don't recomend you to do that if you are not going to write all validations for all your models).

Good luck!

Thank you very much! I set the field to "null" in database,the problem is OK!

Just tested here. My model isn't even calling the validation() function. It seems phalcon first execute the automatic PresenceOf validation based on the NotNull fields on database and then calls the model validation() event