This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Adding new field to a form

Discussion relating to version 1.3.x and 1.4.x

Adding new field to a form

by AdamGilliver » Tue Apr 29, 2014 1:03 pm

Hi,
I am new to Symfony, but I've taken over maintenance of a pre-existing system built using Symfony as part of my job.

I am currently trying to add an extra field to the User form, so the user can choose which currency is shown within the rest of the system.
I have managed to add a new drop-down box and set it up to validate the entry, but it doesn't seem to be updating the database with this new field when the form is submitted.

This is the part of the form setup function, all I've changed is to add in an array with the currencies, and add in the currency widget and validation:
Code: Select all
protected static $currencyChoices = array('pound' => 'Pound', 'euro' => 'Euro', 'dollar' => 'Dollar');

  public function setup()
  {
    $this->setWidgets(array(
      'id'             => new sfWidgetFormInputHidden(),
      'customer_id'    => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Customer'), 'add_empty' => true)),
      'name'           => new sfWidgetFormInputText(),
      'email'          => new sfWidgetFormInputText(),
     'currency'       => new sfWidgetFormSelect(array('choices' => self::$currencyChoices)),
      'password_sha1'  => new sfWidgetFormInputText(),
      'password_salt'  => new sfWidgetFormInputText(),
      'is_super_admin' => new sfWidgetFormInputCheckbox(),
      'created_at'     => new sfWidgetFormDateTime(),
      'updated_at'     => new sfWidgetFormDateTime(),
    ));

    $this->setValidators(array(
      'id'             => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'customer_id'    => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Customer'), 'required' => false)),
      'name'           => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'email'          => new sfValidatorString(array('max_length' => 255, 'required' => false)),
     'currency'       => new sfValidatorChoice(array('choices' =>array_keys(array('pound' => 'Pound', 'euro' => 'Euro', 'dollar' => 'Dollar')), 'required' => false)),   
      'password_sha1'  => new sfValidatorString(array('max_length' => 40, 'required' => false)),
      'password_salt'  => new sfValidatorString(array('max_length' => 32, 'required' => false)),
      'is_super_admin' => new sfValidatorBoolean(array('required' => false)),
      'created_at'     => new sfValidatorDateTime(),
      'updated_at'     => new sfValidatorDateTime(),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorDoctrineUnique(array('model' => 'User', 'column' => array('email')))
    );
    $this->widgetSchema->setNameFormat('user[%s]');
    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
    $this->setupInheritance();
    parent::setup();
  }


I've also updated the schema.yml file to include the currency option:
Code: Select all
User:
  actAs: { Timestampable: ~ }
  columns:
    customer_id:                 { type: integer() }
    name:                        { type: string(255) }
    email:                       { type: string(255), unique: true }
    currency:                    { type: string(255) }
    password_sha1:               { type: string(40) }
    password_salt:               { type: string(32) }
    is_super_admin:              { type: boolean(), notnull: true, default: false }
  relations:
    Customer:                    { onDelete: CASCADE, local: customer_id, foreign: id, type: one }


I've checked the SQL queries using the dev toolbar, and there is no mention of the currency there.

I'm not sure what I'm missing, and I've looked around on the net, but I can't really find much to explain what might be wrong.

Any help would be greatly appreciated.

Thanks.
AdamGilliver
Junior Member
 
Posts: 2
Joined: Tue Apr 29, 2014 12:48 pm

Re: Adding new field to a form

by AdamGilliver » Mon May 12, 2014 11:55 am

This problem has been resolved.

All I needed to do was re-run the command to build the model and sql, and it now works correctly.
AdamGilliver
Junior Member
 
Posts: 2
Joined: Tue Apr 29, 2014 12:48 pm