views:

61

answers:

1

Newbie question.

I've created a very simple LoginForm class in symfony. It checks if the user inserts the username and the password in the field and It work as expected.

// UserLoginForm :: configure()

$this->setWidgets(array(
  'username' => new sfWidgetFormInputText(),
  'password' => new sfWidgetFormInputPassword(),
));
$this->setValidators(array(
  'username' => new sfValidatorString(array('required' => true),
  'password' => new sfValidatorString(array('required' => true)
));


// Action :: executeLogin()

$this->loginForm = new UserLoginForm();

if ($request->isMethod('post')) {
    $params = $request->getParameter('login');
    $this->loginForm->bind($request->getParameter('login'));

    if ($this->loginForm->isValid()) {
        // ... more code                
    }
}

Now I'm trying to check the presence of the username field in the db and if it does not exists, I want to invalidate the form. The same for the password: if the username + password pair doesn't exist, but with a different message.

Trying to find the solution I searched in two possible ways:

In the action :

  • querying the db,
  • check the username,
  • invalidate the form username field (I want the error message in the resulting HTML),
  • same for the username + password check

In this case how I can invalidate the form ? How to handle the error message in the template ?

In the form class calling the isValid() method :

  • add something to the setValidator()
  • OR add a post validator that call a private function which query the model

In this case how can I use the model in the Form class ?

Which is the best/fast approach ? Other possibilities ?

+2  A: 

I think it will be very useful to you to look at the code of the validator of sfDoctrineGuardPlugin. They create that post validator, and set it in the form. Even if you don't want to use sfDoctrineGuardPlugin (or sfGuardPlugin if you use Propel) its code can be a valuable source of inspiration.

nacmartin
So basically i must create a custom validator which access the database and set it as a post validator in the form class.
yuri
Exactly. Just look at the links and do the same. (Or just use sfGuardPlugin!)
nacmartin