Yii2 ActiveForm在提交按钮单击

时间:2018-04-05 06:48:30

标签: php forms yii2

我的问题是,点击提交按钮后,它会删除<div class="help-block">并显示错误消息,无论是否验证表单。它仅在页面刷新后显示。正如您在下面的图片中看到的那样,当输入为空时,它会出现红色边框并在下面显示错误消息,但是在提交按钮后单击错误消息将被删除,但红色边框会保留。在控制台中,每次点击后都有一个POST发送,表格验证不重要,但如果验证有两个POST。
正如我注意到的那样,当我输入不是电子邮件地址和推送提交按钮的输入电子邮件时,它会显示帮助块,其中包含有关错误电子邮件地址的消息。当电子邮件输入为空时,仅删除帮助块 it is on blur

it is after submit button click

以下是我的代码。

视图:

<div class="email">
                <?php $form = ActiveForm::begin([
                    'id' => 'email-form',
                    'action' => ['polisy/wyslij'],
                    'errorCssClass' => 'input-error',
                    ]); ?>
            <div class="row">
                <?= $form->field($model, 'emailAdress')->input('email',[
                        'class' => 'form_renewal',
                        'placeholder' => 'podaj adres email',
                    ]
                )->label(false);?>
                <span class="spacer_email"></span>
                <?= $form->field($model, 'renewalSelect')->dropdownList([
                        'renewal' => 'odnowienie', 
                        'reminder' => 'przypomnienie',
                    ],
                    [
                        'class' =>'renewal-select',
                        'id' => 'renewal-select',
                    ]
                )->label(false); ?>

                <span class="plus" style="display: none;">&#43</span>
                    <?/*= $form->field($model, 'title')->textInput([
                            'class' => 'form_renewal', 
                            'placeholder'=>'nazwa szablonu / tytuł',
                        ]
                    )->label(false) */?>
                <span class="save icon-save" style="display: none;"></span>
            </div>
            <div class="row">
                <?= $form->field($model, 'email_text')->widget(TinyMce::className(), [
                    'options' => ['rows' => 6],
                    'language' => 'pl',
                    'clientOptions' => [
                        'plugins' => [
                            "advlist autolink lists link charmap print preview anchor",
                            "searchreplace visualblocks code fullscreen",
                            "insertdatetime media table contextmenu paste"
                        ],
                        'toolbar' => "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
                    ]
                ])->label(false);?>
            </div>
            <div class="row btn_submit " id="row_submit_btn">
                <?= Html::submitButton('wyślij email', ['class' => 'btn btn_modal']) ?>
            </div>
                <?php ActiveForm::end(); ?>
        </div>

验证规则:

    public function rules()
    {
        return [
            [['emailAdress', 'renewalSelect','email_text'], 'required', 'message' => 'pole wymagane'],
            ['emailAdress', 'email', 'message' => 'to nie jest prawidłowy adres email'],
        ];
}

和控制器:

public function actionWyslij(){
  $model = new RenewalForm();
  $model->load(Yii::$app->request->post());
  if ($model->validate()) {
    $params = array(
        'loginName' => '',
        'agencyID' => ,
        'data' => array(
            'type' => 'email',
            'email' => $model->getEmailAdress(),
            'title' => $model->getTitle(),
            'message' => $model->getEmailText()
            ) 
        );
    $send = Yii::$app->api->call('/notification/send', $params);
    if($send){
      Yii::$app->api->getResponse();
      return $this->redirect('odnowienia?sent=true');
    }
    else {
      return $this->redirect('odnowienia?sent=false');
    }
  }
}

0 个答案:

没有答案