I have a virtual attribute ‘date_of_birth’.
In my model I have a afterValidate function:
public function afterValidate()
{
if(empty($this->dob_day) || empty($this->dob_month) || empty($this->dob_year))
{
$this->addError('date_of_birth', 'date of birth is incomplete');
}
return parent::afterValidate();
}
Form Field:
<div class="row">
<!-- dropdowns for DD, MM, YY -->
<?php echo $form->hiddenField($model, 'date_of_birth'); // render hidden field for ajax validation ?>
<?php echo $form->error($step1, 'dob_day'); ?>
<?php echo $form->error($step1, 'dob_month'); ?>
<?php echo $form->error($step1, 'dob_year'); ?>
<?php echo $form->error($model, 'date_of_birth'); ?>
</div>
This should add an error to the ‘date_of_birth’ attribute but this only happens with ‘validateOnSubmit’ and not with ‘validateOnChange’. I checked the console JSON response and it is identical for both methods.
I have set up the ‘dob_day, dob_month, dob_year’ fields as ‘safe’ in the model validation rules.
Is this a bug or have I missed something out?