groovekiller
groovekiller

Reputation: 1122

Yii Prevent Form Submit on client side validation

I'm having an issue trying to make a client side validation on yii 1.1.12. I have the following

<?php
Yii::app()->clientScript
        ->registerCoreScript('jquery');
?>
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'my-form',
    'enableClientValidation' => true,
        'clientOptions'=> array('validateOnSubmit'=>true,
                                 'afterValidate'=>'js:function() 
                                        {     
                                           return false
                                        }'
                                ),
        'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); 
?>

Even using a return false in after validate, my form still being submitted. Any way to solve this?

Thanks.

Upvotes: 1

Views: 12284

Answers (4)

Kedar Adhikari
Kedar Adhikari

Reputation: 701

'enableClientValidation'=>true,
 'clientOptions' => array('validateOnSubmit'=>true),

Upvotes: 0

Onkar Janwa
Onkar Janwa

Reputation: 3950

You may be going right way. But Yii client side validation also depends on the error you show in form. If your showing error using CHtml::error() method than for that variable client side validation will not happen. I can't say what you are using as you have not posted any code. So try Using $form->error() method. Let me know if still your are facing problem.

Also check the link below given.

<?php
Yii::app()->clientScript
        ->registerCoreScript('jquery');
?>
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'my-form',
    'enableClientValidation' => true,
    'clientOptions'=> array('validateOnSubmit'=>true),
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); 
?>

http://www.waytoblogger.com/blog/technical/yii/yii-client-side-validation/

Upvotes: 4

groovekiller
groovekiller

Reputation: 1122

Solved. just removed

<?php
Yii::app()->clientScript
        ->registerCoreScript('jquery');
?>

Upvotes: 0

Elbek
Elbek

Reputation: 3484

Try this:

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'my-form',
    'enableClientValidation' => true,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); 
?>

I see in clientOptions you are not doing any specific thing. So just removed. And you do not have to include jquery by yourself, As far as i know yii automatically includes it when it is required(Here activeform requires, I guess). You may have a look to this url

Upvotes: 2

Related Questions