SF2显示表单错误,无需提交

时间:2013-07-21 12:00:29

标签: forms symfony-2.3

我创建了一个标识表单,但即使没有提交表单,也会显示错误消息“Bad login”。

我使用app / config / security.yml的帐户来检查表单。

我的控制器:

public function loginAction() {

    $request = $this->getRequest();
    $session = $request->getSession();
    $error = null;

    if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = "Oop's ! Bad login";
    } else {
        $error = "Oop's ! Bad login";
        $session->remove(SecurityContext::AUTHENTICATION_ERROR);
    }

    return $this->render('CmsCmsBundle:Main:Log/login.html.twig', array(
        'last_username' => $session->get(SecurityContext::LAST_USERNAME),
        'error'         => $error,
    ));
}

我的观点:

{% if error %}
<div class="flash flash-error">
{{ error }}
</div> {% endif %}
<form action="{{ path('cms_cms_logcheck') }}" method="post">
            <div class="form-align">
                <label for="username">Login :</label>
                <input type="text" id="username" name="_username" value="{{ last_username }}" required/>
            </div>

            <div class="form-align">
                <label for="password">Pwd :</label>
                <input type="password" id="password" name="_password" required/>
            </div>

            <input type="hidden" name="_target_path" value="/private" />
            <input type="submit" class="btn btn-primary" value="Connexion">
        </form>

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

如果请求的方法=='POST'

,您应该首先检查请求方法并检查数据