开发者

Symfony form validation

开发者 https://www.devze.com 2023-02-07 14:29 出处:网络
//creates a sfForm for the login $this->form = new sfForm(); $this->form->setWidgets(array( \'email\' => new sfWidgetFormInputText(array(\'default\' => \'me@example.com\')),
//creates a sfForm for the login
  $this->form = new sfForm();
  $this->form->setWidgets(array(
    'email' => new sfWidgetFormInputText(array('default' => 'me@example.com')),
    'password' => new sfWidgetFormInputPassword()
    )
  );

  //TODO: sfValidatorRegex on the password!
  $this->form->setValidators(array(
   'email' => new sfValidatorEmail(),
   开发者_Python百科'password' => new sfValidatorString(array(
    'min_length' => 8, 
    'max_length' => 255), 
    array(
     'min_length' => 'Password is too short. Minimum 8 characters required.')
   )));

Then after I check the request for a submit parameter

if($this->form->isValid(){
   loginUser();
}

It doesnt matter how short password, or fake email I put in, it fail ever.


You're not binding post data to the form.

You should put your form in a separate file, I'll assume you put it in lib/form/LoginForm.class.php.

In the form's configure() method create a name format: $this->widgetSchema->setNameFormat("login[%s]").

Your action's code:

$this->form = new LoginForm();
if ($request->isMethod("post")) {
  $this->form->bind($request->getParameter("login"));
  if ($this->form->isValid()) {
    //whatever
  }
}

Before creating an authentication and authorization system from scratch, have a look at the de-facto symfony standard of it: sfDoctrineGuardPlugin. It has a propel version as well, called sfGuardPlugin.

0

精彩评论

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