【问题标题】:Symfony 1.4 : Hide a widget and its validator, based on a cookie?Symfony 1.4:基于 cookie 隐藏小部件及其验证器?
【发布时间】:2011-05-09 10:47:11
【问题描述】:

在我的网站上,我以用于添加 cmets 的形式使用 ReCaptcha 小部件。正确发送表单后,我会将 cookie 写入用户的计算机。

我想在用户拥有该 cookie 时删除 ReCaptcha 小部件,这样回访者就不必输入验证码。我可以在forms/commentForm.class.php 中这样做吗,还是需要创建一个新表单?

【问题讨论】:

    标签: symfony1 symfony-1.4 symfony-forms


    【解决方案1】:

    在会话中保存您的标志:

    <?php
    ...
    if ($form->isValid()) {
        ...
        // comment added
        $this->getUser()->setAttribute('is_bot', false);
        ...
    }
    

    在另一个动作中:

    <?php
    $this->form = new CommentForm();
    if ($this->getUser()->getAttribute('is_bot', true)) {
        $this->form->setWidget();    // set captcha widget
        $this->form->setValdiator(); // set captcha valdiator
    }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      在实际创建表单时,将 User 实例作为选项传递通常很方便:

        public function executeNew(sfWebRequest $request)
        {
          $this->form = new ModelForm(null, array('user'=>$this->getUser));
        }
      

      现在您可以根据用户会话属性配置表单:

      class ModelForm extends BaseModelForm
      {
        public function configure()
        {
          if ($this->getOption('user')->getAttribute('is_bot', false)
          {
            //set your widgets and validators
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-10
        • 1970-01-01
        • 2020-10-16
        • 1970-01-01
        • 2016-09-25
        相关资源
        最近更新 更多