0

I am using Symfony 2.

The way my forms are working is like the following :

  • Forms are submitted in Ajax (JQuery)

  • If there are errors in my form, I receive an XML response with all error messages

    <errors>
    <error id="name">This field cannot be blank</error>
    <error id="email">This email address is not valid</error>
    <error id="birthday">Birthday cannot be in the future</error>
    </errors>

  • If there is no error in my form, I receive an XML response with redirect URL
    <redirect url="/confirm"></redirect>

  • My question is : how can I change "forever" the behavior of forms in Symfony 2 so that I could use a controller like the following :
    public function registerAction(Request $request)
    {
    $member = new Member();

    $form = $this->createFormBuilder($member)
    ->add('name', 'text')
    ->add('email', 'email')
    ->add('birthday', 'date')
    ->getForm();

    if($request->getMethod() == 'POST') {
    $form->bindRequest($request);

    if($form->isValid()) {
    // returns XML response with redirect URL
    }
    else {
    // returns XML response with error messages
    }
    }

    // returns HTML form
    }

Thanks for your help,

Regards,

2 Answers 2

1

A form handler is how I do it as well. Validate the form in the formHandler and create your json or xml response in the controller depending on the response from the formHandler.

<?php
namespace Application\CrmBundle\Form\Handler;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Application\CrmBundle\Entity\Note;
use Application\CrmBundle\Entity\NoteManager;

class NoteFormHandler
{
    protected $form;
    protected $request;
    protected $noteManager;

    public function __construct(Form $form, Request $request, NoteManager $noteManager)
    {
        $this->form = $form;
        $this->request = $request;  
        $this->noteManager = $noteManager;
    }

    public function process(Note $note = null)
    {
        if (null === $note) {
            $note = $this->noteManager->create();
        }

        $this->form->setData($note);

        if ('POST' == $this->request->getMethod()) {
            $this->form->bindRequest($this->request);

            if ($this->form->isValid()) {
                $this->onSuccess($note);

                return true;
            } else { 
                $response = array();
                foreach ($this->form->getChildren() as $field) {
                    $errors = $field->getErrors();
                    if ($errors) {
                        $response[$field->getName()] = strtr($errors[0]->getMessageTemplate(), $errors[0]->getMessageParameters());
                    }
                }

                return $response;
            }
        }

        return false;
    }

    protected function onSuccess(Note $note)
    {
        $this->noteManager->update($note);
    }
}

This only returns 1 error message per field but it does the trick for me.

Sign up to request clarification or add additional context in comments.

Comments

0

Consider encapsulating this logic in a "form handler" service, similar to what is done in FOSUserBundle:

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Form/Handler/RegistrationFormHandler.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.