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条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么