php - 自定义验证不工作- Yii2-app-basic-Yii2

标签 php yii2 custom-validators yii2-basic-app

我昨天发布了一个关于单选按钮自定义验证的问题 Textfield Mandatory On Radio Button .我得到了答案。但是,这不是确切的答案。但是,它解决了我的一个问题。

实际上,我有 2 个单选按钮。

  • 个人
  • 公司

When, Radio Button having "Individual"value is selected, CompanyName textbox should not be mandatory. But, when Radio Button having "Firm"value is selected, CompanyName textbox should be mandatory.

现在发生的事情是,当我选择单选按钮“公司”并且没有为 CompanyName 文本框填充任何值时,数据没有插入到数据库表中。美好的。没关系。但是,错误消息没有以表格形式显示。选择单选按钮公司后,应显示错误消息,因为公司名称文本框是必需的。

我没有明白我做错了什么。这是我的 View 、 Controller 和模型代码。请帮助我。

register.php(查看)

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;
use yii\bootstrap\Modal;
use yii\helpers\Url;
?>

<?php $form = ActiveForm::begin(['id' => 'register-form']); ?>
    .
    .
    .
    <?= $form->field($model, 'AdminType')
            ->radioList(array('Individual'=>'An Individual', 'Firm'=>'Firm/Company'))
            ->label('Are You')?>
    <?= $form->field($model, 'CompanyName')->textInput()->label('Company Name')->error() ?>
    .
    .
<?php ActiveForm::end(); ?>

SiteController.php( Controller )

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\swiftmailer\Mailer;
use app\models\RegisterForm;

public function actionRegister()
{
    // Register Model
    $model = new RegisterForm();
    if ($model->load(Yii::$app->request->post())) 
    {
        $post = Yii::$app->request->post('RegisterForm');
        if ($model->validate())
        {

        }
        else
        {
            // HERE YOU CAN PRINT THE ERRORS OF MODEL
            echo "<pre>";
            print_r($model->getErrors());
            echo "</pre>";
        }
        return $this->refresh();
    }

}

RegisterForm.php(模型)

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use kartik\password\StrengthValidator;


class RegisterForm extends Model
{
    public $fname;
    public $lname;
    public $email;
    public $password;
    public $confirmPassword;
    public $AdminType;
    public $CompanyName;
    public $verifyCode;

    public function rules()
    {
        return [
                [['fname','lname', 'email', 'password','confirmPassword','verifyCode','AdminType'], 'required'],
                ['email', 'email'],
            ['confirmPassword', 'compare', 'compareAttribute' => 'password'], 
                ['verifyCode', 'captcha'],

        //add rule that uses the validator function
                ['AdminType','radioValidator'],
        ];
    }

    //implement the validator
    public function radioValidator($attribute)
    {
        if($this->$attribute === 'Firm')
            return $this->addError('CompanyName', 'Company Name cannot be blank');
    }
}

最佳答案

您应该在 ActiveForm 中添加 'enableAjaxValidation' => true

完成后你的代码应该是,

控制者

public function actionRegister()
{
    $model = new RegisterForm();
    if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
      \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
      return \yii\widgets\ActiveForm::validate($model);
    }

    if ($model->load(Yii::$app->request->post())) 
    {
    }
    else
    {
        // HERE YOU CAN PRINT THE ERRORS OF MODEL
        echo "<pre>";
        print_r($model->getErrors());
        echo "</pre>";
    }
    return $this->refresh();
}

查看

<?php $form = ActiveForm::begin([
           'id' => 'register-form',
           'enableAjaxValidation' => true,
]); ?>
    .
    .
    .
    <?= $form->field($model, 'AdminType')
            ->radioList(array('Individual'=>'An Individual', 'Firm'=>'Firm/Company'))
            ->label('Are You')?>
    <?= $form->field($model, 'CompanyName')->textInput()->label('Company Name')->error() ?>
    .
    .
<?php ActiveForm::end(); ?>

它可能对你有帮助。

关于php - 自定义验证不工作- Yii2-app-basic-Yii2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32969883/

相关文章:

php - 是否可以倒回 PDO 结果?

php - Jquery Mobile - 获取 ajax 结果问题

Yii2注销链接问题

grails - 如何在Grails中使用自定义验证器

PHP rand() ...获得真正的 50/50 结果?

php - 如何打印从Wamp服务器获取的图像?

php - Yii2在Docker上高级

mysql - 如何在 Yii2 中制作多个 UPSERT?

angular - 自定义验证访问表单组件

java - JSF 2 validator 无法使用数据表中的 "var"