1

I have many inputs without connect any models in my form like this:

my view is this and I put this to show to other user to detect what is my problem

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\Tour */

$this->title = 'ثبت تور';

$this->registerJsFile('@web/storage/js/agency/tour.js', ['depends' => [
   \yii\web\JqueryAsset::className(), 
   \yii\validators\ValidationAsset::className(), 
   \yii\widgets\ActiveFormAsset::className()],
]);    

<?php $form = ActiveForm::begin([
    'id' => 'jqTourStep1Form'
]); ?>

<div class="col-xs-6 pull-right padding0">
    <?= $form->field($model, 'title_fa')->textInput() ?>
</div>

<div class="form-group col-xs-4 padding0 pull-right idTesttt">
    <label class="control-label">Start Date</label>
    <input type="text" name="startDate[]" id="startDateTest" class="form-control">
    <span class="help-block"></span>
</div>

<?= Html::submitButton('Next', ['class' => 'btn btn-success']) ?>

<?php ActiveForm::end(); ?>

tour.js :

/* Validation */
$('#jqTourStep1Form').yiiActiveForm('add', {
    id: 'startDateTest',
    name: 'startDate',
    container: '.idTesttt',
    input: '#startDateTest',
    error: '.help-block',
    validate:  function (attribute, value, messages, deferred, $form) {
        yii.validation.required(value, messages, {message: "Validation Message Here"});
    }
});

and I want to Client validate this inputs in Yii 2.

how can I do this ?

2 Answers 2

2

You should use each core validator: Each Validator. But you should use ActiveForm to generate form.

public function rules()
    {
        return [
            // checks if every category ID is an integer
            ['categoryIDs', 'each', 'rule' => ['integer']],
        ]
    }

Add JS code to client side validation:

jQuery('#form-id').yiiActiveForm("add", {
        "id":        "input-id",
        "name":      "input-name",
        "container": "#container-id or unique .container-class of this input",
        "input":     "#input-id or unique .input-class",
        "validate":  function (attribute, value, messages, deferred, $form) {
            yii.validation.required(value, messages, {"message": "Validation Message Here"});
        }
    }
);
Sign up to request clarification or add additional context in comments.

7 Comments

I set a property in my model and used your code and used below code in my view, but it's not working yet, please help me to fix this problem. <?= $form->field($model, 'startDates[]')->textInput() ?>
Its not working on client validation. I need client validation.
So it's working, but not like u want. Next time specify problem instead of talking "not working". Answer updated with client side validation.
I used your client validation code but I get this error: yii.activeForm.js:242 Uncaught TypeError: Cannot read property 'attributes' of undefined
yes, my code is: /* Validation */ $('#jqTourStep1Form').yiiActiveForm('add', { id: 'startDateTest', name: 'startDate', container: '.idTesttt', input: '#startDateTest', error: '.help-block', validate: function (attribute, value, messages, deferred, $form) { yii.validation.required(value, messages, {message: "Validation Message Here"}); } });
|
0

You can create object of specific validator class without using models:

$validator = new \yii\validators\DateValidator();

and validate any value

$error = null;
$validator->validate($startDate, $error);

method validate() will return boolean and add to variable $error message about error. You can read and choose specific validator on this page

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.