このサインアップ フォーム ページで、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 ></button>
</fieldset>
</form>