开发者

Symfony 1.4 : Hide a widget and its validator, based on a cookie?

On my website, I use a ReCaptcha widget in the form used to add comments. Once the form has been correctly sent, I write a cookie t开发者_JAVA百科o the user's computer.

I would like to remove the ReCaptcha widget when the user has that cookie, so that returning visitors don't have to type a captcha. Can I do that in forms/commentForm.class.php, or do I need to create a new form ?


Save your flag in session:

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

In another action:

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

Hope this helps.


It is often handy to pass a User instance as an option when creating a form in action:

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

Now you can configure you form based on user session attributes:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜