开发者

sfGuardDoctrine plugin and form messages

I'm using the sfGuardDoctrine plugin, and I would like to customize the form validation messages.

How could I acomplish this? I can't find anything in the documentation.

The only way I have found is to copy sfGuardValidatorUser.class into /apps/frontend/lib/validator, bu开发者_StackOverflowt I would like to know if there is some way to just override the error message, not override the entire validator...


I have found a post about external authentication. There the author creates a custom form that inherits from the original plugin form.

I think it is a clean way to customize everything, including error messages.

Link: http://blog.honnecke.us/2010/01/using-sfdoctrineguardusers-external-authentication/

Basically, we have to add in app.yml the name of our custom form:

sf_guard_plugin_signin_form: sfGuardCustomFormSignin

Then we create the form into the lib/form directory of our app folder:

class sfGuardCustomFormSignin extends sfGuardFormSignin
{
    public function configure(){
        parent::configure();
        // custom code here
    }
}

Done!


Strangely enough I was wanting to do this and searching StackOverflow just as you posted.

In sfGuardUserForm.class.php

For 'normal' fields (e.g. first_name, last_name), use getValidator:

public function configure()
{
  parent::configure();
  ...
  // You may not need to set the field as true, depending on your sfDoctrineGuard schema
  $this->getValidator('first_name')->setOption('required', true);
  $this->getValidator('first_name')->setMessage('required', 'Please enter your first name');
}

For fields in the PostValidator, the messages can be changed in the code:

  public function configure()
  {
    parent::configure();
    ...

    // Handle the email address error - more complicated than the rest
    $pv = $this->validatorSchema->getPostValidator()->getValidators();
    $pv[0]->setMessage('invalid', "Someone has already registered with this email address");
  }

Source for the basic idea

In sfGuardFormSignin.class.php

There doesn't seem to be an easy to follow logic here... for this post validator I used the following:

 public function configure()
 {
   parent::configure();
   $this->validatorSchema->getPostValidator()->setMessage('invalid', 'Your email address or password is invalid');
 }

I ended up with this structure through trial and error, but it works. (Now I need to move the post validator error from the 'username' field to a global error)

Other useful resources

This post shows how to set default messages to replace the rather unhelpful default symfony form errors.


If it's just the existing validation message you're looking to customise, I've seen four:

1 - failed username
2 - failed password
3 - failed username & password global error
4 - csrf attack after session timeout on form

The first two you can customise directly in plugins/sfDoctrinGuardPlugin/lib/form/doctrine, as you would for any other form.

The third I haven't bothered / found a way.

The fourth can be done like this: Symfony 1.4: Custom error message for CSRF in forms

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜