1

I'm new to Symfony2 and I'm now trying to build a form. I have a class Message and the data in the form should create a Message object.

My problem is that fields are not required, although I've specified it explicitly below in the controller when calling createFormBuilder.

The HTML5 validator for email is triggered however.

And the second problem is that the placeholder attribute is not working. It's just not added to the fields, although I've set it in the view.

  1. controller action :

    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. Message class:

    class Message
    {
       protected $name;
       protected $subject;
       protected $body;
       protected $email;
       + setters and getters here
    }
    
  3. form template

     {# 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('attributes') }} value="{{ value }}" class="field-email   form_field">       
     {% endblock %}
    
      {% block text_widget %}
        <input type="text" {{ block('attributes') }} value="{{ value }}" class="field-name form_field">
      {% endblock %}
    
      {% block textarea_widget %}
        <textarea name="field-message" {{ block('attributes') }}  cols="45" rows="5"  class="field-message form_field">{{ value }}</textarea>
      {% endblock %}
    
  4. the form view

         <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) }}
    
3
  • So just to clarify, you made you own form fields template? It all works as desired using the default templates? Commented Aug 24, 2013 at 14:02
  • I don't know if it works using the default template - I need a custom one so I haven't even tried using the default Commented Aug 24, 2013 at 14:19
  • Fair enough. I have never had much luck messing around with the field templates. Be curious to see the solution. And as @mansoulx's answer shows, it's not really obvious that you are asking about custom field templates. I almost posted the same sort of answer. Might consider reducing things to one field and making it clear what the actual question is. Commented Aug 24, 2013 at 14:24

2 Answers 2

1

You have an error in your form theming template. The block is called widget_attributes

{% block text_widget %}
    <input type="text" {{ block('widget_attributes') }} value="{{ value }}" class="field-name form_field">
{% endblock %}
Sign up to request clarification or add additional context in comments.

Comments

1

Put the required attribute at false for all non required columns, and add the attr attribute to add your placeholder

$form = $this->createFormBuilder($message)
    ->add('Name', 'text', array('required' => false))
    ->add('Email', 'email', array('required' => false, 'attr' => array('placeholder' => 'some text here')))
    ->add('Subject', 'text')
    ->add('Body', 'textarea')
    ->getForm();

3 Comments

that didn't make any difference. required fields are not detected when submitting the form and placeholders are not showing.
are you using a compatible HTML5 browser ? can you tell me what does you browser inspector shows in html output ? or CTRL + U (show source) in your browser !
latest version of Chrome. Like I said, email validator is triggered but not the required validator. That is because the 'required' attribute does not get added to the HTML input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.