sfForm and Symfony 1.0.x

This article has moved to this location.

sfFrom was introduced since symfony 1.1, and has been improved in symfony 1.2 >. Can we use this form system in symfony 1.0?. Of course we can, because symfony form is independent.
What files do we need? Here are the list of files that need to be copied (from symfony 1.2.x lib folder) into lib folder:

  1. form folder
  2. widget folder
  3. validator folder
  4. sfCallable.class.php (in the ‘util’ folder)

In this tutorial, i’ll take form login as an example.
First, create LoginForm.class.php and myValidatorUser.class.php.

class LoginForm extends sfForm
  public function configure()
       'username'=>new sfWidgetFormInput(),
       'password'=>new sfWidgetFormInput(array('type'=>'password')),
      'username'=>new sfValidatorString(),
      'password'=>new sfValidatorString(),
    $this->validatorSchema->setPostValidator(new myValidatorUser());
class myValidatorUser extends sfValidatorBase
  public function configure($options = array(), $messages = array())
    $this->addOption('username_field', 'username');
    $this->addOption('password_field', 'password');
    $this->addOption('throw_global_error', false);
    $this->setMessage('invalid', 'Sorry, your username does not exist');

  protected function doClean($values)
    $username = isset($values[$this->getOption('username_field')]) ? $values[$this->getOption('username_field')] : '';
    $password = isset($values[$this->getOption('password_field')]) ? $values[$this->getOption('password_field')] : '';

    if ($user = ApplicationUserPeer::retrieveByUsername($username))
      if ($user->checkPassword($password))
         return array_merge($values, array('user' => $user));
    if ($this->getOption('throw_global_error'))
      throw new sfValidatorError($this, 'invalid');
    throw new sfValidatorErrorSchema($this, array($this->getOption('username_field') => new sfValidatorError($this, 'invalid')));

In your action class:

public function executeLogin()
   $this->form = new LoginForm();
    if ($this->getRequest()->getMethod() == sfRequest::POST)
      if ($this->form->isValid())
        $values = $this->form->getValues();
    if ($this->getUser()->isAuthenticated()) { 		
      return $this->forward('yourmodule', 'youraction');		

In your loginSuccess.php template

<?php echo form_tag('yourmodule/login')?>
    <?php echo $form ?>   
    <?php echo submit_tag('Submit') ?>

In the next article i’ll show you how to use built-in propel widgets in symfony 1.0.x.


One response to “sfForm and Symfony 1.0.x

  1. Pingback: Using symfony built-in propel widgets in symfony 1.0 « Just Blog, No More…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: