Stas
Stas

Reputation: 23

Yii 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>

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?

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.

Upvotes: 2

Views: 7582

Answers (1)

Anton Kucherov
Anton Kucherov

Reputation: 307

This instruction work, only for "safe" params:

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

You must add "message" to safe validator:

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

Or you must use:

$this->form->subject  = $_POST[$this->name]['subject'];
$this->form->message  = $_POST[$this->name]['message'];

See this documentation: http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules

Your validation doesn't work because "message" is required but not "safe".

Upvotes: 1

Related Questions