dousi6701 2014-01-11 10:43
浏览 42
已采纳

在Yii中实施强制性空验证?

I am implmenting a captcha type, bot checking where instead of having users fill out the irritating capture we create a hidden field called password_confirm and then when checking posted variables if it contains a value an error will be made.

I have read around and apparently it would be best to implement in a beforeValidate() event handler as opposed to creating a new validation rule.

However I am unsure how to stop the process and how to return an error message if that is even necessary, as when you call validate() it will clear all existing error messages.

Also not too sure if we can set it up using scenarions so only in the register scenario does it apply.

In the form Widget:

<div class="row">
        <?php echo $form->hiddenField($model, 'password_confirm'); ?>
        <?php echo $form->error($model,'password_confirm'); ?>
    </div>

In the FormModel:

class LoginForm extends CFormModel
{
public $password_confirm; 

...

protected function beforeValidate() 
    {
    if(empty($this->password_confirm)) 
        {
            return false;
        }
        else 
        {
            return parent::beforeValidate();
        }
    }

I got a Property Not Defined Error: So I added to the User model:

public $password_confirm; 

But then after registering, It didn't show anything except the Application log. There are probably a number of problems here, I sorry for the convoluted question.

  • 写回答

2条回答 默认 最新

  • dongshuofu0039 2014-01-11 11:38
    关注

    try this

    protected function beforeValidate() 
        {
        if(empty($this->password_confirm)) 
            {
                $this->addError('password_confirm','You crazy bot... go away');
            }
            else 
            {
                return parent::beforeValidate();
            }
        }
    

    Surely you can add error if the validation fails using $this->addError($attribute,$message);

    Here $attribute is the field name corresponding to which you want to add error and $message is the message you want to display

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)