Last active
August 29, 2015 13:56
-
-
Save hidenorigoto/8884909 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Controller; | |
use Acme\DemoBundle\Form\Entity\Item; | |
use Acme\DemoBundle\Form\Entity\Tag; | |
use Acme\DemoBundle\Form\Type\ItemWithTagsType; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Symfony\Component\HttpFoundation\Request; | |
use Acme\DemoBundle\Form\ContactType; | |
// these import the "@Route" and "@Template" annotations | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | |
class DemoController extends Controller | |
{ | |
/** | |
* @Route("/contact", name="_demo_contact") | |
* @Template() | |
*/ | |
public function contactAction(Request $request) | |
{ | |
$data = new Item(); | |
$tag1 = new Tag(); | |
$tag1->name = 'PHP'; | |
$tag2 = new Tag(); | |
$tag2->name = 'Ruby'; | |
$data->tags = [$tag1, $tag2]; | |
$form = $this->createForm(new ItemWithTagsType(), $data); | |
$form->handleRequest($request); | |
if ($form->isValid()) { | |
var_dump($form->getData()); | |
} | |
return array('form' => $form->createView()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Form\Entity; | |
use Symfony\Component\Validator\Constraints As Assert; | |
class Item | |
{ | |
public $id; | |
/** | |
* @Assert\NotBlank | |
*/ | |
public $name; | |
/** | |
* @assert\Valid | |
*/ | |
public $tags = []; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Form\Entity; | |
use Symfony\Component\Validator\Constraints As Assert; | |
class Tag | |
{ | |
public $id; | |
/** | |
* @Assert\Length(max=10) | |
*/ | |
public $name; | |
public function __construct($id = null, $name = null) | |
{ | |
$this->id = $id; | |
$this->name = $name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Repository; | |
use Acme\DemoBundle\Form\Entity\Tag; | |
class TagRepository | |
{ | |
public function findAll() | |
{ | |
return [ | |
new Tag(1, 'PHP'), | |
new Tag(2, 'Ruby'), | |
new Tag(3, 'Python'), | |
new Tag(4, 'Java') | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
acme.demo.formtype.tags: | |
class: Acme\DemoBundle\Form\Type\TagsType | |
tags: | |
- { name: form.type, alias: tags } | |
calls: | |
- [setTagsToTextTransformer, [@acme.demo.formtransformer.tagstotext]] | |
acme.demo.repository.tag_repository: | |
class: Acme\DemoBundle\Repository\TagRepository | |
# for normal ORM repository see http://stackoverflow.com/questions/12223176/how-to-inject-a-repository-into-a-service-in-symfony2 | |
acme.demo.formtransformer.tagstotext: | |
class: Acme\DemoBundle\Form\Transformer\TagsToTextTransformer | |
arguments: [@acme.demo.repository.tag_repository] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "AcmeDemoBundle::layout.html.twig" %} | |
{% block title "Symfony - Contact form" %} | |
{% block content %} | |
{{ form_start(form) }} | |
{{ form_errors(form) }} | |
{{ form_row(form.name) }} | |
{{ form_row(form.tags) }} | |
{{ form_rest(form) }} | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment