您好我将用户数据转移到两个模型如果用户单击复选框(公司),它会向他显示需要完成的其他数据。如果checbox = 1,我需要处理场景,必须传递表单的数据字段。这是我从控制器那里采取的行动:
public function actionCreate() {
$model = new UrUserForm();
$userDate = new UserDataForm();
$model->scenario = 'create';
if (($userDate->load(Yii::$app->request->post()) && $userDate->validate() && $model->load(Yii::$app->request->post()) && $model->validate()) || $model->load(Yii::$app->request->post()) && $model->validate()) {
if ($userDate->IsCompany()) {
$userDate->scenario = 'setFirm';
} else {
$userDate->scenario = 'notFirm';
$userDate->clearData();
}
var_dump($userDate->scenario);
exit();
$userDate->saveOptionalData();
$model->RoyalUserData=$userDate->data['Id'];
$model->saveUser();
Yii::$app->session->setFlash('success', 'Użytkownik został dodany');
return $this->redirect(['index']);
} else {
return $this->render('create', [
'model' => $model,
'userDate' => $userDate
]);
}
}
我的模特:
<?php
namespace backend\modules\users\models;
use common\models\UserData;
use frontend\modules\settings\models\Profile;
use yii\base\Model;
use Yii;
class UserDataForm extends Model
{
public $Address;
public $NIP;
public $CompanyName;
public $Website;
public $Phone;
public $IsCompany;
public $IsPhoneConfirmed;
public $CreatedAt;
public $UpdateAt;
public $Rel_State;
public $Rel_Currency;
public $IsDeleted;
public $data;
public function rules()
{
return [
[['Address', 'Phone', 'Rel_State', 'Rel_Currency','IsCompany'], 'safe', 'on' => 'notFirm'],
[['Address', 'Phone', 'Rel_State', 'Rel_Currency','IsCompany'], 'required', 'on' => 'setFirm'],
[['NIP','IsCompany', 'Phone', 'IsPhoneConfirmed', 'CreatedAt', 'UpdateAt', 'Rel_State', 'Rel_Currency', 'IsDeleted'], 'integer'],
[['Address', 'CompanyName', 'Website'], 'string', 'max' => 45],
[['Phone'], 'common\components\validators\PhoneValidator'],
[['NIP'], 'common\components\validators\NipValidator'],
['IsCompany', 'safe']
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['setFirm'] = ['Address', 'Phone', 'Rel_State', 'Rel_Currency','IsCompany'];
$scenarios['notFirm'] = ['Address', 'Phone', 'Rel_State', 'Rel_Currency','IsCompany'];
return $scenarios;
}
public function saveOptionalData() {
$model = new UserData();
$model->Address=$this->Address;
$model->Phone=$this->Phone;
$model->Rel_State=$this->Rel_State;
$model->Rel_Currency= $this->Rel_Currency;
$model->NIP=$this->NIP;
$model->IsCompany = $this->IsCompany;
$model->IsPhoneConfirmed = $this->IsPhoneConfirmed;
$model->CompanyName = $this->CompanyName;
$model->Website = $this->Website;
$this->data=$model;
if ($model->validate() && $model->save()) {
return $model;
}
return false;
}
public function clearData() {
$this->Address = NULL;
$this->Phone = NULL;
$this->Rel_State = NULL;
$this->Rel_Currency = NULL;
$this->NIP = NULL;
$this->IsCompany = NULL;
$this->IsPhoneConfirmed = NULL;
$this->CompanyName = NULL;
$this->Website = NULL;
}
public function IsCompany() {
if ($this->IsCompany == 1) {
return true;
}
return false;
}
}
我阅读了文档,但它对我没有帮助。在我创建的创建动作中
的var_dump($ userDate-&GT;场景); 出口();
表示一切正常,因为当checkobox关闭时vardump spits:string(7)“notFirm”,当他在spits上时:string(7)“setFirm”。我不知道故障在哪里,但每次验证都是安全的,如果复选框是来自规则(地址,电话)的数据应该是必需的。有人看到我的坏,可以帮助我吗?
答案 0 :(得分:0)
我希望你找到答案,但万一你没有答案。您在验证数据后设置方案。在运行验证之前必须设置方案,以便使用不同的验证规则。
在您的代码中
if ($userDate->IsCompany()) {
$userDate->scenario = 'setFirm';
} else {
$userDate->scenario = 'notFirm';
$userDate->clearData();
}
但是在您的代码中的第一个if中,您已经验证了
if (($userDate->load(Yii::$app->request->post()) && $userDate->validate() ...
为了使用场景,我建议如下:
$userDate->load(Yii::$app->request->post();//load data into model
if ($userDate->IsCompany()) {//check if company was set and is equal to 1
$userDate->scenario = 'setFirm';
} else {
$userDate->scenario = 'notFirm';
}
if($userDate->validate()...)//Validation code according to the scenario