Skip to content

Instantly share code, notes, and snippets.

@Spea
Created January 2, 2013 17:29
Show Gist options
  • Save Spea/4436316 to your computer and use it in GitHub Desktop.
Save Spea/4436316 to your computer and use it in GitHub Desktop.
Test All validation constraint (symfony2)
<?php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* @ORM\Table(name="User")
*/
class User
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string")
* @Assert\NotBlank()
*/
private $username;
/**
* @var array
*
* @ORM\Column(name="email_list", type="array")
* @Assert\NotBlank()
* @Assert\Count(
* min = "1",
* max = "100",
* minMessage = "You must specify at least one email",
* maxMessage = "You cannot specify more than {{ limit }} emails"
* )
* @Assert\All( constraints = {
* @Assert\NotBlank(),
* @Assert\Email(
* message = "The email '{{ value }}' is not a valid email.",
* checkMX = true
* )
* })
*/
private $emailList = array();
public function addEmail($email)
{
$this->emailList[] = $email;
}
/**
* @return array
*/
public function getEmailList()
{
return $this->emailList;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* @return string
*/
public function getUsername()
{
return $this->username;
}
}
<?php
namespace Acme\DemoBundle\Controller;
use Acme\DemoBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class WelcomeController extends Controller
{
public function indexAction()
{
$user = new User();
$user->setUsername('Foobar');
$user->addEmail('foo');
var_dump($this->get('validator')->validate($user));
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment