1

I'm trying to find a way to set a default value for any field (not filled by user input) in Yii2 as to avoid a "Cannot be NULL" database exception.

Is there any way that this could be done?

Cheers.

2 Answers 2

5

Yii2 Default Validator - in model validation rules pass all attributes, which you want to be filled with default values, like:

[['attribute', 'second_attribute'], 'default', 'value' => 'something_here'],
Sign up to request clarification or add additional context in comments.

Comments

2

You could add the default values to the model database table

Than use loadDefaultValues() when creating an instance of your model

$model = new Post();
$model->loadDefaultValues();

1 Comment

loadDefaultValues() used to load defalut values from db , " cannot be null" error occurs if there is no default value in batabase not even null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.