tags:

views:

382

answers:

3

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, but I would like to know if there is some way to just override the error message, not override the entire validator...

A: 

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.

PeterB
Where are you placing this? I assume it is /lib/form/doctrine/sfGuardFormSignin.class.phpI have tried it and got this error: Call to undefined method sfGuardValidatorUser::getValidators()
David
Solved, you have to delete the "getValidators()" call, and replace $pv[0] by $pv
David
The above code has been written to go in lib/form/doctrine/sfDoctrineGuardPlugin/sfGuardUserForm.class.php - for the other classes you may need to alter it.
PeterB
Ok, please modify the code as it doesn't work that way.
David
I can't modify it to work for you, as this *is* the working code, copied straight from my project. FWIW I'm using sfDoctrineGuard trunk, not the package release.
PeterB
A: 

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: http://stackoverflow.com/questions/2578397/symfony-1-4-custom-error-message-for-csrf-in-forms

Tom
A: 

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!

David