Business

Technology

 

 

 

 

 

The lifeblood of our business is that R&D spend. There's nothing that flows through a pipe or down a wire or anything else. We have to continuously create new innovation that lets people do something they didn't think they could do the day before.

Steve Ballmer

Symfony: Unbound sfForm: Unexpected extra form field named

Looking for a Symfony Developer?

If you or your company are looking for a Symfony Developer contact us or read about our Symfony Development Service

Symfony: Unbound sfForm: Unexpected extra form field named key visual

 

Ever tried creating an unbound form in Symfony and received the "Unexpected extra form field named" error? This article shows you how to add extra fields to a Symfony sfForm. Safely!

In this use case we have an exiting model that describes an Article. We want to be able edit the Article and have the option to send and email to an address defined in a text box. However we don't want the email field to be bound to the form and we dont want to add the email address to the Article.

So we add a field to our form like so:

class ArticleForm extends BaseArticleForm
{
  public function configure()
  {
    $this->widgetSchema['email'] = new sfWidgetFormInputText();
  }
}

This displays our field nicely and we can enter the email address. However we get a an "Unexpected extra form field named" error. That is because as the form is being validated it finds the email field however no sfValidator to ensure that it is valid.

We fix this by adding an sfValidator* to check the field. We can add one of sfValidatorPass or better still sfValidatorEmail. This will fix the "Unexpected extra form field named" issue and keep Symfony happy

class ArticleForm extends BaseArticleForm
{
  public function configure()
  {
    $this->widgetSchema['email'] = new sfWidgetFormInputText();
    $this->validatorSchema['email'] = new sfValidatorPass();
  }
}

I hope that you find this helpful.

If you found this article useful, please be sure to check out the related articles below.

seddik commented:

it helped me thanx

andrew commented:

thanks for this wonderful post.

it help me a lot.

SoGua commented:

Nice one! And i face another problem. How can i reorder the form fields including these extra form fields?

Adam Pullen commented:

Hello SoGua,

If you are talking about the "presentation" of the fields, I would suggest that you use your HTML skills to lay the fields out how you want them.

However you can also use the sfForm::useFields method. By default it will order the fields in the order that you specify them in the array.

i.e.

$form->useFields(array('firstname', 'lastname', 'age'));

will present order them in 

1) firstname,

2) lastname,

3) age

I find this method of "useField" a bit bothersome as you always have to remember to include the _csrf_token, id and other fields. 

As this is a public method you are also free to call it from your action. Just remember if you do do this from your action you have to "setup" the form on both the "show form (executeEdit)" action and the "update form (executeEdit)" 

I hope that this helps

You can read the official documentation here:

http://www.symfony-project.org/tutorial/1_4/en/whats-new#chapter_a2fae23c9403b0e9ec99806fccf6b53e_sub_sfform_usefields

SoGua commented:

Thanks a lot!

David E. Narvaez commented:

Thanks, this saved me a lot of time.

votoan commented:

Thanks a lot!

mangel commented:

Good tip when is not required validate the field.

Thanks.

John commented:

Saved me the day! Thanks!

Samrat commented:

Thanks a lot!

Tapper commented:

In this snippet is also nicely explained how to prevent saving the extra value:

http://snippets.symfony-project.org/snippet/442

Basically, the way is overriding the updateYourFieldHereColumn() method.

Dave commented:

Thanks, this helped a lot.

noName commented:

This helped me a lot!!! Thanks!!!

Add a comment

*
*
Your email will not be published
*
*