Toma Tomov
Toma Tomov

Reputation: 1664

Custom ajax validation for text length doesn't work yii2

I am trying to check the length of the text entered but without success. It is working with the required rule because i get the field is empty error but not with mine validation. My custom rule works only on form submit. Also tried to enable ajax validation of the form but again nothing.

public function rules()
    {
        return [
            [['author_id', 'title', 'review'], 'required'],
            [['author_id'], 'integer'],
            [['review'], 'string'],
            [['review'], function($attribute, $params){
                if(strlen($this->$attribute) < 10){
                    $this->addError($attribute, 'The review is too short! Minimum 10 symbols!');
                }
            }],
            [['review'], 'trim'],
            [['dt'], 'safe'],
            [['title'], 'string', 'max' => 255],
            [['author_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['author_id' => 'id']],
            [['post_id'], 'exist', 'skipOnError' => true, 'targetClass' => News::className(), 'targetAttribute' => ['post_id' => 'id']],
        ];
    }

My form:

<?php $form=\yii\bootstrap\ActiveForm::begin([
                                'method' => 'post',
                                'options' => [
                                    'id' => 'textarea_' . $model->id . '',
                                    'class' => "textarea_review"
                                ],

                            ]) ?>

                            <input type="hidden" name="flag" value="1"/>
                            <input type="hidden" name="model_id" value="<?= $model->id ?>"/>
                            <?= $form->field($model, 'review')->textarea(['id'=>'update_text_'.$model->id.''])->label(false) ?>
                            <?= $form->field($model, 'csrf_token')->hiddenInput(['value' => $session['token']])->label(false) ?>

                            <?= Html::button('Изпрати', ['onclick'=>'editComment('.$model->id.')', 'class'=>'btn btn-primary send-button']) ?>
                            <?php \yii\bootstrap\ActiveForm::end() ?>

Thank you in advance!

Upvotes: 1

Views: 429

Answers (2)

csminb
csminb

Reputation: 2382

perhaps skipping the inline validator and defining the string rule as follows is the best solution for you:

[['review'], 'string', 'max' => 10, 'message' =>  'The review is too short! Minimum 10 symbols!']

If you absolutelty need a custom validator, the second best option is to use ajax validation.

If neither of the above suit you, the you won't get away with just writing php validation rules. you need to provide client side script to implement the same validation logic in browser.

Either define a custom validator class and override clientValidateAttribute() or you may specify clientValidate property to the inline validator you're using in your custom rule.
Make sure you follow the distinction between yii\validators\InlineValidatorand yii\validators\Validator when reading trough the docs

Upvotes: 1

Yupik
Yupik

Reputation: 5032

For client-side validation you have to set whenClient property aswell, where you put javascript validation.

Here docs: Client Side Validation

Upvotes: 1

Related Questions