Business
Technology
- Administration
- Android
- Apache Solr
- API
- Development Principles
- Django
- Eclipse
- Hibernate
- Javascript/jQuery
- Microsoft .NET
- Microsoft Office
- Patterns
- PHP
- Social Media
- Spring 3.0
- Spring Roo
- Symfony
- Symfony: Doctrine
- Symfony: View
- Symfony: Web Services
The Internet has been the most fundamental change during my lifetime and for hundreds of years. Someone the other day said, "It's the biggest thing since Gutenberg," and then someone else said "No, it's the biggest thing since the invention of writing."
Rupert Murdoch
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
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.
Add a comment

seddik commented:
it helped me thanx
andrew commented:
thanks for this wonderful post.
it help me a lot.
SoGua commented:
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:
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!