Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

symfony - How to get form values in Symfony2 controller

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') {
        $form->bindRequest($request);
        $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 vendorsymfonysrcSymfonyComponentFormForm.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>
        <div class="level2">
            {{ form_row(form.pwd_hash) }}
            <div class="forget_pass"><a href="#" id="frgt">{% trans %}Forgot Password ?{% endtrans %}</a></div>
        </div>
        <input type="submit" class="submit" name="login" value/>
        <div class="clr"></div>
    </form>
</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:

site_user_login:
    pattern: /{_locale}/login
    defaults: {_controller: GDSiteBundle:SiteUser:login}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simply :

$data = $form->getData();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...