2015-04-07 69 views
3

,它應該是這樣的:yii.activeform.js在確定項目產生無效驗證js腳本

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
       jQuery('#form-signup').yiiActiveForm({ 
          "username": { 
           "validate": function(attribute, value, messages) { 
            yii.validation.required(value, messages, { 
             "message": "Username\u4e0d\u80fd\u4e3a\u7a7a\u3002" 
            }); 
            yii.validation.string(value, messages, { 
             "message": "Username\u5fc5\u987b\u662f\u4e00\u6761\u5b57\u7b26\u4e32\u3002", 
             "min": 2, 
             "tooShort": "Username\u5e94\u8be5\u5305\u542b\u81f3\u5c112\u4e2a\u5b57\u7b26\u3002", 
             "max": 255, 
             "tooLong": "Username\u53ea\u80fd\u5305\u542b\u81f3\u591a255\u4e2a\u5b57\u7b26\u3002", 
             "skipOnEmpty": 1 
            }); 
           }, 
           "id": "signupform-username", 
           "name": "username", 
           "validateOnChange": true, 
           "validateOnType": false, 
           "validationDelay": 200, 
           "container": ".field-signupform-username", 
           "input": "#signupform-username", 
           "error": ".help-block" 
          }, 
</script> 

,但在我的項目,當我輸入電子郵件或用戶名是無效的,沒有任何提示或錯誤顯示!當我檢查由yii生成的代碼時,我看到yiiActiveForm的空參數

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
     jQuery('#form-signup').yiiActiveForm([], []); 
    }); 
</script> 

誰能告訴我爲什麼?供應商文件夾有什麼問題嗎?

+1

向我們展示你的模型規則和表單視圖 – soju

回答

0

這可能是因爲您的模型類未設置爲正確的方案,導致驗證被忽略。請有關此主題查看文檔:

http://www.yiiframework.com/doc-2.0/guide-structure-models.html#scenarios

如果你想在當前視圖施加一定的規則,你需要在創建模型的實例來設置場景:

// scenario is set as a property 
$model = new User; 
$model->scenario = 'login'; 

// scenario is set through configuration 
$model = new User(['scenario' => 'login']); 

在模型中,上述用戶的情況下,你將有此功能:

namespace app\models; 

use yii\db\ActiveRecord; 

class User extends ActiveRecord 
{ 
    public function scenarios() 
    { 
     return [ 
      'login' => ['username', 'password'], 
      //more scenarios would follow here... 
     ]; 
    } 
}