Symfony - Personnal Recursive function where required = true return false

112 Views Asked by At

I came here to relate a problem, i do a recursive function to generate some option for a product.

The problem is when i set required to true, it's already false.

I tryed to force it at true but anyway, required = false.

 public function creationChamps($builder, $produits, $label = false) {
        $produitEnfant = $produits->getProduitEnfants();
                //dump($produitEnfant);die;
            // Ajout des options
            foreach ($produitEnfant as $produitOption) {
                // Récupération des paramètres
                $infosComplementaire = $produitOption->getSaisieinfoaddobligatoire();
                $designation = $produitOption->getDesignationInternet();
                $prix = $produitOption->getPrixTTC();
                $quantite_min   = $produitOption->getQuantiteMin();
                $quantite_max   = $produitOption->getQuantiteMax();
                $obligatoire    = $produitOption->getObligatoire();
                $consommationCommune    = $produitOption->getConsommationCommune();
                if ($quantite_min == 0 && $quantite_max <= 1 && $consommationCommune == 0) {
                    $designation_final = ($prix > 0? $designation . ' ' . $prix . ' €':$designation);
                    $builder->add($produitOption->getIdproduit(), CheckboxType::class, array(
                        'label'     => $designation_final,
                        "data"      => false,
                        "mapped"    => false,
                        "required" => true,
                        "attr"      => array('class'=>$produitOption->getIdproduit().'_element')
                    ));
                    if ($infosComplementaire && $quantite_max == 1) {
                        $builder->add($produitOption->getIdproduit().'_infosComplementaire', TextType::class, array(
                            "label" => $label,
                            "mapped" => false,
                            "required" => true,
                            "attr" => array('class'=>$produitOption->getIdproduit().'_infosComplementaire')
                        ));
                    }
                } elseif ($quantite_min <= 1 && $quantite_max <= 1 && $consommationCommune == 1) {
                    // Construction de la liste
                    $option = array();
                    foreach ($produitEnfant as $produitOption) {
                        $infosComplementaire = $produitOption->getSaisieinfoaddobligatoire();
                        $designation = $produitOption->getDesignationInternet();
                        $prix = $produitOption->getPrixTTC();
                        $quantite_min   = $produitOption->getQuantiteMin();
                        $quantite_max   = $produitOption->getQuantiteMax();
                        $obligatoire    = $produitOption->getObligatoire();
                        $consommationCommune    = $produitOption->getConsommationCommune();
                        $prixFinal = $prix;
                        if ($prixFinal == 0) {
                            $option[$designation] = $produitOption->getIdproduit();
                        } else {
                            $option[$designation .' - '. $prixFinal . ' €'] = $produitOption->getIdproduit();
                        }
                    }

                    $builder->add($produitOption->getProduitPere()->getIdproduit().'_liste', ChoiceType::class, array(
                        "label" => $label,
                        "choices" => $option,
                        "mapped" => false,
                        "required" => true,
                        "attr" => array('class'=>$produitOption->getIdproduit().'_element')
                    ));
                } else {
                        // Construction de la liste
                        $option = array();
                        for ($i = $quantite_min; $i <= $quantite_max; $i++) {
                            $prixFinal = $prix * $i;
                            if ($prixFinal == 0) {
                                $option[$i .' ' . $label] = $i;
                            } else {
                                $option[$i .' ' . $label .' - '. $prixFinal . ' €'] = $i;
                            }
                        }
                        $builder->add($produitOption->getIdproduit(), ChoiceType::class, array(
                            "label" => $designation,
                            "choices" => $option,
                            "mapped" => false,
                            "required" => true,
                            "attr" => array('class'=>$produitOption->getIdproduit().'_element')
                        ));
                }
            }  
            foreach ($produitEnfant as $produitOption) {     
                $infosComplementaire = $produitOption->getSaisieinfoaddobligatoire();
                $designation = $produitOption->getDesignationInternet();
                $prix = $produitOption->getPrixTTC();
                $quantite_min   = $produitOption->getQuantiteMin();
                $quantite_max   = $produitOption->getQuantiteMax();
                $obligatoire    = $produitOption->getObligatoire();
                $consommationCommuneFils = false;
                $children = false;
                if (count($produitOption->getProduitEnfants()) > 0) {
                    $children = true;
                    foreach ($produitOption->getProduitEnfants() as $produitEnfant) {
                        if ($produitEnfant->getConsommationCommune()) {
                            $consommationCommuneFils = true;
                            break;
                        }
                    }
                }

                if ($children) {
                    if ($quantite_min == 0 && $quantite_max <= 1 && $consommationCommuneFils == false) {
                        $label = $produitOption->getDesignationInternet();
                    } elseif ($quantite_min <= 1 && $quantite_max <= 1 && $consommationCommuneFils == true) {
                        $label = false;
                    }
                    $options = array();
                    $options['listeOptions'] = $produitOption;
                    // Generation du champs
                    $builder->add($produitOption->getIdproduit() . '_option', listeOptionForm::class, array(
                        'required' => true, 
                        'label' => $label,
                        'mapped' => false,
                        'data'=> $options,
                        "attr" => array("class" => $produitOption->getIdproduit()."_option"),
                    ));
                }  
            }
        return $builder;
    }

Here the dump of the $builder variable:

FormBuilder {#2458 ▼
  -children: array:1 [▼
    1091 => null
  ]
  -unresolvedChildren: array:1 [▼
    1091 => array:2 [▼
      "type" => "Symfony\Component\Form\Extension\Core\Type\ChoiceType"
      "options" => array:5 [▼
        "label" => "Sms"
        "choices" => array:1 [▶]
        "mapped" => false
        "required" => true
        "attr" => array:1 [▶]
      ]
    ]
  ]
  #locked: false
  -dispatcher: EventDispatcher {#2459 ▶}
  -name: "listeOptionsInscription"
  -propertyPath: null
  -mapped: false
  -byReference: true
  -inheritData: false
  -compound: true
  -type: ResolvedTypeDataCollectorProxy {#2451 ▶}
  -viewTransformers: []
  -modelTransformers: []
  -dataMapper: PropertyPathMapper {#2460 ▶}
  -required: false
  -disabled: false
  -errorBubbling: true
  -emptyData: Closure {#2455 ▶}
  -attributes: array:1 [▶]
  -data: array:37 [▶]
  -dataClass: null
  -dataLocked: true
  -formFactory: FormFactory {#2163 ▶}
  -action: ""
  -method: "POST"
  -requestHandler: HttpFoundationRequestHandler {#2145 ▶}
  -autoInitialize: true
  -options: array:36 [▶]
}

Has anyone come across this problem before ?

Thanks for the advice.

EDIT : I dump all the variable and my problem is when i create the createView(), the required is set to false when in the builder, my variable is set to true.

FormView {#4078 ▼
  +vars: array:24 [▶]
  +parent: FormView {#3760 ▶}
  +children: array:1 [▼
    1091 => FormView {#4081 ▼
      +vars: array:33 [▼
        "value" => ""
        "attr" => array:1 [▶]
        "form" => FormView {#4081}
        "id" => "inscription_personne_form_listeOptionsInscription_1091"
        "name" => "1091"
        "full_name" => "inscription_personne_form[listeOptionsInscription][1091]"
        "disabled" => false
        "label" => "Sms"
        "label_format" => null
        "multipart" => false
        "block_prefixes" => array:3 [▶]
        "unique_block_prefix" => "_inscription_personne_form_listeOptionsInscription_1091"
        "translation_domain" => null
        "cache_key" => "_inscription_personne_form_listeOptionsInscription_1091_choice"
        "errors" => FormErrorIterator {#4082 ▶}
        "valid" => true
        "data" => null
        "required" => false
        "size" => null
        "label_attr" => []
        "compound" => false
        "method" => "POST"
        "action" => ""
        "submitted" => false
        "multiple" => false
        "expanded" => false
        "preferred_choices" => []
        "choices" => array:1 [▶]
        "separator" => "-------------------"
        "placeholder" => null
        "choice_translation_domain" => null
        "is_selected" => Closure {#4085 ▶}
        "placeholder_in_choices" => false
      ]
      +parent: FormView {#4078}
      +children: []
      -rendered: false
    }
  ]
  -rendered: false
}
0

There are 0 best solutions below