If using the ‘when’ property on a required validator to determine whether the attribute is required, the client side validation ignores that property and always returns required. Am I missing something there?
/**
* @inheritdoc
*/
public function clientValidateAttribute($model, $attribute, $view)
{
$options = [];
if ($this->requiredValue !== null) {
$options['message'] = Yii::$app->getI18n()->format($this->message, [
'requiredValue' => $this->requiredValue,
], Yii::$app->language);
$options['requiredValue'] = $this->requiredValue;
} else {
$options['message'] = $this->message;
}
if ($this->strict) {
$options['strict'] = 1;
}
$options['message'] = Yii::$app->getI18n()->format($options['message'], [
'attribute' => $model->getAttributeLabel($attribute),
], Yii::$app->language);
ValidationAsset::register($view);
return 'yii.validation.required(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
}
}
For example, I have the following rule on my model:
[['gdate','level'], 'required', 'when'=>function($model) {
return !$model->has_children && $model->start_date;
}],
When the client side validation is output, gdate and level inputs are always keyed as required.
What’s the best way to get around that, disable client side validation for that attribute and add custom script? Seems clunkier than necessary to do that, so I must be missing something…