Symfony validating another dropdown based on the value selected on a dropdown

688 Views Asked by At

I have 3 dropdowns that are not mapped to the FormType I am drawing out these form elements.

ContentType - Region - Languages

ContentType can take 2 values - Standard or Regional. When I select Standard, I do not want the region dropdown to be validated. But it should be validated when Regional ContentType is selected. Region and Languages are dynamically populated.

I have not been able to follow the conditional validating and I don't think its a solution to my scenario here.

$brandId = $options['brandId'];
$builder
        ->add('contentType', 'choice', array(
            'mapped' => false,
            'empty_value' => 'Select Content Type',
            'choices' => array(
                'standard' => 'Standard',
                'regional' => 'Regional'
            ),
            'required' => true,
            'multiple' => false,
            'expanded' => false
        ))
        ->add('language', 'entity', array(
            'required' => false,
            'mapped' => false,
            'empty_value' => 'Select Language',
            'multiple' => false,
            'class' => 'Bundle:Language',
            'property' => 'title',
            'query_builder' => function(EntityRepository $er) use($brandId) {
                return $er->createQueryBuilder('l')
                    ->join('l.regions', 'lr')
                    ->join('lr.brands', 'lrb')
                    ->where('lrb.id = :currentBrandId')
                    ->setParameter('currentBrandId', $brandId);
            }
        ))
        ->add('regionSelection', 'entity', array(
            'required' => false,
            'mapped' => false,
            'empty_value' => 'Select Region',
            'empty_data' => null,
            'multiple' => false,
            'class' => 'Bundle:Region',
            'property' => 'title',
            'query_builder' => function(EntityRepository $er) use($brandId) {
                return $er->createQueryBuilder('r')
                    ->join('r.brands', 'rb')
                    ->where('rb.id = :currentBrandId')
                    ->setParameter('currentBrandId', $brandId);
            }
        ));

Any help on this will be appreciated.

Edit

So I tried adding a constraints to the Form Builder with a callback:

->add('regionSelection', 'entity', array(
            'constraints' => array(
                new Assert\Callback(array(array($this, 'validateRegion')))
            ),
            'mapped' => false,
            'empty_value' => 'Select Region',
            'empty_data' => null,
            'multiple' => false,
            'class' => 'Bundle:Region',
            'property' => 'title',
            'query_builder' => function(EntityRepository $er) use($brandId) {
                return $er->createQueryBuilder('r')
                    ->join('r.brands', 'rb')
                    ->where('rb.id = :currentBrandId')
                    ->setParameter('currentBrandId', $brandId);
            }
        )); 

And the call is as below:

 public function validateRegion($value, ExecutionContextInterface $context)
{
//I am trying to check here if the contentType's selected value = 'regional' and no      //region is selected then the following code executes.
        $context->addViolationAt('regionSelection', 'There is already an event during this time!');

}

My issue now is since I am passing the $this into the callback function, I cannot find how to access the contentFormType field.

1

There are 1 best solutions below

1
On BEST ANSWER

You can check the global $_POST variable (via the Request object) if the other field(s) were submitted... sth like:

public function validateRegion($value, ExecutionContextInterface $context)
{
     $request = Request::createFromGlobals();   
     $formData = $request->request->get("your_form_name");    
     if($formData["contentType"] == "regional"){
        ...
     }
}