IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

Masquer des formulaires imbriqués


Sujet :

Symfony PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2015
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2015
    Messages : 34
    Par défaut Masquer des formulaires imbriqués
    Bonjour tout le monde et bonne année ... :-)

    Je rencontre un petit problème qui ne doit pas être bien compliqué à résoudre, mais j'ai beau chercher je ne trouve pas de sujet qui corresponde à ce qu'il m'arrive ...

    J'ai actuellement un projet avec un formulaire contenant des formulaires imbriqués. Ce formulaire correspond à l'objet "projet", et un formulaire imbriqué à l'objet "Assumptions". La relation entre les objets est : Proj-Assump 1..M

    Pour créer un projet je souhaite afficher le formulaire du FormType projet, mais sans afficher les formulaire imbriqués liés au formType Assumption.

    Ce qu'il se passe actuellement, c'est que le form_end() dans mon twig affiche du coup mes formulaires imbriqués (vides en plus) et mon form n'est pas valide (je pense que c'est du à ça ?).

    Merci de votre aide, je vous fourni un peu de code ... :

    Action controller
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    public function viewCreateAction()
        {
            $em = $this->getDoctrine()->getManager();
            $request = $this->get('request'); // We get request through the service 
            $project = new Project(); // Create a blank project object
            // We bind Project object to our Form ProjectType
            $form = $this->createForm(new ProjectType($em), $project); 
     
     
            if ('POST' == $request->getMethod()) { // Si on a posté le formulaire
     
                $form->bind($request); // On bind les données du form
                $this->createProjectInformation($form->getData(), $em);
     
                if ($form->isValid()) { // Si le formulaire est valide
     
                    // On utilise notre Manager pour gérer la sauvegarde de l'objet
                    $this->createProjectInformation($form->getData(), $em);   
                }
            }
     
            $args = array(
                'formProjectInformation' => $form->createView(),
                'confirm' => $this->confirm,
            );
            return $this->render('ATSCapacityPlanBundle:Configuration:ConfigurationCreate.html.twig', $args);
        }

    Formtype Projet
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    <?php
    namespace ATS\CapacityPlanBundle\Form\Type;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Validator\Constraints\NotBlank;
    use Symfony\Component\Validator\Constraints\Length;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
     
    class ProjectType extends AbstractType
    {
        private $manager;
     
        public function __construct(\Doctrine\ORM\EntityManager $manager)
        {
            $this->manager = $manager;
        }
     
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $name = array('label' => 'Project', 'constraints' => array(new NotBlank, new Length(array('min' => 5, 'max' => 100))));
            $builder->add('name', 'text', $name);
            $engagement = array('label' => 'Engagement', 'constraints' => array(new NotBlank, new Length(array('min' => 5, 'max' => 100))));
            $builder->add('engagement', 'text', $engagement);
            $engagementManager = array('label' => 'Engagement Manager', 'constraints' => array(new NotBlank, new Length(array('min' => 5, 'max' => 100))));
            $builder->add('engagementManager', 'text', $engagementManager);
            $projectManager = array('label' => 'Project Manager', 'constraints' => array(new NotBlank, new Length(array('min' => 5, 'max' => 100))));
            $builder->add('projectManager', 'text', $projectManager);
            $startingWeek = array('label' => 'Starting week', 'constraints' => array(new NotBlank, new Length(array('min' =>1, 'max' => 2))));
            $builder->add('startingWeek', 'text', $startingWeek);
            $version = array('label' => 'Version', 'constraints' => array(new NotBlank, new Length(array('min' => 5, 'max' => 15))));
            $builder->add('version', 'text', $version);
            $versionDate = array('label' => 'Version Date', 'constraints' => array(new NotBlank));
            $builder->add('versionDate', 'date', $versionDate);
            $weekConfig = array('label' => 'Week config', 'constraints' => array(new NotBlank, new Length(array('min' =>1, 'max' => 2))));
            $builder->add('weekConfig', 'text', $weekConfig);
            $builder->add('assumptions', 'collection', array(
            // each item in the array will be an "assumption" field
            'type' => new AssumptionType($this->manager),
            'allow_add'    => true,
            // these options are passed to each "email" type
            'options'  => array(
                'required'  => false,
                'data_class' => 'ATS\CapacityPlanBundle\Entity\Assumption'
                ),
            ));
            $submitOptions = array('label' => 'Add the project', 'attr'=>array('class'=>'btn'));
            $builder->add('validate', 'submit', $submitOptions);
        }
     
        public function getDefaultOptions(array $options)
        {
            return array(
                'data_class' => 'ATS\CapacityPlanBundle\Entity\Project',
            );
        }
     
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'ATS\CapacityPlanBundle\Entity\Project',
            ));
        }
     
        public function getName()
        {
            return 'Project';
        }
     
    }
    formType Assumption
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    <?php
    namespace ATS\CapacityPlanBundle\Form\Type;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use ATS\CapacityPlanBundle\Form\DataTransformer\ProjectToNumberTransformer;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
     
    class AssumptionType extends AbstractType
    {
        private $manager;
     
        public function __construct(\Doctrine\ORM\EntityManager $manager)
        {
            $this->manager = $manager;
        }
     
        public function buildForm(FormBuilderInterface $builder, array $options)
        {   
            $blankAssumption = array('required'=>false, 'attr'=>array('rows'=>1, 'cols'=>60));
            $builder->add('text', 'textarea', $blankAssumption);
            $project = array(
                    // validation message if the data transformer fails
                    'invalid_message' => 'That is not a valid project number',
                );
            $builder->add('project', 'hidden', $project);
            $builder->get('project')
                ->addModelTransformer(new ProjectToNumberTransformer($this->manager));
        }
     
        public function getDefaultOptions(array $options)
        {
            return array(
                'data_class' => 'ATS\CapacityPlanBundle\Entity\Assumption',
            );
        }
     
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'ATS\CapacityPlanBundle\Entity\Assumption',
            ));
        }
     
        public function getName()
        {
            return 'Assumption';
        }
     
    }
    Twig
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    {% extends "::GeneralTemplate.html.twig" %} 
     
    {% block menu %}
        <div id="menubar">
           <ul id="menu">
              <!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
              <li class="selected"><a href={{ path('ats_capacity_plan_configuration') }}>Configuration</a></li>
              <li><a href={{ path('ats_capacity_plan_availability') }}>Availability</a></li>
              <li><a href={{ path('ats_capacity_plan_charge') }}>Charge</a></li>
              <li><a href={{ path('ats_capacity_plan_demands') }}>Demands</a></li>
              <li><a href={{ path('ats_capacity_plan_capacitytable') }}>Capacity Table</a></li>
              <li><a href={{ path('ats_capacity_plan_balance') }}>Balance</a></li>
           </ul>
        </div>
    {% endblock %}
     
     
     
    {% block body %}
        {{ form_start(formProjectInformation) }}
        {{ form_errors(formProjectInformation) }}
            <table>
               <tr>
                 <td>{{ form_label(formProjectInformation.name) }}</td>
                 <td>{{ form_widget(formProjectInformation.name) }}</td>
               </tr>
               <tr>
                 <td>{{ form_label(formProjectInformation.engagement) }}</td>
                 <td>{{form_widget(formProjectInformation.engagement) }}</td>
               </tr>
               <tr>
                 <td>{{ form_label(formProjectInformation.engagementManager) }}</td>
                 <td>{{form_widget(formProjectInformation.engagementManager) }}</td>
               </tr>
               <tr>
                 <td>{{ form_label(formProjectInformation.projectManager) }}</td>
                 <td>{{form_widget(formProjectInformation.projectManager) }}</td>
               </tr>
               <tr>
                 <td>{{ form_label(formProjectInformation.startingWeek) }}</td>
                 <td>{{form_widget(formProjectInformation.startingWeek) }}</td>
               </tr>
               <tr>
                 <td>{{ form_label(formProjectInformation.version) }}</td>
                 <td>{{form_widget(formProjectInformation.version) }}</td>
               </tr>
               <tr>
                 <td>{{ form_label(formProjectInformation.versionDate) }}</td>
                 <td>{{form_widget(formProjectInformation.versionDate) }}</td>
               </tr>
               <tr>
                 <td>{{ form_label(formProjectInformation.weekConfig) }}</td>
                 <td>{{form_widget(formProjectInformation.weekConfig) }}</td>
               </tr>
               <tr>
                 <td class="lastLigne"></td>
                 <td>{{form_widget(formProjectInformation.validate) }}</td>
               </tr>
            </table>
        {{ form_end(formProjectInformation) }}
    {% endblock %}
    Merci à tous !

  2. #2
    Membre Expert Avatar de Nico_F
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2011
    Messages
    728
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Avril 2011
    Messages : 728
    Par défaut
    Hello,

    Pourquoi tu veux masquer une partie de ton formulaire ? Autant faire un autre formulaire sans ces éléments si parfois tu as besoin des formulaires imbriqués et parfois pas.

    Encore mieux : tu peux factoriser la partie de ton FormType qui est commune aux deux formulaires pour ne pas avoir à dupliquer de code.

  3. #3
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2015
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2015
    Messages : 34
    Par défaut
    Citation Envoyé par Nico_F Voir le message
    Hello,

    Pourquoi tu veux masquer une partie de ton formulaire ? Autant faire un autre formulaire sans ces éléments si parfois tu as besoin des formulaires imbriqués et parfois pas.

    Encore mieux : tu peux factoriser la partie de ton FormType qui est commune aux deux formulaires pour ne pas avoir à dupliquer de code.
    Ok, en effet c'est plus simple ...

    Merci je cloture ;-)

  4. #4
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2015
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2015
    Messages : 34
    Par défaut
    Parcontre, 3 fois n'est pas coutumes ...

    J'ai un autre souci que je veux bien que tu regardes. Je ne pense pas que ce soit compliqué mais je ne vois plus où chercher ... :

    http://www.developpez.net/forums/d15...s/#post8492410

    Merci d'avance pour tout tes conseils !!!!

Discussions similaires

  1. [2.x] [Symfony 2.6] Persister des formulaires imbriqués
    Par Kevfou dans le forum Symfony
    Réponses: 27
    Dernier message: 18/05/2015, 21h07
  2. [2.x] [Form] Personnalisation des formulaires imbriqués
    Par FadeToBlack dans le forum Symfony
    Réponses: 10
    Dernier message: 20/03/2013, 17h55
  3. Afficher/masquer des champs de formulaire à la demande
    Par renaud26 dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 16/10/2006, 13h20
  4. [Debutant] formulaire masquer des doublons
    Par anassyto dans le forum Access
    Réponses: 1
    Dernier message: 25/07/2006, 11h46
  5. Masquer des champs dans un formulaire
    Par crazykingpin dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 30/12/2005, 15h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo