Answer the question
In order to leave comments, you need to log in
How to setup a validator for a form from another model?
I have one common Contact form, where 2 forms are taken from 2 models, full name from the Fio model, Number from the Phone model. For fio, I set up validation without problems with Phone, there were difficulties, it does not validate its field, requared shows but does nothing. How to setup Number validation in this view?
View Name
<?php
/* @var $this TownController */
/* @var $model Town */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $this->beginWidget(
'booster.widgets.TbModal',
array('id' => 'myModal')
); ?>
<?php $form = $this->beginWidget('booster.widgets.TbActiveForm', array(
'id' => 'fio-form',
'enableAjaxValidation' => true,
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => true,
),
'action' => Yii::app()->createUrl('fio/create')
)); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Создание контакта</h4>
</div>
<div class="modal-body">
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?PHP echo CHtml::form();
$data = CHtml::listData(Town::model()->findAll(), 'ID', 'Town'); // первое поле, тут запрашивается город и его id
echo CHtml::dropDownList('ID', '', $data,
array(
'prompt' => 'Выберите город',
'ajax' => array(
'type' => 'POST',
'url' => Yii::app()->createUrl('Fio/dynamicStreets'), //ссылка на метод в контроллере
'update' => '#Street_ID', //тэг для второго поля
'data' => array('ID' => 'js:this.value'),
)));
echo CHtml::dropDownList('Street_ID', '', array(), array('prompt' => 'Выберите улицу')); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'Building'); ?>
<?php echo $form->textField($model, 'Building'); ?>
<?php echo $form->error($model, 'Building'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'Surname'); ?>
<?php echo $form->textArea($model, 'Surname', array('rows' => 1, 'cols' => 40)); ?>
<?php echo $form->error($model, 'Surname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'Patronymic'); ?>
<?php echo $form->textArea($model, 'Patronymic', array('rows' => 1, 'cols' => 40)); ?>
<?php echo $form->error($model, 'Patronymic'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'Name'); ?>
<?php echo $form->textArea($model, 'Name', array('rows' => 1, 'cols' => 40)); ?>
<?php echo $form->error($model, 'Name'); ?>
</div>
<?php
$this->widget('ext.widgets.reCopy.ReCopyWidget', array(
'targetClass' => 'clone-this',
'addButtonLabel' => 'Добавить номер',
'addButtonCssClass' => 'add-clone',
'removeButtonLabel' => 'Убрать поле',
'removeButtonCssClass' => 'remove-clone',
'limit' => 5,
));
?>
<div class="clone-this">
<?php echo $form->labelEx($phone, 'Type_ID'); ?>
<?php echo $form->dropDownList($phone, 'Type_ID[]', CHtml::listData(Type::model()->findAll(), 'ID', 'Type_Phone')); ?>
<?php echo $form->error($phone, 'Type_ID[]'); ?>
<?php echo $form->labelEx($phone, 'Number'); ?>
<?php echo $form->textField($phone, 'Number[]', array('rows' => 1, 'cols' => 40)); ?>
<?php echo $form->error($phone, 'Number[]'); ?>
</div>
</div>
<div class="modal-footer">
<?php $this->widget(
'booster.widgets.TbButton',
array(
'buttonType' => 'submit',
'context' => 'primary',
'label' => 'Сохранить'
)
); ?>
<?php $this->widget(
'booster.widgets.TbButton',
array(
'label' => 'закрыть',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
</div>
<?php $this->endWidget(); ?>
<?php $this->endWidget(); ?>
</div><!-- form -->
Answer the question
In order to leave comments, you need to log in
Solved the problem with a third party validator using JavaScript and HTML5 validator bitsofco.de/realtime-form-validation. I got enough functionality.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question