Symfony\Component\Form\Form::bindRequest PHP Method

bindRequest() public method

If the request method is POST, PUT or GET, the data is bound to the form, transformed and written into the form data (an object or an array).
public bindRequest ( Request $request ) : Form
$request Symfony\Component\HttpFoundation\Request The request to bind to the form
return Form This form
    public function bindRequest(Request $request)
    {
        // Store the bound data in case of a post request
        switch ($request->getMethod()) {
            case 'POST':
            case 'PUT':
                $data = array_replace_recursive(
                    $request->request->get($this->getName(), array()),
                    $request->files->get($this->getName(), array())
                );
                break;
            case 'GET':
                $data = $request->query->get($this->getName(), array());
                break;
            default:
                throw new FormException(sprintf('The request method "%s" is not supported', $request->getMethod()));
        }

        return $this->bind($data);
    }

Usage Example

 /**
  * Processes the form with the request
  *
  * @param Form $form
  * @return Message|false the sent message if the form is bound and valid, false otherwise
  */
 public function process(Form $form)
 {
     if ('POST' !== $this->request->getMethod()) {
         return false;
     }
     $form->bindRequest($this->request);
     if ($form->isValid()) {
         return $this->processValidForm($form);
     }
     return false;
 }