Skip to content

Instantly share code, notes, and snippets.

@samjarrett
Created March 3, 2015 09:48
Show Gist options
  • Save samjarrett/8e065bc6fe26519dca07 to your computer and use it in GitHub Desktop.
Save samjarrett/8e065bc6fe26519dca07 to your computer and use it in GitHub Desktop.
Trying to validate an object two different ways
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class Campaign
* @package AppBundle\Entity
* @ORM\Entity
* @ORM\Table
*/
class Campaign
{
// ....
/**
* Image representing the sponsor of the campaign
* @var Image
* @ORM\ManyToOne(targetEntity="Image", cascade={"all"})
* @Assert\Valid
*/
protected $logoImage;
/**
* Image representing the campaign
* @var Image
* @ORM\ManyToOne(targetEntity="Image", cascade={"all"})
* @Assert\NotBlank
* @Assert\Valid
*/
protected $heroImage;
// ...
}
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class CampaignType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('logoImage', new ImageType, array('validation_groups' => array('campaign_logo_image')));
$builder->add('heroImage', new ImageType, array('validation_groups' => array('campaign_hero_image')));
}
public function getName()
{
return 'campaign';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Campaign',
'validation_groups' => array('campaign'),
));
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Class Image
* Doctrine-managed image representation, based upon http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
* @package AppBundle\Entity
* @ORM\Entity
* @ORM\Table
* @ORM\HasLifecycleCallbacks
*/
class Image
{
// ...
/**
* @var UploadedFile
* @Assert\Image(minWidth="300", groups={"campaign_logo_image"})
* @Assert\Image(minWidth="900", groups={"campaign_hero_image"})
*/
protected $file;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
if (isset($this->path)) { // We're updating - so save a reference to the old file, so we can clear it later
$this->temp = $this->path;
$this->path = null;
}
}
/**
* @return \Symfony\Component\HttpFoundation\File\UploadedFile
*/
public function getFile()
{
return $this->file;
}
// ...
}
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ImageType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('file', 'file', array('label' => $options['label'] ?: null));
}
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return 'image';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Image',
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment