开发者

In symfony forms how I can validate a field (i.e. password) querying the db for the right value?

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 ?


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜