Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active August 29, 2015 14:10
Show Gist options
  • Save webdevilopers/e175bdcb8f232ba3a828 to your computer and use it in GitHub Desktop.
Save webdevilopers/e175bdcb8f232ba3a828 to your computer and use it in GitHub Desktop.
Assert entity is selected if checkbox is checked with Expression Constraint - Symfony2 Form Validation
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\FormBuilderInterface;
class DormerRequestType extends BaseRequestType
{
public function buildForm(FormBuilderInterface $builder, array $options) {
switch ($options['flow_step']) {
case 1:
parent::buildForm($builder, $options);
break;
case 2:
$builder
->add('dormerRoofOverhang', 'choice', array(
'choices' => array(
'0' => 'Nein',
'1' => 'Ja'
),
'expanded' => true,
'required' => true,
'translation_domain' => 'dormer'
))
->add('dormerRoofOverhangWidth', 'entity', array(
'class' => 'Acme\AppBundle\Entity\RoofOverhangWidth',
'translation_domain' => 'dormer'
))
;
break;
}
}
public function getName() {
return 'dormerRequest';
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\Expression("this.getDormerRoofOverhang() == 1 & this.getDormerRoofOverhangWidth() != null", groups={"flow_dormerRequest_step2"})
*/
class PriceQuoteRequest
{
/**
* @var $dormerRoofOverhang
*
* @ORM\Column(name="roof_overhang", type="integer")
*
* @Assert\NotBlank(groups={"flow_dormerRequest_step2"})
* @Assert\Type(type="integer", groups={"flow_dormerRequest_step2"})
*/
public $dormerRoofOverhang;
/**
* @var $dormerRoofOverhangWidth
*
* @ORM\OneToOne(targetEntity="AppBundle\Entity\RoofOverhangWidth")
* @ORM\JoinColumn(name="roofOverhangWidth_id", referencedColumnName="id")
*
* @Assert\Type(type="AppBundle\Entity\RoofOverhangWidth", groups={"flow_dormerRequest_step2"})
* Assert\Expression("this.getDormerRoofOverhang() == 1 & this.getDormerRoofOverhangWidth() == null", groups={"flow_dormerRequest_step2"})
*/
public $dormerRoofOverhangWidth;
}
@webdevilopers
Copy link
Author

Goal:
If the checkbox for dormerRoofOverhang has been checked for value 1 then an entity RoofOverhangWidth has to be selected from dormerRoofOverhangWidth.

@rufinus
Copy link

rufinus commented Dec 22, 2014

missed a @ in line 30?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment