2016-02-28 56 views
1

我需要讓我的自定義的驗證規則,以警告用戶電子郵件或phone_number字段是必須的,但自定義驗證函數不叫Yii2自定義驗證函數

到目前爲止我的代碼: 型號

namespace backend\models; 
use Yii; 


class Advertisement extends \yii\db\ActiveRecord 
{ 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'advertisement'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['position', 'location', 'status'], 'required'], 
      [['description'], 'string'], 
      [[ 'phone_number', 'company'], 'integer'], 
      [['created_at', 'updated_at','date_added'], 'safe'], 
      [['position', 'email', 'location', ], 'string', 'max' => 255], 
      [['phone_number','email'], 'myRule'], 
     ]; 
    } 


    public function myRule($attribute, $params) 
    { 

     if (empty($this->email) 
      && empty($this->phone_number) 
     ) { 
      $this->addError($attribute, Yii::t('ap', 'either Phone or Email required')); 

      return false; 
     } 

     return true; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'id' => Yii::t('app', 'ID'), 
      'position' => Yii::t('app', 'Position'), 
      'description' => Yii::t('app', 'Description'), 
      'email' => Yii::t('app', 'Email'), 
      'phone_number' => Yii::t('app', 'Phone Number'), 
      'created_at' => Yii::t('app', 'Created At'), 
      'updated_at' => Yii::t('app', 'Updated At'), 
      'company' => Yii::t('app', 'Company'), 
      'status' => Yii::t('app', 'Status'), 
     ]; 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getCompany0() 
    { 
     return $this->hasOne(User::className(), ['id' => 'company']); 
    } 


} 

控制器動作

public function actionCreate() 
{ 
    $model = new Advertisement(); 

    if ($model->load(Yii::$app->request->post())) { 
     $model->company = Yii::$app->user->identity->id; 
     $model->created_at = date("Y-m-d H:i:s"); 
     $model->date_added = date("Y-m-d H:i:s"); 

      $model->save(); 
      return $this->redirect(['view', 'id' => $model->id]); 
    } else { 
      return $this->render('create', [ 
       'model' => $model, 
      ]); 
    } 
} 

任何幫助嗎?

回答

0

我想你hsold使用$屬性,並從yii2文檔沒有屬性名稱

public function myRule($attribute_name, $params) 
{ 

    if (empty($this->email) 
     && empty($this->phone_number) 
    ) { 
     $this->addError($attribute, Yii::t('app', 'either Phone or Email required')); 

     return false; 
    } 

    return true; 
} 

$

addError()公共方法添加關於指定屬性 到模型對象的錯誤。

這是執行消息選擇和 國際化的幫助方法。公共無效addError($模式,$屬性, $消息,$ PARAMS = [])

試試這個動作創建

public function actionCreate() 
{ 
    $model = new Advertisement(); 

    if ($model->load(Yii::$app->request->post())) { 
     if ($model->validate()) { 


      $model->company = Yii::$app->user->identity->id; 
      $model->created_at = date("Y-m-d H:i:s"); 
      $model->date_added = date("Y-m-d H:i:s"); 

      $model->save(); 
      return $this->redirect(['view', 'id' => $model->id]); 
     } 
     else { 
     $errors = $model->errors; 
     return $this->render('create', [ 
      'model' => $model, 
     ]); 
     } 
    } else { 
     return $this->render('create', [ 
      'model' => $model, 
     ]); 
    } 
} 
+0

我編輯的代碼仍然沒有工作 –

+0

兩件事情1)也顯示所有的型號代碼..?也顯示動作(控制器)代碼 – scaisEdge

+0

我更新了我的問題檢查! –

1

你需要禁用驗證的skipOnEmpty屬性它。對於更多信息Read

Update your code as 

    [['phone_number','email'], 'myRule' ,'skipOnEmpty' => false],