方案已正确执行,但无法进行验证


scenario is taken correctly but does not work validation

嗨,我将用户数据带到两个模型 如果用户单击复选框(公司(,它会向他显示需要完成的其他数据。如果 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->情景(; 退出((;

这表明一切都很好,因为当复选框关闭时,vardump spets:string (7( "notFirm",当他在吐槽时:字符串 (7( "setFirm"。我不知道故障在哪里,但每次验证都是安全的,如果复选框是来自规则(地址、电话(的数据,那应该可以工作。有人看到我的坏处并可以帮助我吗?

我希望

你已经找到了一个答案,但如果你还没有,这里有一个。您将在验证数据设置方案。必须在运行验证之前设置方案,以便使用不同的验证规则。

在您的代码中,您有

   if ($userDate->IsCompany()) {
        $userDate->scenario = 'setFirm';
   } else {
       $userDate->scenario = 'notFirm';
       $userDate->clearData();
   }

但是首先,如果在您的代码中您已经验证了

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