[[+content_image]]
I
I
iceman21122014-09-14 16:02:22
Yii
iceman2112, 2014-09-14 16:02:22

Why doesn't validator work in yii2?

There is a model method rules()

public function rules()
    {
       ....

            [['url'], '\backend\components\BannerValidator'],
           ....
    }

According to \backend\components\BannerValidator:
namespace backend\components;

use Yii;
use yii\validators\Validator;

class BannerValidator extends Validator
{
    /**
     * @inheritdoc
     */
    public function validateAttribute($object, $attribute)
    {
        $this->addError($object, $attribute, 'errrerer');
    }
}

In view:
<?= $form->field($model, 'url')->textInput(['maxlength' => 255]) ?>


Why doesn't the validator fire when I click submit?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
L
LAV45, 2014-09-15
@LAV45

You also need to describe the clientValidateAttribute method, as an example you can look here =>
https://github.com/yiisoft/yii2/blob/master/framew...
OR
The problem is in the controller, here is a working example =>

public function actionCreate()
    {
        $model = new Menu();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question