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 there some way to alter default model validation message, or stop them from taking precedence?

Hello,

I have various models that represent tables in my database with required fields. I see that if a field is set to NOT NULL, Phalcon generates a PresenceOf type validator and outputs an error message when saving if that field is empty.

For example, my username field may be set to NOT NULL in the database. If you fail to set a value for that field before saving, Phalcon outputs : username is required. I do not want this message being output.

I notice that if I add my own PresenceOf validator for that field, it is simply ignored and the generated one executes first.

I need to do one of two things:

  • Alter the message generated by the generated validators.
  • Remove the default validators (preferably specific ones, not globally).

Is there some method of accomplishing one (or both) of these things?

  • Bonus Question Since posting this question, I've noticed that when I enter a string value for an integer type database field, Phalcon doesn't reach my Numericality validator and instead spits out that damn field_name is required message. Non-numeric != empty. How can I end this madness?

Thanks!



58.4k
edited Nov '14

First, if you want to remove the default validators (preferably specific ones, not globally). You used skipAttributes method


    class User extends \Phalcon\Mvc\Model
    {
      public function initialize()
      {
          $this->skipAttributes(
              [
                  'usernasme',
                  'field2',
                  'field3',
                  'field4',
              ]
          );
    }

Alter the message generated by the generated validators.

I think should validate message for spectify



12.6k
edited Nov '14

First, if you want to remove the default validators (preferably specific ones, not globally). You used skipAttributes method


  class User extends \Phalcon\Mvc\Model
  {
    public function initialize()
    {
        $this->skipAttributes(
            [
                'usernasme',
                'field2',
                'field3',
                'field4',
            ]
        );
   }

Alter the message generated by the generated validators.

I think should validate message for spectify

That looks like a simple solution that will work well. I'm really glad I don't have to globally disable anything because I do enjoy how Phalcon validates by default. I will give this a try this evening after work.

I appreciate your help!

Edit: Actually, I just looked up the documentation and it looks like skipAttributes is utilized to disable to inserting/updating of particular attributes - allowing the database to fill in the defaults.

Here is where I read this: https://docs.phalcon.io/en/latest/reference/models.html#skipping-columns

Please correct me if my understanding is wrong.



26.3k
Accepted
answer
edited Nov '14

Check my answer from this post https://forum.phalcon.io/discussion/3486/check-if-variable-is-empty-using-presenceof.

I have described there how to "Remove the default validators (preferably specific ones, not globally).".

Then, define your own PresenceOf validators in order to "Alter the message generated by the generated validators".



12.6k

Check my answer from this post https://forum.phalcon.io/discussion/3486/check-if-variable-is-empty-using-presenceof.

I have described there how to "Remove the default validators (preferably specific ones, not globally).".

Then, define your own PresenceOf validators in order to "Alter the message generated by the generated validators".

Excellent, that looks like it may be exactly what I'm looking for. So it applies to all fields for that individual model, but an exception is thrown if you attempt to save an empty value? Sounds good to me.

Thanks!



26.3k

Yes. So you need to define PresenceOf for each mandatory field. If you don't to this it is a risk of exception.