http://blog.sina.com.cn/s/blog_685213e70101mo4i.html

文档:

http://www.yiiframework.com/doc/api/1.1/CActiveForm

All these validations share the same set of validation rules declared in the associated model class. CActiveForm is designed in such a way that all these validations will lead to the same user interface changes and error message content.

To ensure data validity, server-side validation is always performed. By setting enableAjaxValidation to true, one can enable AJAX-based validation; and by setting enableClientValidation to true, one can enable client-side validation. Note that in order to make the latter two validations work, the user's browser must has its JavaScript enabled. If not, only the server-side validation will be performed.

The AJAX-based validation and client-side validation may be used together or separately. For example, in a user registration form, one may use AJAX-based validation to check if the user has picked a unique username, and use client-side validation to ensure all required fields are entered with data. Because the AJAX-based validation may bring extra workload on the server, if possible, one should mainly use client-side validation.

The AJAX-based validation has a few limitations. First, it does not work with file upload fields. Second, it should not be used to perform validations that may cause server-side state changes. Third, it is not designed to work with tabular data input for the moment.

Login.php:

------------------------------------------------------------------------

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true, //进行客户端验证
'clientOptions'=>array(
//点击submit按钮时进行验证
'validateOnSubmit'=>true,
),
'htmlOptions'=>array(
//设置是否进行表单数据的验证
'onsubmit'=>'return aa()',  //当点击submit按钮时进行验证,若aa函数返回true,则表单提交,若返回false,则不提交
),
)); ?>
 
js:
----------------------------------------------------------------------------
function aa(){
alert('ss');
return false;//返回false则不进行表单的提交
}
 
 
 
 

yii刷新页面验证码不变修改

原因:
每次刷新页面的时候都会调用CCaptcha这个widget的run方法来运行这个助手:
/**

 * Renders the widget.
 */
public function run()
{
    if(self::checkRequirements())
    {
        $this->renderImage();   //生成验证码图片
        $this->registerClientScript();
    }
    else
        throw new CException(Yii::t('yii','GD and FreeType PHP extensions are required.'));
}

/**
 * Renders the CAPTCHA image.
 */
protected function renderImage()
{
    if(!isset($this->imageOptions['id']))
        $this->imageOptions['id']=$this->getId();
   //生成验证码图片链接src地址,这个是生成图片的关键,指向action为 $captchaAction='captcha'的方法,即调
用CCaptchaAction这个方法来生成验证码图片
    $url=$this->getController()->createUrl($this->captchaAction,array('v'=>uniqid()));
    $alt=isset($this->imageOptions['alt'])?$this->imageOptions['alt']:'';
    echo CHtml::image($url,$alt,$this->imageOptions);
} 
CCaptchaAction中的执行流程:

/**

 * Runs the action.
 */
public function run()
{
    if(isset($_GET[self::REFRESH_GET_VAR]))  // AJAX request for regenerating code
    {
        $code=$this->getVerifyCode(true);
        echo CJSON::encode(array(
            'hash1'=>$this->generateValidationHash($code),
            'hash2'=>$this->generateValidationHash(strtolower($code)),
            // we add a random 'v' parameter so that FireFox can refresh the image
            // when src attribute of image tag is changed
            'url'=>$this->getController()->createUrl($this->getId(),array('v' => uniqid())),
        ));
    }
    else
        $this->renderImage($this->getVerifyCode());  //刷新页面时会调用这个,问题就出现在这,他调用
这个方法的时候没有传递参数true
    Yii::app()->end();
} 

/**

 * Gets the verification code.
 * @param boolean $regenerate whether the verification code should be regenerated.
 * @return string the verification code.
 */
public function getVerifyCode($regenerate=false) //从这个参数可以看出 如果$regenerate为true,则会
重新生成验证码图片
{
    if($this->fixedVerifyCode !== null) 
        return $this->fixedVerifyCode;

    $session = Yii::app()->session;
    $session->open();
    $name = $this->getSessionKey();
    if($session[$name] === null || $regenerate)
    {
        $session[$name] = $this->generateVerifyCode();
        $session[$name . 'count'] = 1;
    }
    return $session[$name];
} 

解决办法:

一:根据getVerifyCode这个方法中的这段代码来修改,这段代码是用于验证的,如果设定了fixedVerifyCode
,则每次
生成时都会生成一个固定的验证码,我们所要做的是把这个固定的变成动态的。
if($this->fixedVerifyCode !== null)
return $this->fixedVerifyCode; 修改控制器中生成验证码的配置:
/**
* Declares class-based actions.
*/
public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the register page
'captcha'=>array(
'class'=>'CCaptchaAction',
'fixedVerifyCode' => substr(md5(time()),0,4),
'foreColor' => 0x55FF00,
'testLimit' => 0, //不限制相同验证码出现的次数
'offset' => 5,
'minLength' => 4,
'maxLength' => 4,
'transparent' => true,
),
);
} 二、继承CCaptchaAction这个类,修改 run()方法中的 $this->renderImage($this->getVerifyCode())这句为
$this->renderImage($this->getVerifyCode(true)),其他不变 缺点:这种方法在CActiveForm开启enableClientValidation=true时,总是报验证码不正确,
enableAjaxValidation开启没事,待解决。。。 代码如下:
继承的类DCCaptchaAction.php


对应修改生成验证码的controller如下:
 
 
yii验证码问题总结:
 

Yii CActiveForm的更多相关文章

  1. Yii CActiveForm 客户端验证(enableClientValidation)和自定义验证

    使用Yii的CActiveForm默认使用服务器端模型(model)的rules规则验证数据. 但这会导致无谓的请求提交,比较好的方式是为了用户体验在客户端也验证,而为了安全性,在服务器端和数据库也做 ...

  2. 使用Yii框架自带的CActiveForm实现ajax提交表单

    Php代码:  <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array ...

  3. 最为简易的yii 教程(一)

    了解目录的框架结构 framework主要有 base          框架核心组件 caching        缓存组件 db                数据库组件 gii          ...

  4. CSRF token 无法被验证. ----Yii连接数据库后数据库错误日志报错

    CSRF token 无法被验证. 我使用的是mongodb+ yii1.1 What is CSRF, please see the details here.  http://en.wikiped ...

  5. yii-mail yii 发送邮件

    参考网址:http://shoukii0721.iteye.com/blog/1576225 有很多时候我们需要给用户发送邮件,作留言,或者是激活邮件.等用途. 需要注意的是,设置发送的邮件得有SMT ...

  6. yii弹出层

    Yii弹出层,包装了JQuery的JDialog,使用很方便.Yii组件功能太强大,强大到无法自拔 $this->beginWidget('zii.widgets.jui.CJuiDialog' ...

  7. yii表单

    yii  的dropdownlist,用yii的session可以记下选中的状态 $form = $this->beginWidget('CActiveForm',array('action'= ...

  8. YII开发技巧分享——模型(models)中rules自定义验证规则

    YII的models中的rules部分是一些表单的验证规则,对于表单验证十分有用,在相应的视图(views)里面添加了表单,在表单被提交之前程序都会自动先来这里面的规则里验证,只有通过对其有效的限制规 ...

  9. Yii框架学习笔记(二)将html前端模板整合到框架中

    选择Yii 2.0版本框架的7个理由 http://blog.chedushi.com/archives/8988 刚接触Yii谈一下对Yii框架的看法和感受 http://bbs.csdn.net/ ...

随机推荐

  1. PDF编辑、删除、替换某页面或文字

    在工作中,我们常常会用到PDF,当然尤其是会计,我虽然是程序员,但是“小老鼠”是会计,前几天,突然问我,怎么样将PDF中的某个页面替换掉,也就是删掉某页然后再从另外一个地方找一页补上来: 还需要改变这 ...

  2. 使用Mybatis Generator 生产 AS400中的数据表对象

    第一次使用Mybatis,由于公司核心服务器是AS400,参考了网络各个大大的教程后,发现无法使用Mybatis Generator自动生成AS400中的表对象 参考URL: http://www.c ...

  3. initial,常用于消除css格式

    刚在群里有人问在不改变原有css的情况下怎么清除一个css属性.有人提出了 initial,再此记录下. initial 关键字用于设置 CSS 属性为它的默认值. initial 关键字可用于任何 ...

  4. 打破常规——大胆尝试在路由器上搭建SVN服务器

    注册博客园挺久了,一直比较懒,虽然有几次想写点文章,但是一直没有行动,今天给大家带来一篇比较有意思的文章,不涉及技术上的,希望大家轻拍.本文的文字和图片全部为原创,尊重作者转载请注明出处! 说起路由器 ...

  5. Spark Streaming揭秘 Day12 数据安全容错(Executor篇)

    Spark Streaming揭秘 Day12 数据安全容错(Executor篇) 今天,让我们研究下SparkStreaming在Executor端的数据安全及容错机制. 在SparkStreami ...

  6. 源码编译安装LAMP环境及配置基于域名访问的多虚拟主机

    实验环境及软件版本: CentOS版本: 6.6(2.6.32.-504.el6.x86_64) apache版本: apache2.2.27 mysql版本:  Mysql-5.6.23 php版本 ...

  7. js 使用技巧 - [近几年工作中的经验总结的技巧]

    1.如果 ajax 返回单一的 json 格式,接收方需要这样再格式化一下赋值: var str = eval("(" + msg + ")"); 开发引用: ...

  8. Asp.net之LINQ入门视频教程

    当前位置: 主页 > 编程开发 > Asp.net视频教程 > Asp.net之LINQ入门视频教程 > http://www.xin1234.com/Program/Aspn ...

  9. eclipse里maven项目An error occurred while filtering resources解决办法(转载)

    转自:http://liyanjie918.blog.163.com/blog/static/20227290201581143110105/ 在使用eclipse构建maven项目时,突然出现错误提 ...

  10. 研读代码必须掌握的Eclipse快捷键

    1. Ctrl+左键 和F3 这个是大多数人经常用到的,用来查看变量.方法.类的定义跳到光标所在标识符的定义代码.当按执行流程阅读时,F3实现了大部分导航动作. 2 Ctrl+Shift+G在工作空间 ...