Hi everyone!
I have 2 questions:
- I have next form:
<?php $form = ActiveForm::begin(['id' => 'travel',
'options' => ['class'=>'travel_form'],
'validateOnBlur' => false,
]); ?>
<div class="col-lg-12">
<table>
<thead>
<tr>
<td>Document</td>
<td>Number</td>
<td>Place of issue</td>
<td>Date of issue</td>
<td>Date of expiry</td>
</tr>
</thead>
<tr>
<td class="f-tit lang"><span class="req">*</span> Seaman’s Book / ID
<?=Html::activeHiddenInput($model, "doc_id[]", ['value'=>'1']);?>
</td>
<td>
<?= $form->field($model,'number[]')->textInput(['placeholder'=>'Enter number'])->label(false); ?>
</td>
<td>
<?= $form->field($model, 'country_id[]', ['options'=>['class'=>'half']])->dropDownList(ArrayHelper::map(Countries::find()->all(), 'country_id', 'name'), ['prompt'=>'Select country'])->label(false);?>
</td>
<td>
<?= $form->field($model, 'issue[]')->widget(
DatePicker::className(), [
'clientOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy'
]
])->label(false) ?>
</td>
<td>
<?= $form->field($model, 'expiry[]')->widget(
DatePicker::className(), [
'clientOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy'
]
])->label(false) ?>
</td>
</tr>
<tr>
<td class="f-tit lang"><span class="req">*</span> International passport
<?=Html::activeHiddenInput($model, "doc_id[]", ['value'=>'2']);?>
</td>
<td>
<?= $form->field($model,'number[]')->textInput(['placeholder'=>'Enter number'])->label(false); ?>
</td>
<td>
<?= $form->field($model, 'country_id[]', ['options'=>['class'=>'half']])->dropDownList(ArrayHelper::map(Countries::find()->all(), 'country_id', 'name'), ['prompt'=>'Select country'])->label(false);?>
</td>
<td>
<?= $form->field($model, 'issue[]')->widget(
DatePicker::className(), [
'clientOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy'
]
])->label(false) ?>
</td>
<td>
<?= $form->field($model, 'expiry[]')->widget(
DatePicker::className(), [
'clientOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy'
]
])->label(false) ?>
</td>
</tr>
<tr>
<td class="f-tit lang">National passport
<?=Html::activeHiddenInput($model, "doc_id[]", ['value'=>'3']);?>
</td>
<td>
<?= $form->field($model,'number[]')->textInput(['placeholder'=>'Enter number'])->label(false); ?>
</td>
<td>
<?= $form->field($model, 'country_id[]', ['options'=>['class'=>'half']])->dropDownList(ArrayHelper::map(Countries::find()->all(), 'country_id', 'name'), ['prompt'=>'Select country'])->label(false);?>
</td>
<td>
<?= $form->field($model, 'issue[]')->widget(
DatePicker::className(), [
'clientOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy'
]
])->label(false) ?>
</td>
<td>
<?= $form->field($model, 'expiry[]')->widget(
DatePicker::className(), [
'clientOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy'
]
])->label(false) ?>
</td>
</tr>
<tr>
<td class="f-tit lang">US visa C1/D
<?=Html::activeHiddenInput($model, "doc_id[]", ['value'=>'4']);?>
</td>
<td>
<?= $form->field($model,'number[]')->textInput(['placeholder'=>'Enter number'])->label(false); ?>
</td>
<td>
<?= $form->field($model, 'country_id[]', ['options'=>['class'=>'half']])->dropDownList(ArrayHelper::map(Countries::find()->all(), 'country_id', 'name'), ['prompt'=>'Select country'])->label(false);?>
</td>
<td>
<?= $form->field($model, 'issue[]')->widget(
DatePicker::className(), [
'clientOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy'
]
])->label(false) ?>
</td>
<td>
<?= $form->field($model, 'expiry[]')->widget(
DatePicker::className(), [
'clientOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy'
]
])->label(false) ?>
</td>
</tr>
<tr>
<td class="f-tit lang">Schengen visa
<?=Html::activeHiddenInput($model, "doc_id[]", ['value'=>'5']);?>
</td>
<td>
<?= $form->field($model,'number[]')->textInput(['placeholder'=>'Enter number'])->label(false); ?>
</td>
<td>
<?= $form->field($model, 'country_id[]', ['options'=>['class'=>'half']])->dropDownList(ArrayHelper::map(Countries::find()->all(), 'country_id', 'name'), ['prompt'=>'Select country'])->label(false);?>
</td>
<td>
<?= $form->field($model, 'issue[]')->widget(
DatePicker::className(), [
'clientOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy'
]
])->label(false) ?>
</td>
<td>
<?= $form->field($model, 'expiry[]')->widget(
DatePicker::className(), [
'clientOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy'
]
])->label(false) ?>
</td>
</tr>
</table>
<span class="add btn btn-primary" data-id="">Document</span>
</div>
<div class="bot">
<div class="form-group">
<?= Html::submitButton('Save & continue', ['class' => 'btn btn-primary', 'name' => 'signup-button', 'id' => 'f_sign']) ?>
</div>
<?php ActiveForm::end(); ?>
As you can see there is several inputs with same name. All of them needs to be required. I wrote standard rule:
public function rules()
{
return [
[['doc_id', 'number', 'country_id', 'issue', 'expiry'], 'required'],
[['doc_id', 'country_id'], 'integer'],
[['issue', 'expiry'], 'safe'],
[['number'], 'string', 'max' => 255],
];
}
But when one of the fields is filled (for example first ‘country_id[]’), other passes validation.
Can anyone tell me how to separate validation of this fields?
- In that form I have button “.add” (‘span.add’), which adds same block of fields using ajax:
<tr>
<td><?=Html::activeDropDownList($model,'doc_id[]', ArrayHelper::map(TravelDocs::find()->where(['!=','id','1'])->andWhere(['!=','id','2'])->andWhere(['!=','id','4'])->andWhere(['!=','id','5'])->all(), 'id', 'name'), ['prompt' => 'Select document','class'=>'form-control']);?></td>
<td><?=Html::activeTextInput($model,'number[]',['placeholder'=>'Enter number','class'=>'form-control']);?></td>
<td><?=Html::activeDropDownList($model,'country_id[]', ArrayHelper::map(Countries::find()->all(), 'country_id', 'name'), ['prompt' => 'Select country','class'=>'form-control']);?></td>
<td>
<?= DatePicker::widget([
'model' => $model,
'attribute' => 'issue[]',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy'
]
]);?>
</td>
<td>
<?= DatePicker::widget([
'model' => $model,
'attribute' => 'expiry[]',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy'
]
]);?>
</td>
</tr>
But validation rules doesn’t works on added fields.
How can I validate this part of form?
Sorry for my bad English.