CActiveForm client validation is not working in my widget

I need "Report Bug" modal form on all pages of my application. So I made it in form of widget and placed it on the main layout template of my application.


<? $reportBugForm = $this->widget('application.widgets.reportBugWidget.reportBug',array(),true); ?>

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        ....

    </head>

    <body>

        <? echo $reportBugForm; ?>

I placed it at the top of the page, because it should post ajax requests to itself, regardless of the current page (I don’t want to use any controller). Here is the code of widget class itself:


Yii::import('application.widgets.reportBugWidget.reportBugForm');


class reportBug extends CWidget {


    public $id = 'reportBug';

    public $name = 'reportBugForm';

    public $form;


    public function init() {


        $this->form = new reportBugForm('error');


        if(Yii::app()->request->isAjaxRequest && isset($_POST[$this->name]) && !empty($_POST[$this->name])) {

            $this->form->attributes = $_POST[$this->name];

            if($this->form->validate()) {

                echo 'Form is submited!';

            } else

                echo 'You have incorrectly filled out the form!';

            Yii::app()->end();

        }

    }


    public function run() {


        $this->render('form',array(

            'model'=>$this->form,

            'id'=>$this->id,

            'name'=>$this->name

        ));

    }


}

Here is the code of my model. I’ve simplified everything for visibility:




class reportBugForm extends CFormModel {


    public $subject = '';

    public $message = '';


    public function rules() {

        return array(

            array('message','required','on'=>'error,question,suggestion'),

            array('subject','safe','on'=>'question'),

            array('subject','required','on'=>'error,suggestion')

        );

    }


    public function attributeLabels() {

        return array(

            'subject'=>'Subject',

            'message'=>'Message'

        );

    }

}



And here is the code of my view file:




<div class="modal fade hide" id="<? echo $id; ?>">

    <div class="modal-header">

        <button type="button" class="close" data-dismiss="modal" id="reportBug_iconClose">&times;</button>

        <h3>Report a bug</h3>

    </div>

    <?

    $form = $this->beginWidget('CActiveForm', array(

        'id'=>$name,

        'enableAjaxValidation'=>true,

        'enableClientValidation'=>true,

        'clientOptions'=>array(

            'validateOnSubmit'=>true,

            'validateOnChange'=>false,

            'validateOnType'=>false

        ),

        'focus'=>array($model,'subject')

    ));

    ?>

    <div class="modal-body">

        <fieldset>

            <div class="control-group">

                <?

                echo $form->label($model,'subject',array('for'=>'reportBug_subject'));

                echo $form->textField($model,'subject',array('id'=>'reportBug_subject','placeholder'=>'A short summary of your problem','class'=>'input-xxlarge'));

                echo $form->error($model,'subject',array('class'=>'help-block error'));

                ?>

            </div>

            <div class="control-group">

                <?

                echo $form->label($model,'message',array('for'=>'reportBug_message'));

                echo $form->textArea($model,'message',array('id'=>'reportBug_message','class'=>'input-xxlarge','rows'=>5,'placeholder'=>'Please, provide details of found error'));

                echo $form->error($model,'message',array('class'=>'help-block error'));

                ?>

            </div>

        </fieldset>

    </div>

    <div class="modal-footer">

        <?

        echo CHtml::ajaxSubmitButton('Send',null,array(),array('class'=>'btn btn-primary'));

        echo CHtml::htmlButton('Close',array('id'=>'reportBug_btnClose','class'=>'btn','data-dismiss'=>'modal'));

        ?>

    </div>

    <? $this->endWidget(); ?>

</div>



[b]No client or ajax validation is performed! Form is submitted by ajax without any client validation and


You have incorrectly filled out the form!

string is returned. Please, tell me what could it be?[/b]

P.S. I don’t know maybe it makes difference, but I’m using Twitter Bootstrap extension for Yii.

P.S.S. And one more strange thing. When I use


CHtml::submitButton

in my view file, form is simply submitted by POST, and when I use


CHtml::ajaxSubmitButton

it submits by Ajax and returns the result I’ve described above.

Thank you in advance!

Hi! I’ve updated my question. I hope now I described my problem more precisely. Besides I’ve placed this question on StackOverflow with no result so far.

The ‘string’ that is returned suggests that there is a problem with validating the form.

Open in Firefox with Firebug plugin. Look at XHR tab to see what is being sent through Ajax when you press the submit button.

You should be able to see the posted data and any message returned.


public function init() {


        $this->form = new reportBugForm('error');


        if(Yii::app()->request->isAjaxRequest && isset($_POST[$this->name]) && !empty($_POST[$this->name])) {

            $this->form->attributes = $_POST[$this->name];

            // HERE

            if($this->form->validate()) {

                echo 'Form is submited!';

            } else

                echo 'You have incorrectly filled out the form!';

            Yii::app()->end();

        }

    }