Yii Framework Forum: Form textField values - Yii Framework Forum

Jump to content

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

Form textField values [EDITED FOR CLARIFICATION] Rate Topic: -----

#1 User is offline   enfield 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 288
  • Joined: 12-January 11

Posted 05 December 2011 - 11:29 PM

Why doesn't something like this work in CActiveForm?

<?php echo $form->textField($model, 'attributeId', array('style'=>'width:250px'),'attributeId', 'otherAttribute'); ?>


I want my users to enter otherAttribute instead of attributeId but the code above returns an integrity constraint violation.

EDIT for CLARIFICATION:
I can accomplish this with a dropDownList but need it to be a textField

<?php echo $form->dropDownList($model, 'attributeId', 
          CHtml::listDataEx(ModelB::model()->findAll(), 'attributeId', 'otherAttribute')); ?>


More info: I have two tables TableA and TableB, I then have a form collecting data to populate TableA. TableB (attributeId) is a related table to TableA, hence the foreign key mentioned originally.

If I use
<?php echo $form->textField($model, 'attributeId', array('style'=>'width:250px')); ?>

where attributeId is the foreign key everything works just fine. However there is another attribute in TableB (otherAttribute ) that I want the users to enter in the form but still have attributeId entered in TableA.
0

#2 User is offline   Weavora Team 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 141
  • Joined: 06-December 10

Posted 06 December 2011 - 03:22 AM

Not really understand what you trying to do, but you could try:

// 1. you will get <input type="text" name="SomeForm[otherAttribute]" value="<?php echo $model->attributeId;?>" style=".." />
$form->textField($model, 'attributeId', array('style'=>'width:250px', 'name'=> 'SomeForm[otherAttribute]'))


// 2. you will get <input type="text" name="otherAttribute" value="<?php echo $model->attributeId;?>" style=".." />
$form->textField($model, 'attributeId', array('style'=>'width:250px', 'name'=> 'otherAttribute'))

// 3. you will get <input type="text" name="SomeForm[otherAttribute]" value="<?php echo $model->otherAttribute;?>" style=".." />
$form->textField($model, 'otherAttribute', array('style'=>'width:250px'))


0

#3 User is offline   enfield 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 288
  • Joined: 12-January 11

Posted 06 December 2011 - 04:29 AM

More info: I have two tables TableA and TableB, I then have a form collecting data to populate TableA. TableB (attributeId) is a related table to TableA, hence the foreign key mentioned originally.

If I use
<?php echo $form->textField($model, 'attributeId', array('style'=>'width:250px')); ?>
where attributeId is the foreign key everything works just fine. However there is another attribute in TableB (otherAttribute ) that I want the users to enter in the form but still have attributeId entered in TableA.
0

#4 User is offline   enfield 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 288
  • Joined: 12-January 11

Posted 06 December 2011 - 06:02 PM

Anyone else have any ideas?
0

#5 User is offline   waitforit 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 401
  • Joined: 09-February 11

Posted 07 December 2011 - 12:47 AM

I'm probably reading this wrong.

But why not (in controller)

$modelA = new A;
$modelB = new B;

Then, in the view:

<?php echo $form->textField($modelA, 'attributeId');?>
<?php echo $form->textField($modelB, 'attributeId');?>
0

#6 User is offline   enfield 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 288
  • Joined: 12-January 11

Posted 07 December 2011 - 08:35 PM

View Postdniznick, on 07 December 2011 - 12:47 AM, said:

I'm probably reading this wrong.

But why not (in controller)

$modelA = new A;
$modelB = new B;

Then, in the view:

<?php echo $form->textField($modelA, 'attributeId');?>
<?php echo $form->textField($modelB, 'attributeId');?>



Yeah it is not a multimodel form, just capturing data through a foreign key. Thanks for the input though.
0

#7 User is offline   enfield 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 288
  • Joined: 12-January 11

Posted 07 December 2011 - 10:27 PM

Edited my original question to hopefully clarify what I am after.
0

#8 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 07 December 2011 - 11:53 PM

First, you must add a new field to the model (public $otherAttribute). Then create a text field for it. You can use CExistValidator to check if the entered value exists in the related table. If yes, then in beforeSave() find this value and set model's attributeId. Or you can set it to NULL otherwise.
0

#9 User is offline   rajesh chaurasia 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 12-January 13
  • Location:Mohali

Posted 20 March 2013 - 11:05 PM

hi friends i am using

<?php echo $form->labelEx($model,'attribute1'); ?>
<?php echo $form->textField($model,'attribute1'); ?>
<?php echo $form->error($model,'attribute1'); ?>

<?php echo $form->labelEx($model,'attribute2'); ?>
<?php echo $form->textField($model,'attribute2'); ?>
<?php echo $form->error($model,'attribute2'); ?>

this work fine ...
0

#10 User is offline   abhijeet 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 05-September 14

Posted 19 September 2014 - 12:51 AM

how to fach database value in textfield in table format
please help me
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