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?
 
     
    