4

我将一个模型文件用于 2 个表单。一个用于注册,其他用于添加成员

我没有为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如何为场景设置字段验证消息?任何帮助/提示/建议都是可观的。

4

1 回答 1

12

我可以知道为什么您需要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也必须设置,否则将使用默认场景。


Model没有变化。一切都应该与您的示例相同。

如果您想让它成为唯一的电子邮件,您还必须对Model进行更改:

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()],
}
于 2016-07-30T19:40:10.373 回答