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 ?