SfGuardDoctrine plugin and form posts

I am using the sfGuardDoctrine plugin and I would like to customize form validation messages.

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

The only way I have found is to copy sfGuardValidatorUser.class to / apps / frontend / lib / validator, but I would like to know if there is a way to just override the error message rather than override the whole validator ..

+2


a source to share


3 answers


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

I think this is a clean way to tweak everything, including error messages.

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

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



sf_guard_plugin_signin_form: sfGuardCustomFormSignin

      

Then we create a form in the lib / form directory of our application folder:

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

      

Done!

+3


a source


Oddly enough, I wanted to do this and search StackOverflow in the same way you posted.

In sfGuardUserForm.class.php

For "normal" fields (eg 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 PostValidator, the messages can be changed in 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");
  }

      

The source of the basic idea



In sfGuardFormSignin.class.php

There seems to be no simple 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 message authentication error from the username field to the global error)

Other helpful resources

This post shows how to set default messages to replace the pretty useless symfony default form errors.

+2


a source


If it's just an existing validation message that you want to customize, 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 can be configured directly in plugins / sfDoctrinGuardPlugin / lib / form / doctrine, just like any other form.

Third I didn't bother / found a way.

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

-1


a source







All Articles