Symfony2. Form and Validation
Upcoming SlideShare
Loading in...5
×
 

Symfony2. Form and Validation

on

  • 3,071 views

Symfony2. Form and Validation

Symfony2. Form and Validation

Statistics

Views

Total Views
3,071
Views on SlideShare
3,067
Embed Views
4

Actions

Likes
0
Downloads
18
Comments
0

2 Embeds 4

http://symfony2developer.com 3
http://ipv6.symfony2developer.com 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Symfony2. Form and Validation Symfony2. Form and Validation Presentation Transcript

    • Symfony2 Form and Validation
    • Creating an Entity Class // src/Volcano/VideostatusBundle/Entity/Clip.php namespace VolcanoVideostatusBundleEntity; class Clip { protected $id; protected $url; protected $timeStart; protected $timeFinish; }
    • Creating a Simple Form: Controller class DefaultController extends Controller { public function newAction(Request $request) { $clip = new Clip(); //... $form = $this->createFormBuilder($clip) ->add('url', 'text') ->add('timeStart', 'integer') ->add('timeFinish', 'integer') ->add('save', 'submit') ->getForm(); return $this->render('VolcanoideasVideoBundle:Default:new.html.twig', array( 'form' => $form->createView(), )); } }
    • Creating a Simple Form: Template {% extends "AcmeDemoBundle::layout.html.twig" %} {% block content %} <h1 class="title">Form demo</h1> {{ form(form) }} {% endblock %}
    • Creating a Simple Form: Template
    • Creating a Simple Form: Controller class DefaultController extends Controller { public function newAction(Request $request) { $clip = new Clip(); $form = $this->createFormBuilder($clip) //... ->getForm(); $form->handleRequest($request); if ($form->isValid()) { // perform some action, such as saving the task to the database return $this->redirect($this->generateUrl('clip_success')); } // } }
    • Validation use SymfonyComponentValidatorConstraints as Assert; class Clip { /** * @AssertNotBlank(message="Enter URL") * @AssertUrl(message="Enter valid URL", protocols={"http", "https"}) */ protected $url; /** * @AssertNotBlank(message="Enter Start Time") * @AssertType(type="integer") */ protected $timeStart; }
    • Validation service $clip = new Clip(); //... $validator = $this->get('validator'); $errors = $validator->validate($clip); if (count($errors) > 0) { return new Response(print_r($errors, true)); } else { return new Response('The clip is valid!'); }
    • Creating Form Classes // src/Volcano/VideoBundle/Form/Type/ClipType.php namespace VolcanoVideoBundleFormType; use SymfonyComponentFormAbstractType; use SymfonyComponentFormFormBuilderInterface; class ClipType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('url','text'); $builder->add('timeStart', 'integer'); //... } public function getName() { return 'task'; } }
    • Creating a Simple Form: Controller class DefaultController extends Controller { public function newAction(Request $request) { $clip = new Clip(); $form = $this->createForm(new ClipType(), $clip); $form->handleRequest($request); if ($form->isValid()) { // perform some action, such as saving the task to the database return $this->redirect($this->generateUrl('clip_success')); } // } }
    • Creating Form Classes // src/Volcano/VideoBundle/Form/Type/ClipType.php public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'VolcanoVideoBundleEntityClip', )); }
    • Creating a Simple Form: Template {% extends "AcmeDemoBundle::layout.html.twig" %} {% block content %} <h1 class="title">Form demo</h1> {{ form(form) }} {% endblock %}
    • Creating a Simple Form: Template
    • Creating a Simple Form: Template <form method="post" action=""> <div id="form"> <div> <label for="form_url" class="required">Url</label> <input type="text" id="form_url" name="form[url]" required="required"> </div> <div>...</div> <div> <button type="submit" id="form_save" name="form[save]">Save</button> </div> <input type="hidden" id="form__token" name="form[_token]" value=" f9fa3d38371a9344a906049a1ab0bdda5f5374a3"> </div> </form>
    • Rendering a Form {{ form_start(form) }} {{ form_errors(form) }} {{ form_row(form.url) }} {{ form_row(form.timeStart) }} {{ form_row(form.timeFinish) }} {{ form_row(form.save) }} {{ form_end(form) }}
    • Rendering a Form: form_row {{ form_label(form.url) }} {{ form_errors(form.url) }} {{ form_widget(form.url) }}
    • Rendering a Form: form_label {{ form_label(form.url, "Clip URL") }} <label for="form_url" class="required">Clip URL</label>
    • Rendering a Form: form_errors {{ form_errors(form.url) }} <ul><li>This value is not a valid URL.</li></ul>
    • Rendering a Form: form_widget {{ form_widget(form.url, {'attr': {'class': 'text-input'}}) }} <input type="text" id="form_url" class="text-input" name="form[url]" required="required" value="123123">
    • Form theming: Inside a Separate Template {# src/Volcano/VideoBundle/Resources/views/Form/fields.html.twig #} {% block form_row %} {% spaceless %} <div class="form_row"> {{ form_label(form) }} {{ form_errors(form) }} {{ form_widget(form) }} </div> {% endspaceless %} {% endblock form_row %}
    • Form theming: Inside a Separate Template {% form_theme form 'VolcanoVideoBundle:Form:fields.html.twig' %} {% block content %} {{ form(form) }} {% endblock content %}
    • Form theming: Inside the same Template {% form_theme form _self %} {% block content %} {{ form(form) }} {% endblock content %} {% block form_row %} {% spaceless %} <div class="form_row"> {{ form_label(form) }} {{ form_errors(form) }} {{ form_widget(form) }} </div> {% endspaceless %} {% endblock form_row %}
    • Form theming: Global # app/config/config.yml twig: form: resources: - 'VolcanoVideoBundle:Form:fields.html.twig'
    • Form theming: Individual field {% form_theme form _self %} {% block _form_url_row %} <div class="url"> {{ form_label(form) }} {{ form_errors(form) }} {{ form_widget(form) }} </div> {% endblock %}
    • Embedding a Collection of Forms // src/Volcano/VideoBundle/Form/Type/TagType.php class TagType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name'); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'VolcanoVideostatusBundleEntityTag', )); } public function getName() { return 'tag'; } }
    • Embedding a Collection of Forms // src/Volcano/VideoBundle/Form/Type/ClipType.php class ClipType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('url','text'); $builder->add('timeStart', 'integer'); //... $builder->add('tags', 'collection', array( 'type' => new TagType(), 'allow_add' => true, 'allow_delete' => true ) ); } }