Symfony2: HTM5 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.
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()));
}
}
Message class:
class Message
{
protected $name;
protected $subject;
protected $body;
protected $email;
+ setters and getters here
}
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 %}
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) }}
No comments:
Post a Comment