Last active
August 29, 2015 14:10
-
-
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
This file contains 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 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'; | |
} | |
} |
This file contains 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 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Goal:
If the checkbox for
dormerRoofOverhang
has been checked for value1
then an entityRoofOverhangWidth
has to be selected fromdormerRoofOverhangWidth
.