4
votes

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>
2
I want this behavior <input type="submit" id="id_submit" name="click to go next" class="submit"></input> rathen than <button type="submit" id="ponemonSurvey_submit" name="click here to go" class="submit"></input>Ali Hassan

2 Answers

1
votes

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

1
votes

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.