php – 如何在场景中为具有ajax验证的字段设置验证消息? – Yii2

前端之家收集整理的这篇文章主要介绍了php – 如何在场景中为具有ajax验证的字段设置验证消息? – Yii2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用一个模型文件2个表单.一个用于SIGNUP&其他用于添加成员.

我没有为SIGNUP表单设置任何场景.但是,设置了添加成员表单的方案.

模型

public function rules() {
  return [

    //Add Members
    ['first_name','required','message'=>'Please enter first name.','on'=>'addteammembersidebar'],['email','message'=>'Please enter email address.',['mobile','message'=>'Please enter mobile number.',//Common
    ['first_name','message'=>'Please enter your first name.'],'message'=>'Please enter your email address.'],'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; ?>

除电子邮件字段外,所有验证错误消息均有效如果,我删除’enableAjaxValidation’=>从现场来看,它是有效的.但是,对我来说’enableAjaxValidation’=>是必需的.

图片

与图像一样,可以清楚地看到错误消息“请输入您的电子邮件地址”.哪个应该是“请输入电子邮件地址.”.只有电子邮件字段验证错误消息不正确.除了一切都很好.

如何为场景的电子邮件字段设置验证消息?任何帮助/提示/建议都很明显.

我可以知道您为什么需要在这里使用AjaxValidation进行电子邮件验证?对于这种类型,只需要在没有它的情况下编写它就足够了,因为当您想要从数据库或其他模型中搜索和检索数据时,AjaxValidation更适合,而不是模型本身.

但是,如果您觉得需要AjaxValidation,则必须设置一些不同的东西,因为您当前的代码将无效.

在View中设置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,['enableAjaxValidation' => true])->label(false);

为什么需要这个?您已将AjaxValidation设置为活动状态,但您尚未设置此Ajax可以使用的URL.在大多数情况下,它在ActiveForm :: begin()中设置.

在Controller中设置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->场景,否则它将使用默认场景.

模型没有变化.一切都应该保持与你的例子相同.

如果您想要制作独特的电子邮件,您还必须对模型进行更改:

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','message' => 'Email must be unique','targetClass' => User2::className()],}
原文链接:https://www.f2er.com/php/135404.html

猜你在找的PHP相关文章