Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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!



32.5k

Hi

To print message you need setup it http://docs.phalconphp.com/en/latest/reference/flash.html. Then in controller validity it



6.0k

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 ?



1.1k
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 ?



1.1k

Yes, I have use it in controller.thanks

Hi

To print message you need setup it http://docs.phalconphp.com/en/latest/reference/flash.html. Then in controller validity it



1.1k

Yes, I have use it in controller.thanks

Hi

To print message you need setup it http://docs.phalconphp.com/en/latest/reference/flash.html. Then in controller validity it



6.0k

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



6.0k
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



1.1k

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



6.0k
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