develup
develup

Reputation: 41

Symfony 2 - Form submission on enter in text input

How to I setup this simple form to submit after an enter occurs in the text field below?

public function indexAction(Request $request)
{
    // create a task and give it some dummy data for this example
    $task = new Task();
    $task->setTask('test');
    $task->setDueDate(new \DateTime('tomorrow'));

    $form = $this->createFormBuilder($task)
        ->add('task', 'text', array('label' => 'Enter State', 'attr' => array('class' => 'typeahead')))
        ->getForm();

Upvotes: 1

Views: 1387

Answers (1)

Santiag00
Santiag00

Reputation: 666

I'm afraid there is no option for that in Symfony http://symfony.com/doc/current/reference/forms/types/text.html

You could always add some JS code to your view to handle this:

document.getElementById("text_field_id").addEventListener("keydown", function(e) {
    if (!e) { var e = window.event; }
    e.preventDefault();

    // Enter is pressed
    if (e.keyCode == 13) { submitForm(); }
}, false);

Upvotes: 1

Related Questions