2013-08-24 44 views
0

提交下面的表單時,當調用$form->getData();時,作爲textarea的字段「Body」未映射到$ message對象。symfony2:表單沒有正確提交所有值

我可以看到在$ request.parameters有兩個值:

- 形式,它是含有以下形式的數組中的值,但textarea的值丟失。

-Body,它是一個包含textarea值的字符串。

爲什麼Body不是表單對象的一部分?

  1. 控制器動作:

    class ContactController extends Controller { 
    
    public function indexAction(Request $request) { 
        $message = new Message(); 
    
        $form = $this->createFormBuilder($message) 
         ->add('Name', 'text', array('required' => true)) 
         ->add('Email', 'email') 
         ->add('Subject', 'text') 
         ->add('Body', 'textarea') 
         ->getForm(); 
    
        $form->handleRequest($request); 
    
        if ($form->isValid()) { 
        // data is an array with "name", "email", and "message" keys 
        $data = $form->getData(); 
        } 
    
        return $this->render('PhotographPhotoBundle:Contact:index.html.twig', array('form' => $form->createView())); 
    } 
    } 
    
  2. 消息類別:

    class Message 
    { 
        protected $name; 
        protected $subject; 
        protected $body; 
        protected $email; 
        + setters and getters here 
    } 
    
  3. 形式模板

    {# src/Photograph/PhotoBundle/Resources/views/Form/fields.html.twig #} 
    
        {% block field_row %} 
        {% spaceless %} 
        {{ form_errors(form) }} 
        {{ form_widget(form) }} 
        {% endspaceless %} 
        {% endblock field_row %} 
    
    {% block email_widget %} 
        <input type="email" {{ block('widget_attributes') }} value="{{ value }}" class="field-email form_field">  
    {% endblock %} 
    
        {% block text_widget %} 
        <input type="text" {{ block('widget_attributes') }} value="{{ value }}" class="field-name form_field"> 
        {% endblock %} 
    
        {% block textarea_widget %} 
        <textarea name="field-message" {{ block('widget_attributes') }} cols="45" rows="5" class="field-message form_field">{{ value }}</textarea> 
        {% endblock %} 
    
  4. 形式視圖

     <form action="{{ path('PhotographPhotoBundle_contact') }}" method="post" class="feedback_form" > 
    
               {{ form_errors(form) }}          
    
               {{ form_widget(form.Name) }} 
                 <div class="clear"></div> 
               {{ form_widget(form.Email, { 'attr': {'placeholder': 'email' }}) }} 
                 <div class="clear"></div> 
               {{ form_widget(form.Subject, { 'attr': {'placeholder': 'sujet' }}) }} 
                 <div class="clear"></div> 
               {{ form_widget(form.Body, { 'attr': {'placeholder': 'message' }}) }} 
                 <div class="clear"></div>  
               <input value="envoyer votre message" type="submit" class="feedback_go" /> 
                 <div class="ajaxanswer"></div> 
                {{ form_end(form) }} 
    
+2

驗證getBody/setBody是否已正確定義。我想這可能是一個大小寫敏感的問題。嘗試 - >添加('身體','textarea')但值得懷疑。否則,將textarea更改爲文本。如果文本能夠正常工作,那麼您又有模板問題。你在textarea模板中有一個name屬性的事實有點可疑。 – Cerad

+0

是的,它似乎與textarea有關,因爲如果我將其更改爲文本,它將起作用。我想知道問題是什麼...... – Sam

+0

其實你是對的,這是導致問題的名稱屬性。我拿出來了,現在沒事了。請回答問題,以便將其標記爲答案。你只在這裏評論。謝謝 – Sam

回答

1

此:

{% block textarea_widget %} 
    <textarea name="field-message" {{ block('widget_attributes') }} cols="45" rows="5" class="field-message form_field">{{ value }}</textarea> 
{% endblock %} 

應該是:

{% block textarea_widget %} 
    <textarea {{ block('widget_attributes') }} cols="45" rows="5" class="field-message form_field">{{ value }}</textarea> 
{% endblock %} 

換句話說,下降的name屬性。我第一次成功地調試了其中的一個模板!

+0

不錯的一個:)謝謝! – Sam

1

FormBuilder::add()方法的第一parameter是字段的名稱。它應該與你的課程的屬性相匹配。我不太確定爲什麼它只適用於Body部分。將表單類型更改爲:

$form = $this->createFormBuilder($message) 
    ->add('name', 'text', array('required' => true)) 
    ->add('email', 'email') 
    ->add('subject', 'text') 
    ->add('body', 'textarea') 
    ->getForm(); 

請注意小寫字母,以便它們與您的實體匹配。如果你想改變一個字段的標籤,你可以將它設置了config數組中:

->add('body', 'textarea', array(
     'attributes' => array(
       'label' => 'Body' 
     ) 
    ) 
    ) 

或者只是正確地更改您的模板:

<label>Body</label> 
{{ form_widget(form.Body, { 'attr': {'placeholder': 'message' }}) }} 

一個更明確的說明,請參見the docs

+0

謝謝,但我已經將第一個參數更改爲小寫,以便它們匹配課程中的屬性,並且我也在視圖中更改了它。但沒有運氣。它只適用於將textarea更改爲文本的情況。 – Sam