Top Banner
Symfony2 Form and Validation
26

Symfony2. Form and Validation

May 10, 2015

Download

Technology

Symfony2. Form and Validation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Symfony2. Form and Validation

Symfony2

Form and Validation

Page 2: Symfony2. Form and Validation

Creating an Entity Class// src/Volcano/VideostatusBundle/Entity/Clip.php

namespace Volcano\VideostatusBundle\Entity;

class Clip

{

protected $id;

protected $url;

protected $timeStart;

protected $timeFinish;

}

Page 3: Symfony2. Form and Validation

Creating a Simple Form:Controllerclass 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(),

));

}

}

Page 4: Symfony2. Form and Validation

Creating a Simple Form:Template

{% extends "AcmeDemoBundle::layout.html.twig" %}

{% block content %}

<h1 class="title">Form demo</h1>

{{ form(form) }}

{% endblock %}

Page 5: Symfony2. Form and Validation

Creating a Simple Form:Template

Page 6: Symfony2. Form and Validation

Creating a Simple Form:Controllerclass 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'));

}

//

}

}

Page 7: Symfony2. Form and Validation

Validationuse Symfony\Component\Validator\Constraints as Assert;

class Clip

{

/**

* @Assert\NotBlank(message="Enter URL")

* @Assert\Url(message="Enter valid URL", protocols={"http", "https"})

*/

protected $url;

/**

* @Assert\NotBlank(message="Enter Start Time")

* @Assert\Type(type="integer")

*/

protected $timeStart;

}

Page 8: Symfony2. Form and Validation

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!');

}

Page 9: Symfony2. Form and Validation

Creating Form Classes// src/Volcano/VideoBundle/Form/Type/ClipType.php

namespace Volcano\VideoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;

use Symfony\Component\Form\FormBuilderInterface;

class ClipType extends AbstractType

{

public function buildForm(FormBuilderInterface $builder, array $options)

{

$builder->add('url','text');

$builder->add('timeStart', 'integer');

//...

}

public function getName()

{

return 'task';

}

}

Page 10: Symfony2. Form and Validation

Creating a Simple Form:Controllerclass 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'));

}

//

}

}

Page 11: Symfony2. Form and Validation

Creating Form Classes

// src/Volcano/VideoBundle/Form/Type/ClipType.php

public function setDefaultOptions(OptionsResolverInterface $resolver)

{

$resolver->setDefaults(array(

'data_class' => 'Volcano\VideoBundle\Entity\Clip',

));

}

Page 12: Symfony2. Form and Validation

Creating a Simple Form:Template

{% extends "AcmeDemoBundle::layout.html.twig" %}

{% block content %}

<h1 class="title">Form demo</h1>

{{ form(form) }}

{% endblock %}

Page 13: Symfony2. Form and Validation

Creating a Simple Form:Template

Page 14: Symfony2. Form and Validation

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>

Page 15: Symfony2. Form and Validation

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) }}

Page 16: Symfony2. Form and Validation

Rendering a Form: form_row

{{ form_label(form.url) }}

{{ form_errors(form.url) }}

{{ form_widget(form.url) }}

Page 17: Symfony2. Form and Validation

Rendering a Form: form_label

{{ form_label(form.url, "Clip URL") }}

<label for="form_url" class="required">Clip URL</label>

Page 18: Symfony2. Form and Validation

Rendering a Form: form_errors

{{ form_errors(form.url) }}

<ul><li>This value is not a valid URL.</li></ul>

Page 19: Symfony2. Form and Validation

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">

Page 20: Symfony2. Form and Validation

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

Page 21: Symfony2. Form and Validation

Form theming:Inside a Separate Template

{% form_theme form 'VolcanoVideoBundle:Form:fields.html.twig' %}

{% block content %}

{{ form(form) }}

{% endblock content %}

Page 22: Symfony2. Form and Validation

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

Page 23: Symfony2. Form and Validation

Form theming:Global

# app/config/config.ymltwig: form: resources: - 'VolcanoVideoBundle:Form:fields.html.twig'

Page 24: Symfony2. Form and Validation

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

Page 25: Symfony2. Form and Validation

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' => 'Volcano\VideostatusBundle\Entity\Tag', )); }

public function getName() { return 'tag'; }}

Page 26: Symfony2. Form and Validation

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

)

);

}

}