Symfony2. Form and Validation

download Symfony2. Form and Validation

of 26

  • date post

    10-May-2015
  • Category

    Technology

  • view

    4.073
  • download

    1

Embed Size (px)

description

Symfony2. Form and Validation

Transcript of Symfony2. Form and Validation

  • 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 %} Form demo {{ 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 %} Form demo {{ form(form) }} {% endblock %} 13. Creating a Simple Form: Template 14. Creating a Simple Form: Template

Url
...
Save

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") }} Clip URL 18. Rendering a Form: form_errors {{ form_errors(form.url) }}

  • This value is not a valid URL.

19. Rendering a Form: form_widget {{ form_widget(form.url, {'attr': {'class': 'text-input'}}) }} 20. Form theming: Inside a Separate Template {# src/Volcano/VideoBundle/Resources/views/Form/fields.html.twig #} {% block form_row %} {% spaceless %}

{{ form_label(form) }} {{ form_errors(form) }} {{ form_widget(form) }}

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

{{ form_label(form) }} {{ form_errors(form) }} {{ form_widget(form) }}

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

{{ form_label(form) }} {{ form_errors(form) }} {{ form_widget(form) }}

{% 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 ) ); } }