2012-06-26 21 views
1

我在這個註冊表單頁面上使用了安裝Doctrine DBAL的最新版本的Silex(沒有.phar)。Symfony/Silex:表單提交什麼都不做(沒有錯誤,沒有任何返回)

如果我輸入無效的詳細信息,它會返回到例外的表格。但是如果細節是有效的,而不是重定向到/ 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> 

回答

1

嘛,貌似我忘了補充{{ form_rest }}到嫩枝表單模板。

既然我還沒有包含{{ form_errors(form) }},我看不到有關缺少CSFP標記的錯誤,這是一個隱藏字段,它被添加到表單中。

相關問題