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 ?

Amir Khoshhal
  • 107
  • 2
  • 11

2 Answers2

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"});
        }
    }
);
Yupik
  • 4,932
  • 1
  • 12
  • 26
  • 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() ?> – Amir Khoshhal Jun 03 '17 at 04:44
  • Its not working on client validation. I need client validation. – Amir Khoshhal Jun 06 '17 at 04:22
  • So it's working, but not like u want. Next time specify problem instead of talking "not working". Answer updated with client side validation. – Yupik Jun 06 '17 at 05:43
  • I used your client validation code but I get this error: yii.activeForm.js:242 Uncaught TypeError: Cannot read property 'attributes' of undefined – Amir Khoshhal Jun 12 '17 at 07:45
  • 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"}); } }); – Amir Khoshhal Jun 12 '17 at 07:50
  • What is `$j` in view code? There's missing `foreach` or something? – Yupik Jun 12 '17 at 08:42
  • $j is for iterator loop and this is not necessary and i removed from my view code. my problem is in javascript code and my error is: yii.activeForm.js:242 Uncaught TypeError: Cannot read property 'attributes' of undefined at jQuery.fn.init.add (yii.activeForm.js:242) at jQuery.fn.init.$.fn.yiiActiveForm (yii.activeForm.js:16) – Amir Khoshhal Jun 12 '17 at 08:49
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

Timur
  • 488
  • 4
  • 14