2015-09-11 100 views
3

我有窗體,由ActiveForm小部件創建。用戶在那裏輸入波蘭郵政編碼。在適當的控制器我把DB輸入的數據,例如:Yii2,自定義驗證:clientValidateAttribute()無法正常工作

$company_profile_data->postal_code = $_POST['CompanyProfiles']['postal_code']; 
$company_profile_data->update(); 

我決定使用獨立驗證郵政代碼驗證。 規則對模型該屬性:

public function rules() { 
    return [ 
     //...some other rules... 
     ['postal_code', 'string', 'length' => [6,6]], 
     ['postal_code', PostalValidator::className()], //standalone validator 
    ]; 
} 

應用/組件/驗證/ PostalValidator類代碼

namespace app\components\validators; 

use yii\validators\Validator; 
use app\models\CompanyProfiles; 
use app\models\Users; 

class PostalValidator extends Validator { 
    public function init() { 
     parent::init(); 
    } 

    public function validateAttribute($model, $attribute) { 

    if (!preg_match('/^[0-9]{2}-[0-9]{3}$/', $model->$attribute)) 
     $model->addError($attribute, 'Wrong postal code format.'); 
    } 

public function clientValidateAttribute($model, $attribute, $view) { //want js-validation too 
    $message = 'Invalid status input.'; 
    return <<<JS 
if (!/^[0-9]{2}-[0-9]{3}$/.test("{$model->$attribute}")) { 
    messages.push("$message"); 
} 
JS; 
    } 
} 

所以,正確的代碼的例子是00-202

當我(在用戶角色)輸入不正確的值,重新加載頁面,我看到Wrong postal code format.消息,雖然我重新定義clientValidateAttribute方法和寫JS-validation,正如我所說,不會讓頁面重新加載。然後我再次按提交按鈕:此時頁面不重新載入,我看到Invalid status input.消息(所以,第二次按下JS時間觸發器)。但是,當我輸入正確的代碼後,我仍然看到Invalid status input.消息,沒有任何反應。

那麼,我的clientValidateAttribute()方法有什麼問題? validateAttribute()效果很好。

UPDATE 段從控制器

public function actionProfile(){ //can't use massive assignment here, cause info from 2 (not 1) user models is needed 
    if (\Yii::$app->user->isGuest) { 
     return $this->redirect('/site/index/'); 
    } 
    $is_user_admin = Users::findOne(['is_admin' => 1]); 
    if ($is_user_admin->id == \Yii::$app->user->id) 
     return $this->redirect('/admin/login/'); 

    $is_user_blocked = Users::find()->where(['is_blocked' => 1, 'id' => \Yii::$app->user->id])->one(); 
    if($is_user_blocked) 
     return $this->actionLogout(); 

//3 model instances to retrieve data from users && company_profiles && logo 
    $user_data = Users::find()->where(['id'=>\Yii::$app->user->id])->one(); 
    $user_data->scenario = 'update'; 

    $company_profile_data = CompanyProfiles::find()->where(['user_id'=>Yii::$app->user->id])->one(); 
    $logo = LogoData::findOne(['user_id' => \Yii::$app->user->id]); 
    $logo_name = $logo->logo_name; //will be NULL, if user have never uploaded logo. In this case placeholder will be used 

    $upload_logo = new UploadLogo(); 
    if (Yii::$app->request->isPost) { 

     $upload_logo->imageFile = UploadedFile::getInstance($upload_logo, 'imageFile'); 

     if ($upload_logo->imageFile) { //1st part ($logo_data->imageFile) - whether user have uploaded logo 
      $logo_file_name = md5($user_data->id); 
      $is_uploaded = $upload_logo->upload($logo_file_name); 
      if ($is_uploaded) { //this cond is needed, cause validation for image fails (?) 
       //create record in 'logo_data' tbl, deleting previous 
       if ($logo_name) { 
        $logo->delete(); 
       } else { //if upload logo first time, set val to $logo_name. Otherwise NULL val will pass to 'profile' view, and user wont see his new logo at once 
        $logo_name = $logo_file_name.'.'.$upload_logo->imageFile->extension; 
       } 
       $logo_data = new LogoData; 
       $logo_data->user_id = \Yii::$app->user->id; 
       $logo_data->logo_name = $logo_name; 
       $logo_data->save(); 
      } 
     } 
    } 

    if (isset($_POST['CompanyProfiles'])){ 

     $company_profile_data->firm_data = $_POST['CompanyProfiles']['firm_data']; 
     $company_profile_data->company_name = $_POST['CompanyProfiles']['company_name']; 
     $company_profile_data->regon = $_POST['CompanyProfiles']['regon']; 
     $company_profile_data->pesel = $_POST['CompanyProfiles']['pesel']; 
     $company_profile_data->postal_code = $_POST['CompanyProfiles']['postal_code']; 
     $company_profile_data->nip = $_POST['CompanyProfiles']['nip']; 
     $company_profile_data->country = $_POST['CompanyProfiles']['country']; 
     $company_profile_data->city = $_POST['CompanyProfiles']['city']; 
     $company_profile_data->address = $_POST['CompanyProfiles']['address']; 
     $company_profile_data->telephone_num = $_POST['CompanyProfiles']['telephone_num']; 
     $company_profile_data->email = $_POST['CompanyProfiles']['email'];   
     $company_profile_data->update(); 
    } 

    if (isset($_POST['personal-data-button'])) { 
     $user_data->username = $_POST['Users']['username']; 
     $user_data->password_repeat = $user_data->password = md5($_POST['Users']['password']); 
     $user_data->update(); 
    } 
    return $this->render('profile', ['user_data' => $user_data, 'company_profile_data' => $company_profile_data, 'upload_logo' => $upload_logo, 'logo_name' => $logo_name]); 
} 
+0

您正在使用加載函數?我想只有在提交表單後調用該加載。 –

+0

不,我不使用它。我這樣做(正如我上面寫的):'$ company_profile_data-> postal_code = $ _POST ['CompanyProfiles'] ['postal_code']'。所以,我不使用大規模的任務。你會發現,其他屬性在提交前正確驗證(沒有任何'load()')。但不是在獨立驗證的情況下。 –

+0

我在控制器的問題。你可以用控制器代碼更新文章? –

回答

2

我的不準確性在clientValidateAttribute()方法中。取而代之的$model->$attribute在代碼片段:

if (!/^[0-9]{2}-[0-9]{3}$/.test("{$model->$attribute}")) { 

...我不得不使用預定義的JS-VAR value,會導致輸入的數值變化這個變種的變化。所以,我的新代碼是:

public function clientValidateAttribute($model, $attribute, $view) { 
    return <<<JS 
if (!/^[0-9]{2}-[0-9]{3}$/.test(value)) { 
    messages.push("Wrong postal code format."); 
} 
JS; 
} 
0

型號不加載規則和行爲,直到not called any function從模型。當您致電$company_profile_data->update();模型調用updatevalidate功能。

嘗試添加$company_profile_data = CompanyProfiles::find()此代碼後:

$company_profile_data->validate(); 

或者只是使用load功能。我認爲這會有所幫助。

+1

兩種情況都沒有成功。但是,我認爲,我已經解決了這個問題。感謝參與。 :) –