如果yii2中的另一组字段为空,我想使一组字段成为必需字段.任何一组字段都需要完美地填充(必需)



我正在处理录取单,需要获取家长信息。因此,有一个条件是,任何一个父母的数据都需要完全填写。意味着如果我不填写父亲的详细信息,那么母亲的所有字段都将是必需的。如果我填写了父亲的任何信息字段,那么在父亲的详细信息中,所有字段都需要填写,反之亦然。

这是我的招生表格型号代码:

public function rules()
{
return [
[['firstname', 'middle_name', 'surname_name'],'required'],
[['residential_telephone_no',],'required'],
[['father_name', 'father_qualification', 'father_occupation', 'father_mobile_no', ], 'safe'],//this needs to be alternately required 
[['mother_name', 'mother_qualification', 'mother_occupation', 'mother_mobile_no', ], 'safe'],//this needs to be alternately required
[[ 'admission_date'], 'safe'],
[['form_id'], 'required'],
[['aadhaar_no'], 'match', 'pattern' => '/^d{12}$/', 'message' => 'Field must contain exactly 12 digits.',],
];
}

我只想捕捉父母的任何一个细节。

您可以使用yiivalidatprsValidator::$when回调来指定是否应该应用规则。如果应该使用规则,则回调应返回true。

例如,当母亲的名字为空时,我们可以制作包含父亲详细信息的字段:

[
['father_name', 'father_qualification', 'father_occupation', 'father_mobile_no'],
'required',
'when' => function ($model) {
return empty($model->mother_name);
}
]

最新更新