Yii Framework Forum: Form Field Validation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Form Field Validation Rate Topic: -----

#1 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 237
  • Joined: 26-September 12

Posted 19 November 2012 - 05:20 AM

In my application have 2 tables. user and address with fields..
1. User- user_id, name, age
2. Address- user_id, city, state

for creating 2 table only Usercontroller used. The form of "User" contain the fields of "Address". like this
<div class="row">
            
		<?php echo $form->labelEx($model,'name'); ?>
		<?php echo $form->textField($model,'name',array('size'=>50,'maxlength'=>50)); ?>
                <?php echo $form->error($model,'name'); ?>
	</div>
      <div class="row">
                <?php echo $form->labelEx(Address::model(), 'city'); ?>
                <?php echo $form->textField(Address::model(), 'city', array('size' => 50, 'maxlength' =>50));>
                <?php echo $form->error(Address::model(), 'city'); ?>
        </div>   



The validation of user table fields worked correctly. but the validation (field is empty, required) for "Address" table not worked. How can add validation to both model form fields?
Thanks
Aneesh.
0

#2 User is offline   Reza m 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 07-March 12

Posted 19 November 2012 - 05:34 AM

you should send a model for address to your view

<div class="row">
                <?php echo $form->labelEx($AddressModel, 'city'); ?>
                <?php echo $form->textField($AddressModel, 'city', array('size' => 50, 'maxlength' =>50));>
                <?php echo $form->error($AddressModel, 'city'); ?>
        </div>



read this:
collect data for two or more models
0

#3 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,165
  • Joined: 05-January 12
  • Location:Morocco

Posted 19 November 2012 - 05:35 AM

You need to use an actual Address model in your form instead of Address::model().

Obviously, in the controller action that renders that form, you need to pass an Address model and validate it like the way you do with the User model.
0

#4 User is offline   Iran 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 27-September 12

Posted 19 November 2012 - 05:38 AM

Are you have in your form this code? =>

<?php echo $form->errorSummary(array($model,$AddressModel)); ?> 



in the place of $AddressModel in can put other variable name.
0

#5 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 237
  • Joined: 26-September 12

Posted 19 November 2012 - 06:43 AM

Thanks for all rplys... it helps to me.
but the validation for 2nd model fields happends only when click on the textboxes of that model? How solve this?
Thanks
Aneesh.
0

#6 User is offline   yiiplayboy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 03-May 12

Posted 22 November 2012 - 06:29 AM

View PostRvr101, on 19 November 2012 - 06:43 AM, said:

Thanks for all rplys... it helps to me.
but the validation for 2nd model fields happends only when click on the textboxes of that model? How solve this?


don't forget to include the "city" field as required in your address model in your rules array
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users