2017-01-02 151 views
-1

如何知道在yii2主動表單中是否觸發了驗證? 我正在使用yii2表單驗證不起作用

$('#formId').yiiActiveForm('validate', true); 

驗證窗體,但它總是返回undefined。

+2

[中yii2 JS活性形式確認]的可能的複製(http://stackoverflow.com/questions/41422316/js-active-form-validation -in-yii2) –

+0

爲什麼你將'options'作爲'TRUE'來傳遞? –

+0

這兩個問題都不一樣。仔細讀。只有情況是相同的,即表單驗證... @ YasarArafath –

回答

0

嘗試

MODEL

例如,

public function rules() 

    { 
     return [ 

      [['first_name', 'last_name', 'email_address','city','contact_phone', 'Address', 'date_created'], 'required'], 

      ['contact_phone', 'unique'], 

      ]; 

    } 

FIRST_NAME像輸入姓名與您的視圖文件

在你VIEW文件

<div class="form-group" > 

<?= Html::activeLabel($model, 'first_name', ['class'=>'control-label col-sm-3']); ?> 

<div class="col-sm-6"> 

<?= Html::activeTextInput($model, 'first_name',['class' => ['form-control']]); ?> 

<?= Html::error($model, 'first_name',['style' => 'color:red;']); ?> 

</div> 

</div> 
1

觸發表單驗證試試這個:

var $form = $("#formId"), 
    data = $form.data("yiiActiveForm"); 
$.each(data.attributes, function() { 
    this.status = 3; 
}); 
$form.yiiActiveForm("validate"); 

我將已建立的驗證在JavaScript活性形式的功能,它會返回真/假。也許有用:

function checkForm(form_id){ 
    var $form = $("#"+form_id), data = $form.data("yiiActiveForm"); 
    $.each(data.attributes, function() { 
     this.status = 3; 
    }); 
    $form.yiiActiveForm("validate"); 
    if ($form.find('.has-error').length == 0) { 
     return true; 
    } 
    return false; 
} 

調用它:

checkForm("formId"); // it will be return true/false and also validating the form