Symfony Forms Tutorial: Quick Registration Form

I was initially skeptical about these numerous form frameworks that are coming out, I would initially opt to create forms and validations the old fashion way, but after giving symfony forms a shot; I can honestly say i’m converted! Lets start the tutorial by creating a basic registration form with a captcha. I would assume you know the basics of symfony if not go to their website (http://www.symfony-project.org) and check out their tutorial.

After setting up the project, app and modules we will install the plugin sfFormExtraPlugin so we can use ReCaptcha:

symfony plugin:install sfFormExtraPlugin

After that, go to http://www.recaptcha.net and register to acquire a public and private key; we’ll use that later. Now lets start creating the form class

class RegistrationForm extends sfForm {
const PUBLIC_KEY = 'PASTE YOUR PUBLIC KEY FROM RECAPTCHA';
const PRIVATE_KEY = 'PASTE YOUR PRIVATE KEY FROM RECAPTCHA';

public function configure() {
$this->setWidgets(array(
'username'=>new sfWidgetFormInput(array(), array('size'=>32)),
'password'=>new sfWidgetFormInputPassword(array(), array('size'=>32)),
'confirm_password'=>new sfWidgetFormInputPassword(array(), array('size'=32)),
'email'=>new sfWidgetFormInput(array(), array('size'=>32)),
'captcha'=>new sfWidgetFormReCaptcha(array(
'public_key'=>self::PUBLIC_KEY
))
));

$this->widgetSchema->setNameFormat('register[%s]'); //this acts as a namespace and also will be use later for binding POST

$this->setValidators(array(
'username'=>new sfValidatorString(array('max_length'=>50), array('max_length'=>'Max length of username is 50')),
'password'=>new sfValidatorString(),
'confirm_password'=>new sfValidatorString(),
'email'=>new sfValidatorEmail(),
'captcha'=>new sfValidatorReCaptcha(array('private_key' => self::PRIVATE_KEY), array('captcha' => 'Didn\'t match captcha'))
));

$this->validatorSchema->setPostValidator(new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 'confirm_password', array(), array('invalid' => 'Passwords do not match')));

$this->widgetSchema->setLabels(array(
'username'=>'Username',
'password'=>'Password',
'confirm_password'=>'Confirm Password',
'email'=>'Email Address',
'captcha'=>'Captcha'
));
}
}

The code in your actions.class.php would be as follows

public function executeRegister(sfWebRequest $r) {
$this->form = new RegistrationForm();
if($r->isMethod('post')) {
$captcha = array(
'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
'recaptcha_response_field'  => $request->getParameter('recaptcha_response_field'),
);

$this->form->bind(array_merge($request->getParameter('register'), array('captcha' => $captcha)));
if($this->form->isValid()) {
$values = $this->form->getValues(); //associative array of values
//you can access the POST data like $values['username'] or $values['email']
//do database insertion or other processsing here
}
}
}

And lastly displaying the form in your corresponding template would be as follows

<?php echo $form ?>

In the next article, we would be discussing how to customize your form layout.

  • Share/Bookmark
3 Comments

3 Comments

  1. Hi,

    I followed everything and still can’t get it working. All I get on form submission is a blank page. The other validations on the page also seem to have stopped working. I am getting frustrated with the lack of proper documentation of plugins in symfony. Here is my site I put up in a couple of weeks: http://www.scikappa.com and the link with recaptcha http://www.scikappa.com/school/new. Any pointers?

    ~jayz

  2. Are you using symfony in development mode? Usually a blank page means an error, so make sure you have error reporting turned on in php/symfony. This can be found in the php.ini for php and settings.yml in symfony for the particular app.

  3. nice tutorials jm… 1 click for u jejeje ^_^

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>