I am trying to validate user input.
$params = Yii::$app->getRequest()->getBodyParams();
$model = new DynamicModel($params);
$model->addRule(['userId', 'category', 'type'], 'required');
$model->addRule('userId', 'integer');
$model->addRule('category',
function ($attribute, $params, $validator) use ($model) {
var_dump($params); exit;
});
$model->validate();
return $model;
How can I access the value of category
parameter so that I can apply my validation logic. Currently its getting null
You need something like this:
$model->addRule(
'category',
function ($attribute, $params, $validator) use ($model) {
if (empty($model->$attribute)) {
$model->addError($attribute, 'Error message');
}
}
);
Inline validators are explained in the guide.