Skip to content

Instantly share code, notes, and snippets.

View coreymcmahon's full-sized avatar

Corey McMahon coreymcmahon

View GitHub Profile
@coreymcmahon
coreymcmahon / DefaultController.php
Created May 5, 2012 03:01
Usage of the Symfony form object in a Controller - http://www.symfonycentral.com
<?php
/* ... rest of the controller */
/**
* @Route("/article/add", name="post_add")
* @Template()
*/
public function addAction()
{
$form = $this->createFormBuilder()
@coreymcmahon
coreymcmahon / add.html.twig
Created May 5, 2012 03:06
Using the Symfony Form object in a Twig template
{# Post-back to a named route #}
<form action="{{ path('post_add') }}" method="post" {{ form_enctype(form) }}>
{# Display any messages for validation errors #}
{{ form_errors(form) }}
<div>
{# Show the fields for the 'title' parameter #}
{{ form_label(form.title) }}
{{ form_errors(form.title) }}
{{ form_widget(form.title) }}
@coreymcmahon
coreymcmahon / add.html.twig
Created May 5, 2012 03:35
Rendering an entire form with a single call to form_widget - http://www.symfonycentral.com
<form action="{{ path('post_add') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="submit" />
</form>
@coreymcmahon
coreymcmahon / ArticleController.php
Created May 5, 2012 04:02
Example of a controller using a Form to persist data to the DB - http://www.symfonycentral.com
<?php
/* ... rest of the controller */
if ($this->getRequest()->getMethod() == 'POST') {
/* First we 'bind' the postback data to the form */
$form->bind($this->getRequest()->get('form'));
if ($form->isValid()) {
/* Now we get the data as an associative array */
$formdata = $form->getData();
@coreymcmahon
coreymcmahon / ArticleType.php
Created May 5, 2012 05:54
Defining a Symfony form in a separate class - http://www.symfonycentral.com
<?php
// src/MyCompany/BlogBundle/Form/Type/ArticleType.php
namespace MyCompany\BlogBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class ArticleType extends AbstractType
{
@coreymcmahon
coreymcmahon / ArticleController.php
Created May 5, 2012 05:59
Using the Form class in a Controller - http://www.symfonycentral.com
<?php
use MyCompany\BlogBundle\Form\Type\ArticleType;
use MyCompany\BlogBundle\Entity\Article;
/* ... rest of the controller */
/**
* @Route("/article/add", name="post_add")
* @Template()
*/
@coreymcmahon
coreymcmahon / Article.php
Created May 6, 2012 09:31
Using annotations to define validation constraints - http://www.symfonycentral.com/symfony-does-validation.html
<?php
// src/MyCompany/BlogBundle/Entity/Article.php
use Symfony\Component\Validator\Constraints as Assert;
class Article
{
/**
* @Assert\NotBlank()
*/
public $title;
@coreymcmahon
coreymcmahon / gist:2621255
Created May 6, 2012 09:36
Use a validator to validate an instance of an entity class - http://www.symfonycentral.com
<?php
/* ... rest of the controller */
$article = new Article();
/* do stuff to the Article entity instance */
$validator = $this->get('validator');
$errors = $validator->validate($author);
/* rest of the controller... */
<?php
use MyCompany\BlogBundle\Entity\Article;
use MyCompany\BlogBundle\Form\ArticleType;
use Symfony\Component\HttpFoundation\Request;
/* ... rest of the controller */
public function updateAction(Request $request)
{
$article = new Article();
<?php
namespace MyCompany\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Doctrine\ORM\EntityRepository;
/* Import the entity and form classes */
use MyCompany\BlogBundle\Entity\User;