Custom validator

hi sorry for english . I want make a custom validator. This is the fields of my model:




* @property integer $idEventi

 * @property string $nomeEvento

 * @property string $dataInizio

 * @property string $dataFine

 * @property string $dataPrevista

 * @property string $numPartecipanti

 * @property string $insegnante

 * @property string $svolgimento



i want make a custom validator for the 3 fields data

dataInizio = start date

dataFine=end date

dataPrevista=expexted date

If i write in the expected date i want an error if i insert a start date or end date .

im trying to make my custom validator , first i add 3 variables in my model :




 public $dataInizio;

 public $dataPrevista;

 public $dataFine;




then i change the function rule :




public function rules()

    {

        return [

            [[ 'nomeEvento', 'svolgimento'], 'required'],

            [['svolgimento'], 'string'],

            [['nomeEvento', 'dataInizio', 'dataFine', 'dataPrevista', 'numPartecipanti', 'insegnante'], 'string', 'max' => 45],

            [['dataInizio', 'dataFine', 'dataPrevista'],'controlloDate']

        ];

    }




and create a function controlloDate :




public function controlloDate($attribute,$params){

        if (isset($this->dataPrevista)and( isset($this->$dataInizio)or isset($this->dataFine))){

            $this->addError($attribute, 'Se riempi il campo data prevista non puoi riempire i campi data fine e data inizio!');

        }

    }




But don’t work .Can u help me ? tanks

I think your controlloDate function has to return true or false as well as call addError

I change the function controlloDate as this :




 public function controlloDate($attribute,$params){

        if (isset($this->dataPrevista)and !isset($this->$dataInizio) and !isset($this->dataFine)){

            return true;

            

        }

        else {

            $this->addError($attribute, 'Se riempi il campo data prevista non puoi riempire i campi data fine e data inizio!');

            return false;

        }

    }




but dont work…

Does it pass or fail validation?

To help debug, you could try adding , ‘skipOnEmpty’ => false, ‘skipOnError’ => false

ie




[['dataInizio', 'dataFine', 'dataPrevista'],'controlloDate', 'skipOnEmpty' => false, 'skipOnError' => false]



i insert your code , if i insert the 3 date and click on submit i have this error:


Undefined variable: dataInizio

this is my eventi model :





<?php


namespace common\models;


use Yii;


/**

 * This is the model class for table "Eventi".

 *

 * @property integer $idEventi

 * @property string $nomeEvento

 * @property string $dataInizio

 * @property string $dataFine

 * @property string $dataPrevista

 * @property string $numPartecipanti

 * @property string $insegnante

 * @property string $svolgimento

 */

class Eventi extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */


    public $dataInizio;

    public $dataPrevista;

    public $dataFine;


    public static function tableName()

    {

        return 'Eventi';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [[ 'nomeEvento', 'svolgimento'], 'required'],

            [['svolgimento'], 'string'],

            [['nomeEvento', 'dataInizio', 'dataFine', 'dataPrevista', 'numPartecipanti', 'insegnante'], 'string', 'max' => 45],

            [['dataInizio', 'dataFine', 'dataPrevista'],'controlloDate', 'skipOnEmpty' => false, 'skipOnError' => false]

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'idEventi' => 'Id Eventi',

            'nomeEvento' => 'Nome Evento',

            'dataInizio' => 'Data Inizio',

            'dataFine' => 'Data Fine',

            'dataPrevista' => 'Data Prevista',

            'numPartecipanti' => 'Num Partecipanti',

            'insegnante' => 'Insegnante',

            'svolgimento' => 'Svolgimento',

        ];

    }


    public function controlloDate($attribute,$params){

        if (isset($this->dataPrevista)and !isset($this->$dataInizio) and !isset($this->dataFine)){

            return true;

            

        }

        else {

            $this->addError($attribute, 'Se riempi il campo data prevista non puoi riempire i campi data fine e data inizio!');

            return false;

        }

    }

}



i dont understand , how i can set the 3 date as the values fields ?

It works the error is :


$this->$dataInizio

but it work only on submit the other field validate in real time (tinking ajax)

Don’t work , everytime an error , if i insert only dataPrevista too.

From looking at your code, that is expected behaviour.

However, to make this easier i would recommend you use the "when" option for rules. Here is an example also with 3 fields:




public function rules() 

.

.

.

.

['exactFrequency',

	'required',

	'when' => function($model) {

		if (array_key_exists('numPurchases', $this->selectedFields) === true

		&& strpos($model->frequencyOperator, 'between') === false) {

			return true;

		} else {

			return false;

		}

	},

	'skipOnEmpty' => false,

	'skipOnError' => false

],

[['minFrequency', 'maxFrequency'],

	'required',

	'when' => function($model) {

		if (array_key_exists('numPurchases', $this->selectedFields) === true

		&& strpos($model->frequencyOperator, 'between') !== false) {

			return true;

		} else {

			return false;

		}

	},

	'skipOnEmpty' => false,

	'skipOnError' => false

],

.

.

.






Tank but i want create my custom validator , my answer is :

$this->dataPrevista is the value of dataPrevista field?

$this->dataInizio is the value of dataInizio field?

$this->dataFine is the value of dataFine field?

How i must click on submimt to control and not client side control?

I solved in part by changing the function controlloDate:




public function controlloDate($attribute,$params, $enableClientValidation = true){

        

        if ($this->dataPrevista!=''and $this->dataInizio=='' and $this->dataFine==''){

            return true;

            

        }

        elseif($this->dataPrevista==''and $this->dataInizio!='' and $this->dataFine!=''){

            return true;

        }

        elseif ($this->dataPrevista==''and ($this->dataInizio=='' or $this->dataFine=='')) {

             $this->addError($attribute, 'Se riempi il campo data inizio riempi anche il campo data fine!');

            return false;

        }

        elseif ($this->dataPrevista!=''and ($this->dataInizio!='' or $this->dataFine!='')) {

             $this->addError($attribute, 'Se riempi il campo data prevista non puoi riempire i campi data fine e data inizio!');

            return false;

        }

        elseif ($this->dataPrevista==''and $this->dataInizio=='' and $this->dataFine=='') {

             $this->addError($attribute, 'Non puoi lasciare tutti i campi vuoti!');

             return false;

        }

        else{

             $this->addError($attribute, 'Errore Generico!');

             return false;

        }


    }




-Works If I put a value in dataPrevista and dataInizio or DataFine

-Works If I don’t put a value in dataPrevista and put only one value in dataInizio or dataFine.

-Do Not work if I leave everything blank.

-Do Not work client-side …