You are on page 1of 1

Forms

Field class that converts submitted data to normalized values


FORM Form collection of fields that knows how to validate itself

COMPONENT Template
Domain objects
file that renders a form or a field in HTML
an object a form uses to populate default values and where submitted data is written

PHP
FIELDS BirthdayField
CheckboxField
FileField
HiddenField
PercentField
RepeatedField
// src/Acme/HelloBundle/Contact/ContactForm.php
namespace Acme\HelloBundle\Contact;
ChoiceField IntegerField TextField
CollectionField LanguageField TextareaField use Symfony\Component\Form\Form;
use Symfony\Component\Form\TextField;
CountryField LocaleField TimeField use Symfony\Component\Form\TextareaField;
DateField MoneyField TimezoneField use Symfony\Component\Form\CheckboxField;
DateTimeField NumberField UrlField
class ContactForm extends Form {
EntityChoiceField PasswordField protected function configure() {
PHP $this->add(new TextField('subject',
array( 'max_length' => 100, )));
use Symfony\Component\Form\TextField $this->add(new TextareaField('message'));
FIELD OPTIONS data
required $field = new TextField('name',
$this->add(new TextField('sender'));
$this->add(new CheckboxField('ccmyself',
disabled array( array( 'required' => false, )));
'data' => 'Custom default...', }
trim 'property_path' => 'token', }
property_path ));

Choice Max Time YAML


VALIDATOR Collection MaxLength True
# Acme/BlogBundle/Resources/config/validation.yml
Acme\BlogBundle\Author:
(constraints) Date Min Type properties:
firstName:
DateTime MinLength Url - NotBlank: ~
Email NotBlank Valid - MinLength: 3
False NotNull lastName:
- NotBlank: ~
File Regex - MinLength: 3

CUSTOM HTML form_enctype


form_errors
Outputs the enctype attribute of the form tag. Required for file uploads
Outputs the a <ul> tag with errors of a field or a form
(templates) form_label Outputs the <label> tag of a field
form_field Outputs HTML of a field or a form
form_hidden Outputs all hidden fields of a form

Twig PHP
echo $view['form']->render($form['title'],
{{ form_field(form.title, { 'class': 'important' }) }}
array('class' => 'important' ))

Twig PHP
{{ form_label(form.title) }} echo $view['form']->label($form['title']);

All labels and error messages are automatically internationalized


{# TwigBundle::form.html.twig #}
Twig
Helpers {% block errors %}
Twig Block PHP Template Name {% if errors %}
<ul>
errors errors FrameworkBundle:Form:errors.php {% for error in errors %}
hidden hidden FrameworkBundle:Form:hidden.php <li>{% trans error.0 with error.1 from
validators %}</li>
label label FrameworkBundle:Form:label.php {% endfor %}
render selects the template to render based on the </ul>
{% endif %}
underscored version of the field's class name {% endblock errors %}

Twig PHP
PROTOTYPING <form action="#" {{ form_enctype(form) }} <form action="#" <?php echo $view['form']->
method="post"> enctype($form) ?> method="post">
Render the form {{ form_field(form) }} <?php echo $view['form']->render($form) ?>
<input type="submit" /> <input type="submit" />
(all fields) </form> </form>

http://symfony.com http://andreiabohner.wordpress.com

You might also like