1

我在此注册表单页面上使用安装了 Doctrine DBAL 的最新版本的 Silex(不带 .phar)。

如果我输入了无效的详细信息,它会返回到该表单作为例外。但是如果详细信息是有效的,而不是重定向到 /success/ 页面,它会再次返回相同的表单,就像什么都没发生一样。数据库没有收到任何条目,Apache 错误日志也没有报告任何问题。

<?php

// ...

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Yaml\Parser;
use Silex\Provider\FormServiceProvider;
use Symfony\Component\Validator\Constraints as Assert;

// ...

$app->match('/signup/', function(Request $request) use($app, $page) { 

  $data = array('name' => 'John','surname' => 'Smith','telephone' => '00011112222');

  $form = $app['form.factory']->createBuilder('form', $data)
    ->add('name', 'text', array(
        'constraints' => array(
            new Assert\NotBlank(), 
            new Assert\MinLength(2),
        ),
        'invalid_message' => 'First name is too short, It should have 2 characters or more',
    ))
    ->add('surname', 'text', array(
        'constraints' => array(
            new Assert\NotBlank(), 
            new Assert\MinLength(2),   
        ),
        'invalid_message' => 'Surname is too short, It should have 2 characters or more',
    ))
    ->add('telephone', 'text', array(
        'constraints' => array(
            new Assert\NotBlank(), 
            new Assert\Regex("/[\d\-\ ]+/"),
            new Assert\MinLength(11),
        ),
        'invalid_message' => 'Please enter a valid phone number. Must have 11 digits and may contain dashes (-) or spaces.',
    ))
    ->getForm();

  if ('POST' == $request->getMethod()) {
     $form->bindRequest($request);

     if ($form->isValid()) {

        $data = $form->getData();

        $app['db']->insert('signups', array(
            'forename'  => $data['name'],
            'surname'   => $data['surname'],
            'telephone' => $data['telephone']
        ));

        return $app->redirect('/success/');

     }
  }

  $page['form'] = $form->createView();    

  return $app['twig']->render('signup.html.twig', $page);

}, 'POST|GET');



$app->match('/success/', function() use($app, $page) { 

    return $app['twig']->render('success.html.twig', $page);

}, 'POST|GET');

和树枝形式

<form class="well" action="/signup/" method="post">
<fieldset>
    <div class="control-group">
        {% if (form_errors(form.name)) or (form_errors(form.surname)) or (form_errors(form.telephone)) %}
                <div class="error-in-form">
                    <h5 style="color:#c00;">Please review the following errors:</h5>
                    <br />
                    <div>
                        <p class="help-msg"><span>First Name: </span></p>
                        <div class="error-msg">{{ form_errors(form.name) }}</div>
                        <div class="clearfix"></div>
                    </div>

                    <div>
                        <p class="help-msg"><span>Surname: </span></p>
                        <div class="error-msg">{{ form_errors(form.surname) }}</div>
                        <div class="clearfix"></div>
                    </div>

                    <div>
                        <p class="help-msg"><span>Telephone: </span></p>
                        <div class="error-msg">{{ form_errors(form.telephone) }}</div>
                        <div class="clearfix"></div>
                    </div>
                </div>
            {% endif %}

        {{ form_label(form.name) }}
        <div class="controls">

            {{ form_widget(form.name, { 'attr': { 'class': 'input-medium' } } ) }}
            {{ form_widget(form.surname, { 'attr': { 'class': 'input-medium' } } ) }}

        </div>
    </div>
    <div class="control-group">
        {{ form_label(form.telephone) }}
        <div class="controls">
            {{ form_widget(form.telephone, { 'attr': { 'class': 'input-fullwidth' } } ) }}
        </div>
    </div>
    <p class="tnc">If you accepts the terms and conditions below, please proceed.</p>
    <button id="big-red-button" type="submit" class="btn btn-danger btn-fullwidth">Submit &gt;</button>
</fieldset>
</form>
4

1 回答 1

1

好吧,看起来我忘了添加{{ form_rest }}到 Twig 表单模板中。

由于我也没有包含{{ form_errors(form) }}任何内容,因此我看不到有关缺少 CSFP 令牌的错误,这是一个添加到表单中的隐藏字段。

于 2012-06-28T09:49:41.917 回答