Country select box in symfony setWidgets

Posted on

For my register form was the requirement to select from which Country a user is.

Symfony have a nice feature called select_country_tag which I wanted to use in the $this->setWidgets() in the mysfGuardFormSignin.class.php the problem is that the select_country_tag gives not the array back.

After a little bit ‘reverse engineering’ the solution for me is like this

    $c = new sfCultureInfo(sfContext::getInstance()->getUser()->getCulture());
    $countries = $c->getCountries();

here the full class

less lib/form/mysfGuardFormSignin.class.php 
<?php

class mysfGuardFormSignin extends sfForm
{
  public function configure()
  {
    $c = new sfCultureInfo(sfContext::getInstance()->getUser()->getCulture());
    $countries = $c->getCountries();

    $this->setWidgets(array(
      'username' => new sfWidgetFormInput(),
      'email' => new sfWidgetFormInput(),
      'password' => new sfWidgetFormInput(array('type' => 'password')),
      'country'  => new sfWidgetFormSelect(array('choices' => $countries)),
    ));

    $this->setValidators(array(
      'username' => new sfValidatorString(),
      'email'    => new sfValidatorString(),
      'password' => new sfValidatorString(),
      'country'  => new sfValidatorString(),
    ));

    $this->validatorSchema->setPostValidator(new sfGuardValidatorUser());

    $this->widgetSchema->setNameFormat('signin[%s]');
  }
}
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s