4

How I can set button type as input tag in symfony2?

<input type="submit" id="id_submit" name="click to go next" class="submit"></input>

using symfony2 formType as public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder

    ->add('submit', 'button',array('attr' => array('class'=>'submit','type'=>'input')))
    ;
}

As it shows

<button type="submit" id="ponemonSurvey_submit" name="click here to go" class="submit"></input>
Ali Hassan
  • 607
  • 1
  • 11
  • 24

2 Answers2

1

If you want to add button to your forms, you can use the inputType submit (symfony2.3 and more) Documentation

$builder->add('save', 'submit', array(
    'attr' => array('class' => 'submit'),
    'label' => "click to go next"
));

You have also the reset and button inputType.

button html tag is more recommended than input tag, it's more flexible (you can use an image as a button for example).

If you really want a <input type="submit" rather than <button type="submit", you can add your own inputType to symfony as descripted there in the cookbook

Vivien
  • 1,159
  • 2
  • 14
  • 34
goto
  • 7,908
  • 10
  • 48
  • 58
  • I want this behavior rathen than – Ali Hassan Jan 30 '14 at 09:15
  • Thanks for your response, but your tag also showing me – Ali Hassan Jan 30 '14 at 09:17
  • Check my link to see the difference between the two of them. The button tag is more recommended than the input one, button is more flexible. http://stackoverflow.com/questions/3543615/difference-between-input-type-submit-and-button-type-submittext-button – goto Jan 30 '14 at 09:21
  • You are correct, but still I need to define input tag as submit in formtype. I dont want to show button tag as submit. – Ali Hassan Jan 30 '14 at 09:26
  • Kindly help me if you know how I can define in my formType. – Ali Hassan Jan 30 '14 at 09:27
1

From Symfony 2.3 and so on (doesn't work on earlier versions):

$builder->add('save', 'submit', array(
                'label' => 'Click to go next',
                'attr' => array(
                    'class' => 'the_class_you_want',
                )));

The Submit button has an additional method isClicked() that lets you check whether this button was used to submit the form. This is especially useful when a form has multiple submit buttons:

if ($form->get('save')->isClicked()) {
    // ...
}

Tip: you shouldn't put "click to go next" in the name attribute. This attribute is not aimed to receive human readable text. Use the 'label' option instead, like I wrote above.

Note: you can't change the css id attribute of an input in the FormBuilder. To do that, you have to use the template.

Starx
  • 77,474
  • 47
  • 185
  • 261
Jivan
  • 21,522
  • 15
  • 80
  • 131