ajaxSubmitButton in CForm

Hi,

I am using CForm with some customized configurations for user inputs. The configurations of the CForm are altered via ajax on the fly so that I can add or delete CFormElements according to some preferences. This works great but I got problems adding a simple ajaxSubmitButton to my CForm configuration.

Is there an elegant way to add ajaxSubmitButtons to a CForm object? I don’t want to use CActiveForm as it isn’t that flexible

Thanks in advance!

Ok, after asking it came to my mind that I could use this workaround. Instead of adding the buttons via form-config I render the button in the partial view between the two <form> tags like this.


<div class="form">


    <?php

        echo $form->renderBegin();


        foreach($form->getElements() as $element)

            echo $element->render();


        echo CHtml::ajaxSubmitButton('search',

        array('/item/admin'),

            array(

            'method'=>'POST',

        ));

        

        

        echo $form->renderEnd();

    ?>


</div>

Thanks for sharing Haensel…

Hi,

i’ve got the solution.

  1. assign key ‘attributes’ to your button with an array as value

  2. assign key ‘ajax’ in array attributes and configure linke ajaxButton, ajaxSubmitButton.


            'buttons' => array(

                'login' => array(

                    'type' => 'submit',

                    'label' => 'Speichern',

                    'attributes' => array(

                        'class' => 'submit',

                        'ajax' => array(

                            'type' => 'POST', //request type

                            'url' => Yii::app()->createUrl($this->getId() . '/AjaxTest'), //url to call.

                            'update' => '#content', //selector to update

                        )

                    ),

                ),

            )

Best Regards

M. Bunge

Thanks! Really nice find!