-
-
Save loren-osborn/4436548 to your computer and use it in GitHub Desktop.
This file contains hidden or 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\Entity; | |
// use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Validator\Constraints as Assert; | |
/** | |
* FooTester | |
* | |
* @ ORM\Table(name="FooTester") | |
*/ | |
class FooTester | |
{ | |
/** | |
* @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; | |
} | |
/** | |
* @param $emailList array | |
*/ | |
public function setEmailList($emailList) | |
{ | |
$this->emailList = $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; | |
} | |
} |
This file contains hidden or 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\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
class FooType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->add('id', 'integer'); | |
$builder->add('username', 'text'); | |
$builder->add( | |
'emailList', | |
'collection', | |
array( | |
'type' => 'email', | |
'allow_add' => true, | |
'label' => 'Email', | |
'error_bubbling' => true, | |
'required' => true, | |
'options' => array( | |
'required' => true | |
) | |
) | |
); | |
} | |
public function getName() | |
{ | |
return 'email_array_test'; | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'Acme\DemoBundle\Entity\FooTester', | |
'csrf_protection' => false | |
)); | |
} | |
} |
This file contains hidden or 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\Entity\FooTester; | |
use Acme\DemoBundle\Form\Type\FooType; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
class WelcomeController extends Controller | |
{ | |
public function indexAction() | |
{ | |
$fooObject = new FooTester(); | |
if (0) { | |
$fooObject->setUsername('Foobar'); | |
$fooObject->addEmail('foo'); | |
return new Response(var_export($this->get('validator')->validate($user), true)); | |
} else { | |
$form = $this->createForm(new FooType(), $fooObject); | |
$form->bind(array('username' => 'Foobar', 'emailList' => array('foo'))); | |
return new Response(var_export(array('isValid()' => $form->isValid(), 'getErrors()' => $form->getErrors()), true)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment