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

Like this? Share it with your network

Share

Symfony2. Form and Validation

on

  • 3,547 views

Symfony2. Form and Validation

Symfony2. Form and Validation

Statistics

Views

Total Views
3,547
Views on SlideShare
3,543
Embed Views
4

Actions

Likes
0
Downloads
26
Comments
0

2 Embeds 4

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

Accessibility

Categories

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 Presentation Transcript

  • 1. Symfony2 Form and Validation
  • 2. Creating an Entity Class // src/Volcano/VideostatusBundle/Entity/Clip.php namespace VolcanoVideostatusBundleEntity; class Clip { protected $id; protected $url; protected $timeStart; protected $timeFinish; }
  • 3. 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(), )); } }
  • 4. Creating a Simple Form: Template {% extends "AcmeDemoBundle::layout.html.twig" %} {% block content %} <h1 class="title">Form demo</h1> {{ form(form) }} {% endblock %}
  • 5. Creating a Simple Form: Template
  • 6. 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')); } // } }
  • 7. 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; }
  • 8. 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!'); }
  • 9. 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'; } }
  • 10. 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')); } // } }
  • 11. Creating Form Classes // src/Volcano/VideoBundle/Form/Type/ClipType.php public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'VolcanoVideoBundleEntityClip', )); }
  • 12. Creating a Simple Form: Template {% extends "AcmeDemoBundle::layout.html.twig" %} {% block content %} <h1 class="title">Form demo</h1> {{ form(form) }} {% endblock %}
  • 13. Creating a Simple Form: Template
  • 14. 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>
  • 15. 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) }}
  • 16. Rendering a Form: form_row {{ form_label(form.url) }} {{ form_errors(form.url) }} {{ form_widget(form.url) }}
  • 17. Rendering a Form: form_label {{ form_label(form.url, "Clip URL") }} <label for="form_url" class="required">Clip URL</label>
  • 18. Rendering a Form: form_errors {{ form_errors(form.url) }} <ul><li>This value is not a valid URL.</li></ul>
  • 19. 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">
  • 20. 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 %}
  • 21. Form theming: Inside a Separate Template {% form_theme form 'VolcanoVideoBundle:Form:fields.html.twig' %} {% block content %} {{ form(form) }} {% endblock content %}
  • 22. 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 %}
  • 23. Form theming: Global # app/config/config.yml twig: form: resources: - 'VolcanoVideoBundle:Form:fields.html.twig'
  • 24. 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 %}
  • 25. 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'; } }
  • 26. 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 ) ); } }