Informer
Informer

Reputation: 205

scenario is taken correctly but does not work validation

Hi I take user data to two models If the user clicks the checkbox (company) it show him the additional data that needs to complete. I needs to work on scenario if checbox = 1 the data fields of the form must be passed. It is my action from the controller:

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

An my model:

<?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;
    }
    }

I read the documentation but it does not help me. In the create action I created

var_dump($userDate->scenario); exit();

which indicates that there is everything okay because when checkobox is off vardump spits: string (7) "notFirm" and when he's on spits: string (7) "setFirm." I do not know where the fault but each time validation is safe, that should work that if checkbox is on data from rules(addres, phone) should be required. Anyone see my bad and can help me?

Upvotes: 1

Views: 74

Answers (1)

Pablo Flores
Pablo Flores

Reputation: 1400

I hope you have found an answer, but in case you haven't here's one. You're setting the scenario after you validate the data. Scenarios must be set before you have run the validation in order to use different validation rules.

In your code you have

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

But in the first if in your code you have already validated

if (($userDate->load(Yii::$app->request->post()) && $userDate->validate() ...

In order to use a scenario I suggest the following:

$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

Upvotes: 0

Related Questions