Skip to content

Instantly share code, notes, and snippets.

View coreymcmahon's full-sized avatar

Corey McMahon coreymcmahon

View GitHub Profile
<h1>Users</h1>
{% if users|length == 0 %}
{# No users to display... #}
<p>Could not find any users :(</p>
{% else %}
<table>
<thead>
<td>Username</td>
<td>Email address</td>
<td>Password</td>
<?php
namespace MyCompany\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/* Include the required validators */
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
@coreymcmahon
coreymcmahon / UserType.php
Created May 7, 2012 13:24
A UserType class used to implement a form for the User entity - http://www.symfonycentral.com/symfony-does-validation.html
<?php
namespace MyCompany\BlogBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
<?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;
<?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();
@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... */
@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 / 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 / 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 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();