简体   繁体   中英

Symfony2: HTML5 attributes for generated form

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) }} 

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();

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM