Assign Values And Show Another In Yii Textfield

Hi,

Here is what I face…

I need to add a readonly field in to a form and also a value which i pick from model.at the same time i need to show a name in that text field and pass an integer to the DB as the value of that field.




<div class="row">

  <?php echo $form->labelEx($model,'pkg_id'); ?>

  <?php echo $form->textField($model,'pkg_id', array('readonly'=>true,'value'=>$pkg->pkg_id)); ?>

  <?php echo $form->error($model,'pkg_id'); ?>

</div>



in here as value i’m passing an id…but I need to show in the text field the name of that package…How can this be done???? :(

I think you need to change this


'value'=>$pkg->pkg_id

into


'value'=>$pkg->pkg_name

or whatever it is (based on your column name in db that have the ‘name of that package’ value). so it will show the name not the id.

yes it is…But then it will pass the name to the table…I need only to sen the id and show the name…anyway I handle it by adding a action in controller and get the id from model before save… :)

In view


<div class="row">

		<?php echo $form->labelEx($model,'pkg_id'); ?>

		<?php echo $form->textField($model,'pkg_id', array('size'=>10,'readonly'=>true, 'value'=>$pkg->pkg_name)); ?>

		<?php echo $form->error($model,'pkg_id'); ?>

	</div>

In controller action


if(isset($_POST['PortalUserProfile']))

		{

			$pkg_id = PortalPackages::model()->findByAttributes(array('pkg_name'=>$_POST['PortalUserProfile']['pkg_id']));

			

			

			$model->attributes=$_POST['PortalUserProfile'];

			$model->pkg_id = $pkg_id->pkg_id;

			

			if($model->save())

				$this->redirect(array('view','id'=>$model->user_id));

		}

good then :)

May be you only need the following:




<div class="row">

  <?php echo $form->labelEx($model,'pkg_id'); ?>

  <?php echo $form->hiddenField($model, 'pkg_id'); ?>

  <?php echo CHtml::textField('pkg_id', $pkg->pkg_name, array('readonly' => true)); ?>

</div>