【发布时间】:2016-02-18 16:43:13
【问题描述】:
Yii2 唯一验证不适用于员工 ID 和公司 ID 的组合 下面是我的模型代码。
public function rules()
{
return [
[['company_id', 'role_id'], 'required'],
[['company_id', 'role_id', 'status'], 'integer'],
[['employee_id'], 'string', 'max' => 15],
[['report_to'], 'string', 'max' => 16],
[['id',],'safe'],
['employee_id', 'unique', 'targetAttribute' => ['company_id', 'employee_id'], 'message' => 'The combination of Company ID and Employee ID has already been taken.']
// ['employee_id', 'unique', 'targetAttribute' => ['company_id'], 'message' => 'The combination of Company ID and Employee ID has already been taken.']
];
}'
这是我的控制器代码
$model = new Employee();
//$profile = new Profile();
// $profile->scenario = 'emp_bulk_uplscenariooad';
if($model->load(Yii::$app->request->post())) {
$model->company_id = $userModel->company_id;
$model->employee_id = Yii::$app->request->post()['Employee']['employee_id'];
$model->role_id = Yii::$app->request->post()['Employee']['role_id'];
$model->report_to = Yii::$app->request->post()['Employee']['report_to'];
// print_r(!$model->validate());die();
if($model->save(false)){
}
*************************************************************************
please help me with this.thanks in advance
【问题讨论】:
标签: validation yii2 unique