Question

I am new to yii and having trouble with client side validation for my textarea. I am not sure what I am doing wrong but for some reason the client side validation does not work only for this textarea.

Below is my rules in my model:

public function rules()
{
    return array(
        array('content', 'required'),
        array('user_id, seen', 'numerical', 'integerOnly'=>true),
        array('datetime', 'safe'),
        array('id, user_id, content, datetime, seen', 'safe', 'on'=>'search'),
    );
}

Here is the code in my view:

<?php $form=$this->beginWidget('TbActiveForm', array(
        'id'=>'post-form',
        'action'=>Yii::app()->createUrl('/feedback/default/create'),
        'enableClientValidation'=>true,
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
        ),
    )); ?>
    <div class="modal-header">
        <a class="close" id="close_modal" onclick="idEmpty(event)">&times;</a>
        <h4>New Feedback</h4>
    </div>

    <div class="modal-body">
        <?php echo $form->textarea($model,'content', array('id'=>'feedback_content','class'=>'feedback_textarea')); ?>
        <?php echo $form->error($model,'content'); ?>
    </div>

    <div class="modal-footer">
        <div class="hint" style="float:left">
            <span style="color:red;font-weight: bold">Hint: </span>You can use <?php echo CHtml::link('markdown', 'http://daringfireball.net/projects/markdown/syntax'); ?> syntax!
        </div>
        <?php echo TbHtml::submitButton('Send Feedback', array('color' => TbHtml::BUTTON_COLOR_SUCCESS)); ?>
    </div>

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

and finally this is my controller:

public function actionCreate()
{
    $model=new Feedback;
    if(isset($_POST['Feedback'])) {
        $model->attributes = $_POST['Feedback'];
        $model->user_id = Yii::app()->user->getId(); 
        $model->datetime = date("Y-m-d H:i:s");
        $model->seen = 0;
        if($model->validate()) {
            $model->save();
            $this->redirect(array('/dashboard'));
        }
        else
            echo 'cant validate';
    }

}

If I try to violate the rule for this textarea, like if I leave it blank, I do not get the proper error message and I end up getting "can't validate" (which is only there for testing purposes). My database is mysql and the intended field is of type "TEXT".

Any help is appreciated. Thanks

Was it helpful?

Solution

Remove 'id'=>'feedback_content' from $form->textarea(). CActiveForm it self will create the ID for a form filed. For your text area id should be Feedback_content not feedback_content (Case sensitive). Because of this may be validation message is not notifying in the view. Put errorSummary to check the validation

 <?php echo $form->errorSummary($model); ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top