[solved] Changing Labels not Working

New topics about Symfony 2 should go here

Moderators: tiagojsag, dcobalt

[solved] Changing Labels not Working

Postby weiserc » Sun Jul 03, 2011 11:12 pm

Hi

I am getting into Symfony2. My problem at the moment is that my form is not outputting the labels I have specified.

I have created the following form class:

Code: Select all
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class CityType extends AbstractType
{
   public function buildForm(FormBuilder $builder, array $options) {
      $builder->add('name');
      $builder->add('postalCode');
      $builder->add('state');
      $builder->add('country');
   }
}


and twig template:

Code: Select all
   <form action="{{ path('manage_createCity') }}" method="post" {{ form_enctype(form) }}>
      <h2>Add a News City</h2>
       {{ form_errors(form) }}
   
       {{ form_row(form.name, {'label': 'city name: '}) }}
       {{ form_row(form.postalCode, {'label': 'postal code: '}) }}
       {{ form_row(form.state, {'label': 'state: '}) }}
       {{ form_row(form.country, {'label': 'country: '}) }}
   
       {{ form_rest(form) }}
   
       <input type="submit" value="save data" class="submit" />
   </form>


instead of outputting the label e.g. "city name: " it outputs the column name "name". Does someone have a idea why that is?

Thanks
Craig
Last edited by weiserc on Wed Jul 13, 2011 5:17 pm, edited 1 time in total.
Would you like to be a candidate in your own US presidential election? Check out http://www.yourelection.net/us_election_game.php (powered by symfony)
weiserc
Member
 
Posts: 43
Joined: Fri Dec 28, 2007 1:11 am

Re: Changing Labels not Working

Postby weiserc » Fri Jul 08, 2011 12:04 am

I just remembered to clear the cache. Unfortunately that didn't help :?~:-?~:???:

I do hope someone can help me solve this.
Would you like to be a candidate in your own US presidential election? Check out http://www.yourelection.net/us_election_game.php (powered by symfony)
weiserc
Member
 
Posts: 43
Joined: Fri Dec 28, 2007 1:11 am

Re: Changing Labels not Working

Postby Mr. Tool » Tue Jul 12, 2011 1:23 pm

$builder->add('postalCode', array('label' => 'Postal Code:'));

try this
Mr. Tool
Member
 
Posts: 72
Joined: Fri May 27, 2011 11:37 am

Re: Changing Labels not Working

Postby weiserc » Tue Jul 12, 2011 5:48 pm

Thanks for the tip, but that just gives me the following error:

Expected argument of type string or Symfony\Component\Form\FormTypeInterface, array given
500 Internal Server Error - UnexpectedTypeException
Would you like to be a candidate in your own US presidential election? Check out http://www.yourelection.net/us_election_game.php (powered by symfony)
weiserc
Member
 
Posts: 43
Joined: Fri Dec 28, 2007 1:11 am

Re: Changing Labels not Working

Postby xaav » Tue Jul 12, 2011 6:19 pm

Just use the following:

Code: Select all
City name: {{ form_widget(form.name) }}

'
It looks cleaner also.
xaav
Member
 
Posts: 97
Joined: Mon Jun 06, 2011 6:47 pm

Re: Changing Labels not Working

Postby weiserc » Tue Jul 12, 2011 11:55 pm

That would work, but it would mean that I would create all labels by hand. Is that really the best way?
Would you like to be a candidate in your own US presidential election? Check out http://www.yourelection.net/us_election_game.php (powered by symfony)
weiserc
Member
 
Posts: 43
Joined: Fri Dec 28, 2007 1:11 am

Re: Changing Labels not Working

Postby Mr. Tool » Wed Jul 13, 2011 9:39 am

äht fail.

use
$builder->add('postalCode', 'text', array('label' => 'Postal Code:'));
Mr. Tool
Member
 
Posts: 72
Joined: Fri May 27, 2011 11:37 am

Re: Changing Labels not Working

Postby weiserc » Wed Jul 13, 2011 5:16 pm

Thanks Mr. Tool that works. :)~:-)~:smile:
Would you like to be a candidate in your own US presidential election? Check out http://www.yourelection.net/us_election_game.php (powered by symfony)
weiserc
Member
 
Posts: 43
Joined: Fri Dec 28, 2007 1:11 am

Re: [NOTsolved] Changing Labels not Working

Postby darklow » Tue Jul 03, 2012 9:52 am

Just realized this syntax doesn't work with collection type.
Of course we can specify labels in buildForm() method, but does anyone have ideas, why specifying { label: 'label' } doesn't work in twig form_row method?

This is really important for me, because i can access entity object in twig, and need this to work:
Code: Select all
{{ form_row(item, { label: item.vars.value.getName() }) }}


Does anyone have ideas, how to specify custom label using form_row or form_widget for collection fields using Twig?
Thank you
darklow
Junior Member
 
Posts: 4
Joined: Tue Jul 03, 2012 9:45 am

Re: [solved] Changing Labels not Working

Postby trsteel88 » Sun Feb 17, 2013 11:32 am

darklow, did you find a solution for this?
trsteel88
Member
 
Posts: 64
Joined: Tue Jun 21, 2011 11:18 am


Return to General Symfony 2 discussion

Who is online

Users browsing this forum: Bing [Bot] and 6 guests