Take the 2-minute tour ×

I am using a login form on Symfony2 with the following controller code

public function loginAction(Request $request)
    $user = new SiteUser();
    $form = $this->createForm(new LoginType(), $user);

    if ($request->getMethod() == 'POST') {
        $data = $form->getValues();
        // Need to do something with the data here

    return $this->render('GDSiteBundle::header.html.twig', array('form' => $form->createView()));

But I am getting the following warning:

Warning: array_replace_recursive() [function.array-replace-recursive]: Argument #1 is not an array in \vendor\symfony\src\Symfony\Component\Form\Form.php line 593 500 Internal Server Error - ErrorException

Can someone help me understand whats incorrect, and how I can fix it? Thanks.

Update: The twig file is something like this:

<div class="form">
                {{ form_errors(form) }}
                <form action="{{ path('site_user_login') }}" method="POST" {{ form_enctype(form) }}>
                <div class="level1">
                    {{ form_row(form.username) }}
                    <a href="javascript:void(0)" id="inscription">{% trans %}Registration{% endtrans %}</a>
                <div class="level2">
                    {{ form_row(form.pwd_hash) }}
                    <div class="forget_pass"><a href="#" id="frgt">{% trans %}Forgot Password ?{% endtrans %}</a></div>
                <input type="submit" class="submit" name="login" value/>
                <div class="clr"></div>

Here is the function in the Form's Type

public function buildForm(FormBuilder $builder, array $options)
    $builder->add('username', 'text', array('label' => 'Username : '));
    $builder->add('pwd_hash','password', array('label' => 'Password : '));

Here is the route:

    pattern: /{_locale}/login
    defaults: {_controller: GDSiteBundle:SiteUser:login}
share|improve this question
add comment

7 Answers

up vote 27 down vote accepted

Simply :

$data = $form->getData();
share|improve this answer
Nice Answer - RE: below answers. Symfony often allows for many ways to get similar results. It's always good to research "Best Practices" when looking for SF solutions. –  Justin Pfister Dec 12 '12 at 16:14
add comment

None of the above worked for me. This works for me:

$username = $form["username"]->getData();
$password = $form["password"]->getData();

I hope it helps.

share|improve this answer
Hmmm strange, i got it working with $request->request->get('username'); Did you pass the request object as a parameter to the action? And you need to include the Request Class too in the above. –  VishwaKumar Apr 6 '12 at 5:53
add comment

I think that in order to get the request data, bound and validated by the form object, you must use this command :

$form->getClientData(); // Deprecated since version 2.1, to be removed in 2.3.
share|improve this answer
add comment

In symfony 2 ( to be more especific, the 2.3 version ) you can get a data of an field by

$var = $form->get('yourformfieldname')->getData();

or you can get all data sended

$data = $form->getData();

where '$data' is any array to store the fields e values of your form.

share|improve this answer
add comment

If you're using Symfony 2 security management, you don't need to get posted values, you only need to manage form template (see documentation).

If you aren't using Symfony 2 security management, I advise you strongly to use it. If you don't want to or if you can't, can you give us the LoginType's sources ?

share|improve this answer
Thanks @BlackCharly I am beginning to read the documentation. I have also updated the code to give the reader complete clarity with the same. Appreciate your help. –  VishwaKumar Jan 24 '12 at 13:43
add comment

Supposing you are in a controller (your class does extends Controller right?), you can use :


instead of using a injected parameter Request $request. Might solve the problem as the bindRequest method is causing the error.

share|improve this answer
It doesn't solve the issue. However if I refresh the error page, it renders the partial template defined. –  VishwaKumar Jan 24 '12 at 13:49
`Yes because, you are not send data via post again. It is the form submit that is failing. –  i.am.michiel Jan 24 '12 at 14:06
But i have specified method="POST" in the twig template. This is correct right? Or am i missing something? –  VishwaKumar Jan 24 '12 at 14:09
add comment

I got it working by this:

    $username = $request->request->get('username');
    $password = $request->request->get('password');

    // Do something with the post data

You need to have the Request $request as a parameter in the function too! Hope this helps.

share|improve this answer
add comment

Your Answer


