Form textField values

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.

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'))




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.

Anyone else have any ideas?

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.

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

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.

hi friends i am using

<?php echo $form->labelEx($model,‘attribute1’); ?>

	&lt;?php echo &#036;form-&gt;textField(&#036;model,'attribute1'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'attribute1'); ?&gt;

<?php echo $form->labelEx($model,‘attribute2’); ?>

	&lt;?php echo &#036;form-&gt;textField(&#036;model,'attribute2'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'attribute2'); ?&gt;

this work fine …

how to fach database value in textfield in table format

please help me