未调用Yii2自定义验证功能

时间:2016-02-28 14:57:56

标签: php validation yii2 yii2-advanced-app

我需要制作自定义验证规则,以警告用户电子邮件或phone_number字段是必需的,但未调用自定义验证功能

我的代码到目前为止: 模型

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,
            ]);
     }
 }

任何帮助?

2 个答案:

答案 0 :(得分:3)

您需要为其验证Validator的skipOnEmpty属性..更多信息Read

 Update your code as 

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

答案 1 :(得分:0)

我认为您使用$ attribute而不是$ attribute_name

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;
}
来自yii2 doc

  

addError()public method添加有关指定属性的错误   到模型对象。

     

这是一个执行消息选择的辅助方法   国际化。 public void addError($ model,$ attribute,   $ message,$ 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,
        ]);
    }
}