Skip to content

Instantly share code, notes, and snippets.

@paulchubatyy
Created February 2, 2014 16:42
Show Gist options
  • Save paulchubatyy/8771166 to your computer and use it in GitHub Desktop.
Save paulchubatyy/8771166 to your computer and use it in GitHub Desktop.
One form to hande several method submission at a time.
<?php
/**
* Class HttpMethodBasedRequestHandler
* @package Citylance\FrameworkBundle\Form\Extension\HttpFoundation
* @author Paul Chubatyy <[email protected]>
*/
namespace Citylance\FrameworkBundle\Form\Extension\HttpFoundation;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\RequestHandlerInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\HttpFoundation\Request;
class HttpMethodBasedRequestHandler implements RequestHandlerInterface
{
/**
* @var array
* Form methods handled
*/
private $methods = array('POST', 'PUT', 'PATCH');
/**
* Set the allowed methods for form submission
* @param $methods
* @return $this
*/
public function setAllowedMethods($methods)
{
if (is_string($methods)) {
$methods = array($methods);
}
$this->methods = $methods;
return $this;
}
/**
* Submits a form if it was submitted.
*
* @param FormInterface $form The form to submit.
* @param mixed $request The current request.
* @throws UnexpectedTypeException
*/
public function handleRequest(FormInterface $form, $request = null)
{
if (!$request instanceof Request) {
throw new UnexpectedTypeException($request, 'Symfony\Component\HttpFoundation\Request');
}
$name = $form->getName();
$method = $request->getMethod();
if (!in_array($method, $this->methods)) {
return;
}
if ('GET' === $method) {
if ('' === $name) {
$data = $request->query->all();
} else {
// Don't submit GET requests if the form's name does not exist
// in the request
if (!$request->query->has($name)) {
return;
}
$data = $request->query->get($name);
}
} else {
if ('' === $name) {
$params = $request->request->all();
$files = $request->files->all();
} elseif ($request->request->has($name) || $request->files->has($name)) {
$default = $form->getConfig()->getCompound() ? array() : null;
$params = $request->request->get($name, $default);
$files = $request->files->get($name, $default);
} else {
// Don't submit the form if it is not present in the request
return;
}
if (is_array($params) && is_array($files)) {
$data = array_replace_recursive($params, $files);
} else {
$data = $params ? : $files;
}
}
// Don't auto-submit the form unless at least one field is present.
if ('' === $name && count(array_intersect_key($data, $form->all())) <= 0) {
return;
}
$form->submit($data, 'PATCH' !== $method);
}
}
<?php
namespace /* you should know your namespaces */;
use Citylance\FrameworkBundle\Form\Extension\HttpFoundation\HttpMultipleMethodRequestHandler;
class SomethingElseType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$requestHandler = new HttpMultipleMethodRequestHandler;
$requestHandler->setAllowedMethods(array('POST', 'PUT', 'PATCH'));
$builder->setRequestHandler($requestHandler);
$builder->add('one')
->add('two')
->add('three')
->add('four')
->add('five')
->add('six');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment