Using custom validation (checkDay) with custom scenario, and empty input form.
Custom validation code (checkDay) executed even if input form is empty.
Custom validation code (checkDay) not executed when input form is empty.
I have data according to the table below. The field work_day and day_off has been filled with number 7 and 0. Then I input the field work_day_n with '1,2,3,4,5,6,7' (day count matching work_day value) and day_off_n with _blank_ (cause day_off is zero). The validation rule has been set (below), but because day_off_n input form is empty, the validation rule is not executed for this field. It should be executed because for those field I set to use custom validation.
My table:
name type value
--------------------------------
work_day tinyint(3) 7
work_day_n varchar(100)
day_off tinyint(3) 0
day_off_n varchar(100)
Controller:
public function actionSetDay() {
$model = new SmartPlan();
$model->scenario = SmartPlan::SCENARIO_SET_DAYS;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', 'Success.');
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', ['model' => $model]);
}
}
Model:
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::SCENARIO_SET_DAYS] = ['day_off_n', 'work_day_n'];
return $scenarios;
}
public function rules()
{
return [
[['day_off'], 'integer', 'max' => 12, 'min' => 1],
[['work_day'], 'integer', 'max' => 12, 'min' => 1],
[['day_off_n', 'work_day_n'], 'string'],
[['day_off_n'], 'checkDay', 'on' => self::SCENARIO_SET_DAYS],
[['work_day_n'], 'checkDay', 'on' => self::SCENARIO_SET_DAYS],
];
}
public function checkDay($attribute, $params)
{
// days filled separated by comma. explode to array.
die('here is not executed when input form is empty / blank');
}
Yii version 2.0.8 / 2.0.11.2
PHP version 7.1.2
OS Archlinux
Please try to set skipOnEmpty => false like this..
[['day_off_n'], 'checkDay', 'on' => self::SCENARIO_SET_DAYS, 'skipOnEmpty' => false],
[['work_day_n'], 'checkDay', 'on' => self::SCENARIO_SET_DAYS, 'skipOnEmpty' => false],
Thank you for your question.
In order for this issue tracker to be effective, it should only contain bug reports and feature requests.
We advise you to use our community driven resources:
If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.
_This is an automated comment, triggered by adding the label question._
See http://www.yiiframework.com/doc-2.0/guide-input-validation.html#inline-validators:
Note: By default, inline validators will not be applied if their associated attributes receive empty inputs or if they have already failed some validation rules. If you want to make sure a rule is always applied, you may configure the skipOnEmpty and/or skipOnError properties to be false in the rule declarations.
@hirenbhut93 @samdark
If we use skipOnEmpty => false, then the input form must not be empty. I've tried the skipOnError too. For my use case, the form can be empty or not empty. What I want is the validator will always check the associated attributes whether those attributes is empty or not. Error or not will be decided by the custom validator. Is there any other way for that? Thanks.
If we use skipOnEmpty => false, then the input form must not be empty.
No. skipOnEmpty does exactly what it says: skipping validation if the value is empty. Setting it to false doesn't mean you require value to be non-empty. It means only that the validation rule will be executed on empty values as well. Inside your validation function you can decide if empty value is valid or not.
@samdark:
Yes, the validation rule is executed on empty value. Turn out my code is in the wrong order, and the skipOnEmpty is working. Many thanks for the help.
You're welcome.