简体   繁体   中英

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.

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/

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

Solved. just removed

<?php
Yii::app()->clientScript
        ->registerCoreScript('jquery');
?>
'enableClientValidation'=>true,
 'clientOptions' => array('validateOnSubmit'=>true),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM