dpgu5074 2014-12-11 10:55
浏览 103
已采纳

场景无法使用Yii 2进行表单验证

Using a AJAX based form with Yii 2 and everything is working fine except the single rule that is defined for a specific scenario; if I leave the field blank it passes validation fine, which it shouldn't.

The JS:

$(document).ready(function() {

    /* Processes the company signup request */

    $('#company_form').submit(function(e) {

        // The below helps stop double submits caused by submitting via ajax
        e.preventDefault();
        e.stopImmediatePropagation();

        // Sign them up
        signup('company');

        return false;

    });

})

function signup(type) {

    var url;
    var type2;
    var field;
    var form_id;

    // Set file to get results from..

    switch (type) {
        case 'company':
            url = '/site/company-signup';
            form_id = 'company_form';
            type2 = type;
            break;
        case 'client':
            url = '/site/client-signup';
            form_id = 'client_form';
            type2 = type;
            break;
    }

    // Set parameters
    var dataObject = $('#' + form_id).serialize();

    // Run request  

    getAjaxData(url, dataObject, 'POST', 'json')

        .done(function(response) {

            if (response.result) {

                //......

            } else {

                //.......

            }

        })

        .fail(function() {

            //.....

        });

    // End

}

function getAjaxData(loadUrl, dataObject, action, type) {

    return jQuery.ajax({
        type: action,
        url: loadUrl,
        data: dataObject,
        dataType: type
    });    

}

My controller code:

$signup = new SignupForm(['scenario' => 'company']);

if (Yii::$app->request->isAjax) {

    if ($signup->load(Yii::$app->request->post()) and $signup->validate()) {

        //.......

    } else {

        //.......

    }

    // Output response
    echo json_encode($data);

}

The model code:

private $db;
public $company_name;
public $first_name;
public $last_name;
public $email;
public $username;
public $password;
public $password_again;

/**
 * Validation rules
 * @return array An array of validation rules
 */ 

public function rules() {       
    return [
        // Format some data
        [['company_name', 'first_name', 'last_name', 'email', 'username', 'password', 'password_again'], 'trim'],
        ['username', 'filter', 'filter' => 'strtolower'],
        [['company_name', 'first_name', 'last_name', 'email', 'username', 'password', 'password_again'], 'sanitize'],           
        // If company scenario, require company name
        ['company_name', 'required', 'on' => 'company'],
        // Require all other fields
        [['first_name', 'last_name', 'email', 'username', 'password', 'password_again'], 'required'],

        //......

}

As you can see the 4th rule should require the company_name field if we are in the company scenario, but it doesn't.

What am I doing wrong here?

  • 写回答

2条回答 默认 最新

  • douhuang1852 2014-12-12 10:15
    关注

    Ok, worked it out. Not sure if it's a Yii bug, but:

    $signup = new SignupForm(['scenario' => 'company']);
    

    That doesn't work by itself. If you are going to do that, then you need something like this in your model:

    function __construct($config) {
        $this->scenario = $config['scenario'];
    }
    

    ...or alternatively you can do something like this:

    $signup = new SignupForm();
    $signup->scenario = 'company';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组