ptoledo
(Informatica)
January 23, 2015, 12:20pm
1
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.
Pasman
(Pasman P)
January 23, 2015, 1:55pm
2
Do you defined ‘whenClient’ ?
flarpy
(Peter)
January 23, 2015, 1:57pm
3
ensure you have skipOnEmpty and skipOnError set correctly eg
'datePurchasedValidator',
'when' => function() {return true;},
'skipOnEmpty' => false,
'skipOnError' => false
ptoledo
(Informatica)
January 23, 2015, 7:59pm
4
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
flarpy
(Peter)
January 24, 2015, 7:16am
5
Please post all your rules here
ptoledo
(Informatica)
January 27, 2015, 7:00pm
6
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']);
}
}
Pasman
(Pasman P)
January 27, 2015, 8:12pm
7
ptoledo:
This is my model…
['precio','required','when' => function($model) {return true;},'skipOnEmpty' => false,'skipOnError' => false]
‘when’ callback returns true => validator works always.
ptoledo
(Informatica)
January 28, 2015, 6:38pm
8
I understand that but if i change to false, yii still seeing that the attribute is required