我正在为2个表单使用一个模型文件。一个用于 SIGNUP &其他用于A dding成员。
我没有为 SIGNUP 表单设置任何方案。但是,设置添加成员表单的方案。
模型
public function rules() {
return [
//Add Members
['first_name', 'required','message'=>'Please enter first name.','on'=>'addteammembersidebar'],
['email', 'required','message'=>'Please enter email address.','on'=>'addteammembersidebar'],
['mobile','required','message'=>'Please enter mobile number.','on'=>'addteammembersidebar'],
//Common
['first_name', 'required','message'=>'Please enter your first name.'],
['email', 'required','message'=>'Please enter your email address.'],
['mobile','required','message'=>'Please enter your mobile number.'],
];
}
查看
在这里,我设置了类似$modelTeamMembers->scenario = 'addteammembersidebar';
的场景。
<?php foreach ($modelsTeamMembers as $indexMember => $modelTeamMembers):
$modelTeamMembers->scenario = 'addteammembersidebar';
?>
<tr class="house-item">
<td class="vcenter">
<?php
// necessary for update action.
if (! $modelTeamMembers->isNewRecord) {
echo Html::activeHiddenInput($modelTeamMembers, "[{$indexMember}]id");
}
?>
<?php
$modelTeamMembers->first_name = $first_name;
echo $form->field($modelTeamMembers, "[{$indexMember}]first_name")->label(false);
?>
</td>
<td>
<?php
$modelTeamMembers->last_name = $last_name;
echo $form->field($modelTeamMembers, "[{$indexMember}]last_name")->label(false);
?>
</td>
<td>
<?php
$modelTeamMembers->email = $email;
echo $form->field($modelTeamMembers, "[{$indexMember}]email",['enableAjaxValidation' => true])->label(false);
?>
</td>
<td>
<?php
$modelTeamMembers->mobile = $mobile_number;
echo $form->field($modelTeamMembers, "[{$indexMember}]mobile",
['inputOptions' => ['class' => 'form-control', 'maxlength'=>"10"]])->label(false);
?>
</td>
</tr>
<?php endforeach; ?>
除email
字段外,所有验证错误消息均有效。如果,我从字段中删除'enableAjaxValidation' => true
,它就有效。但是,对我来说'enableAjaxValidation' => true
是必需的。
图片
在图片中,可以清楚地看到错误消息“请输入您的电子邮件地址。”应该是“请输入电子邮件地址。”。只有email
字段验证错误消息不正确。除了一切都很好。
如何为场景设置email
字段的验证消息?任何帮助/提示/建议都很明显。
答案 0 :(得分:10)
我是否知道为什么您需要在此处使用AjaxValidation
进行电子邮件验证?对于这种类型,只要你想要从数据库或其他模型中搜索和检索数据而不是模型本身,AjaxValidation
更适合这种类型就足够了。
但是,如果您认为自己需要AjaxValidation
,则必须设置一些不同的内容,因为您当前的代码将无效。
在查看:
中设置AjaxValidation
// Set to: index.php?r=profile/email-validation
$form = ActiveForm::begin(['validationUrl' => ['profile/email-validation']]);
// This is set correctly (no changes are needed comparing to your attempt)
echo $form->field($modelTeamMembers, "[{$indexMember}]email", ['enableAjaxValidation' => true])->label(false);
为什么需要这个?您已将AjaxValidation
设置为活动状态,但尚未设置此Ajax可用的URL。在大多数情况下,它在ActiveForm::begin()
中设置。
在控制器中设置AjaxValidation
(必填):
// Method that renders your view file
public function actionSomethingThatRendersView()
{
// Code here
$user->scenario = 'addteammembersidebar'; // Custom scenario name
return $this->render(/* the remaining code goes here */);
}
// This is the method that Ajax will send request to ($_POST[])
public function actionEmailValidation()
{
$post = Yii::$app->request->post();
if (!empty($post))
{
Yii::$app->response->format = Response::FORMAT_JSON; // Must be in JSON format
$profile = new User(); // Edit your class name in here
// Custom scenario (must be the same as above otherwise you might get unexpected response)
$profile->scenario = 'addteammembersidebar';
$profile->load($post);
return ActiveForm::validate($profile);
}
}
为什么需要这个? Ajax将发送请求,但没有任何操作的请求将不会执行任何操作。这将“创建具有相同规则和属性的新对象”,并将尝试使用新的数据集进行验证。对于渲染方法,还必须设置$obj->scenario
,否则它将使用默认方案。
模型没有变化。一切都应该和你的例子一样。
如果您想要制作独特的电子邮件,您还必须对模型进行更改:
public function rules()
{
// ...
['email', 'unique', 'message' => 'Email must be unique'],
// If your attribute is not in the same table as defined in class, then:
['email', 'unique', 'message' => 'Email must be unique', 'targetClass' => User2::className()],
}