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.

Skip system required property on validation

Hello, I didnt get any solution in my last thread http://forum.phalconphp.com/discussion/3256/skip-columns-on-validation. Are there any changes? This is really critical issue when you cant set system values before SQL execution which are required in database model. With current pattern you need set up all values before validation ie in controller instead of model. Do you plan any changes in core? It would be really great add new possibilities into model initialization.

If you have columns that are NOT NULL in the DB, but don't want to make them required in your model for some reason, there is a way to tell the model not to check for required fields. I'll be damned if I can find the function name though.



1.9k

To remove "not null" validation globally for all your models, do this:

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

Check this page: https://github.com/phalcon/cphalcon/issues/440

It would be really great if you find that function :) Can you define specific columns? Because if you disable check for all required field, its bad. I can change DB model too but this is really last step i want to do because database keep data consistency. Framework is only tool how to get data from DB.



1.9k
Accepted
answer
edited Oct '14

Yep, that's possible too, by using the default value for your DB table field.

public function beforeValidationOnCreate()
{
    if (!$this->createdAt) {
        $this->createdAt = new RawValue('default');
    }
}

Check http://forum.phalconphp.com/discussion/283/created-at-and-validation

SkipAttributes on anything is bad function. This will remove columns from SQL statement too. The best would be add function to skip validation.

This one is useful too BTW: http://forum.phalconphp.com/discussion/962/optional-columns-in-models

Looks like this will be the best solution. However there is no default value so I will set up current time instead.

Yep, that's possible too, by using the default value for your DB table field.

```php public function beforeValidationOnCreate() { if (!$this->createdAt) { $this->createdAt = new RawValue('default'); } } ```

Check http://forum.phalconphp.com/discussion/283/created-at-and-validation



1.9k

Michal, if you would've read the whole post, then you'd have seen the answer by Phalcon. His answer doesn't imply skipping attributes on anything; he is skipping attributes conditionally.

But hey: sorry for trying to help you out dude! ;)

Nah its ok. Finally have another opinion for my problem. However still think there should be option to disable validation for some columns. I use similar pattern in one project and this DB base validation isnt saving.