Conditional Validation

Hi everyone. I need helo with conditional Validation in Yii 2.0.

In my model, I have one attribute that it’s requiered if other attribute is selected but when i use the option ‘when’ (read it in the documentation) doesn’t work, it’s seem like this option no exist and the attribute is always requiered.

Do you defined ‘whenClient’ ?

ensure you have skipOnEmpty and skipOnError set correctly eg




				'datePurchasedValidator',

				'when' => function() {return  true;},

				'skipOnEmpty' => false,

				'skipOnError' => false



Thanks for your answers but I still can not make it work.

i put the condition that flarp said but the conditional attribute of the model still be required and I don’t know why

Please post all your rules here

This is my model…




namespace app\modules\configuracion\models;


use Yii;


/**

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

 *

 * @property integer $idproducto

 * @property string $descripcion

 * @property integer $categoria

 * @property string $codigo

 * @property string $precio

 * @property integer $suspendido

 *

 * @property Detalle[] $detalles

 * @property Categoria $categoria0

 */

class Producto extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'producto';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['descripcion','categoria'], 'required'],

            [['categoria', 'suspendido'], 'integer'],

            [['precio'], 'number'],

            [['descripcion'], 'string', 'max' => 70],

            [['codigo'], 'string', 'max' => 30],

        	['precio','required','when' => function($model) {return  true;},'skipOnEmpty' => false,'skipOnError' => false]

        ];

    }

    

    


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'idproducto' => 'Idproducto',

            'descripcion' => 'Descripcion',

            'categoria' => 'Categoria',

            'codigo' => 'Codigo/PLU',

            'precio' => 'Precio',

            'suspendido' => 'Suspendido',

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getDetalles()

    {

        return $this->hasMany(Detalle::className(), ['producto' => 'idproducto']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getCategoria0()

    {

        return $this->hasOne(Categoria::className(), ['idcategoria' => 'categoria']);

    }

}




‘when’ callback returns true => validator works always.

I understand that but if i change to false, yii still seeing that the attribute is required