Reputation: 107
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 ?
Upvotes: 1
Views: 1433
Reputation: 5032
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"});
}
}
);
Upvotes: 2
Reputation: 498
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
Upvotes: 0