Form tags and layout

I am rendering form elements in a sidebar, but I want these elements submitted along with other form elements in the page. Because of the way the page is rendered it is not possible to put form tags above and below all form elements without actually nesting form tags in the layout rather than the view.




//Outputs form tag as expected

<?php echo CHtml::beginForm('','get'); ?>


//Outputs select element as expected within the form

<?php echo CHtml::activeLabel($model, "cohort");?>

<?php echo CHtml::activeDropDownList($model, "cohort", Yii::app()->common->cohortsDropDown,array(

											'id'=>'cohort-external-filter',

											'class'=>'span2',

											'prompt'=>'',

											));?>


<?php// This passes the output of the renderPartial to $this->filterForm which is rendered in the layout (sidebar)

$this->filterForm =$this->renderPartial('/_forms/ks4FilterForm',array(

					'model'=>$model,

					'form'=>$form),true

);?>

		

<?php//Rendered as expected within the form

$this->widget('bootstrap.widgets.BootGridView',array(

	'id'=>'ks4summary-grid1',

	'dataProvider'=>$dataProvider,

	'columns'=>array(

			array(

			'name'=>'col1',

			'header'=>'',

			//'htmlOptions'=>array('width'=>'80px'),

		),

		array(

			'name'=>'col2',

			'header'=>'Total',

			//'htmlOptions'=>array('width'=>'80px'),

		),

	),

)); ?>


//Outputs the end form tag above the rendered form held in $this->filterForm which is useless

<?php echo CHtml::endForm()?>



I know why this is happening, but starting to embed form tags within layouts e.g. in column1_with_form_tags just feels wrong. Is there another/proper way of dealing with this without writing a load of jquery.

Many thanks

Hi Termine,

I would choose from the following 2 implementations.

  1. Semantically place all the fields in the page, but visually place some of them in the side bar with the help of css.

  2. Create 2 forms: the main form in the page and the sub form in the side bar. And copy the values of the fields of the sub form to the corresponding hidden fields of the main form on submitting … just like as you are thinking.

I don’t believe that there’s a smarter solution. :(

Thanks very much softark. It looks like creating 2 forms and then using jquery to pass the fields back and forth is a cleaner way of doing it then trying to use form tags at the theme layer. Just thought I would ask in case I was missing something obvious.

Yeah, I agree.

BTW, just in case you didn’t take care by now, you should not nest the forms like this:




<form id="form_a">

    ...

    <form id="form_b">

        ...

    </form>

    ...

</form>



It’s illegal and won’t work as expected.

Your first post is OK, because it will output the html like this:




<form id="form_a">

    ...

</form>

...

<form id="form_b">

    ...

</form>