【问题标题】:Yii Model has an invalid validation rule. Won't insert to multiple modelsYii 模型的验证规则无效。不会插入多个模型
【发布时间】:2014-01-30 03:14:23
【问题描述】:

我正在尝试创建一个用户,并且可以插入到我的 SystemUsers 模型和 Party 模型中,但是现在当我添加代码以尝试向我的 Persons 模型插入时,我得到错误 Persons has an invalid validation规则。该规则必须指定要验证的属性和验证器名称。

我的控制器代码有什么问题吗?

 public function actionCreate()
    {
        $model = new SystemUsers;
        $modelParties = new Parties;
        $modelPersons = new Persons;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($invoice);

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

        $model->attributes = $_POST['SystemUsers']; 
        Yii::app()->db->createCommand("insert into parties (party_type_id) values ('1')")->execute();           
        $id = Yii::app()->db->lastInsertId; //The id from parties is a fk of system users

        $model->password = md5($model->password);//this works
        $model->party_id = $id; //this works
        $modelPersons->party_id = $id; //I believe errors start to occur startin here
        $modelPersons->email = $model->username;
        $modelPersons->save();



        if($model->save())
            echo "<script>alert('User Created');</script>";

//      }
        else $this->redirect(array('views22','id'=>$model->id));
 }

$this->render('create',array(
  'parties'=>$modelParties,
  'model'=>$model,
));
 }

这些是我的 Persons 模型的规则::

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('party_id, email, company_name, last_name, middle_name, gender, dob_year, dob_month, dob_day, nickname, language, address, country, location, moderator, from_date_location, to_date_location'),
        array('dob_year, dob_month, dob_day, language, country, location', 'numerical', 'integerOnly'=>true),
        array('party_id', 'length', 'max'=>20),
        array('email', 'length', 'max'=>100),
        array('company_name, last_name, middle_name, nickname', 'length', 'max'=>255),
        array('gender', 'length', 'max'=>6),
        array('sub_admin, moderator', 'length', 'max'=>1),
        array('image', 'safe'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('party_id, email, company_name, last_name, middle_name, gender, image, dob_year, dob_month, dob_day, nickname, language, address, country, location, sub_admin, moderator, from_date_location, to_date_location', 'safe', 'on'=>'search'),
    );
}

【问题讨论】:

    标签: php mysql sql yii


    【解决方案1】:

    在规则函数中,您没有提到第一个数组的规则。您只是在没有规则的情况下提及字段名称。

    你必须提到required字段验证规则如下

     array('party_id, email, company_name, last_name, middle_name, gender, dob_year, dob_month, dob_day, nickname, language, address, country, location, moderator, from_date_location, to_date_location','required'),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 2014-06-26
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多