Brett
Brett

Reputation: 20079

Scenario not working on form validation with 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?

Upvotes: 0

Views: 4919

Answers (2)

Brett
Brett

Reputation: 20079

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';

Upvotes: 1

Fortran
Fortran

Reputation: 593

But in 1th rule you have "company_name" field that it is not in scenario. Why did you use this?

public function scenarios() {...}

Upvotes: 0

Related Questions