[sonataAdmin]:测试选择输入值

时间:2017-05-02 08:13:59

标签: symfony sonata-admin

我想检索select输入的值并对此值进行测试。如果这个值对应于我在我的情况下所寻找的“自由职业者”我会显示一个块,如果没有我显示另一个

这是我的代码:

 $emr = $this->modelManager->getEntityManager('PivotalBOBundle:Role');
    $queryr = $kernel->getContainer()->get('doctrine')->getRepository('PivotalBOBundle:Role')->findAll();

    $choises=array();

    foreach ($queryr as $res){
        $choises[$res->getRole()]=$res->getRole();
    }
->tab('Spécifique')
        ->with('Type d\'utilisateur', array('class' => 'col-md-12'))
        ->add('type', 'choice', array('label' => 'Type',
            'choices' => $choises))
        ->end()

->with('Freelancer', array('class' => 'col-md-12 Freelancer'))
        ->add('type', 'checkbox', array('required' => false, 'label' => 'Freelancer'))
        ->add('categories', 'sonata_type_model', array(
                "multiple" => true,
                'label' => 'Catégorie',
                'required' => true,
                'query' => $query))
            ->add('outilsEtTechnologie', 'sonata_type_model', array(
                'required' => false,
                "multiple" => true,
                'label' => 'Outils et Technologie'))
            ->add('niveauCompetences', 'sonata_type_collection', array(
                'required' => false,
                'label' => 'Niveau des compétences',
                'by_reference' => true), array(
                'edit' => 'inline',
                'inline' => 'table'
            ))
            ->add('niveauLangue', 'sonata_type_collection', array(
                'required' => false,
                'label' => 'Niveau des langues',
                'by_reference' => false), array(
                'edit' => 'inline',
                'inline' => 'table'
            ))
            ->add('formation', 'sonata_type_collection', array(
                'required' => false,
                'label' => 'Formations',
                'by_reference' => false), array(
                'edit' => 'inline',
                'inline' => 'table'
            ))
            ->add('mobilite', 'entity', array(
                'class' => 'Pivotal\BOBundle\Entity\Mobilite',
                'required' => true,
                'label' => 'Mobilite'
            ))
            ->add('frequence', 'entity', array(
                'class' => 'Pivotal\BOBundle\Entity\Frequence',
                'required' => true,
                'label' => 'Frequence'
            ))
            ->add('niveauExperience', 'entity', array(
                'class' => 'Pivotal\BOBundle\Entity\NiveauExperience',
                'required' => true,
                'label' => 'Niveau Experience'))
            ->add('missionetranger', 'entity', array(
                'class' => 'Pivotal\BOBundle\Entity\Missionetranger',
                'required' => true,
                'label' => 'Mission Etranger'))
            ->add('note', null, array('label' => 'Note'))
        ->end()
        ->with('Jobowner ', array('class' => 'col-md-12 Jobowner'))
        ->add('type', 'checkbox', array('required' => false, 'label' => 'Jobowner'))
        ->add('societe', 'entity', array(
                'class' => 'Pivotal\BOBundle\Entity\Societe',
                'property' => 'nom',
                'label' => 'Societe',
                'required' => false,
            ))
        ->end();

这是我的渲染the case 1

的图片

这是我的目标:

the freelancer option

the jobowner option

2 个答案:

答案 0 :(得分:0)

您可以使用$this->getSubject()访问当前主题。

因此,您可以使用以下方式修改表单:

$subject = $this->getSubject();

// with "Type d'utilisateur"

if ($subject && $subject->getType() === 'freelance') {
    // with "Freelancer"
}

// other tabs/form-fields

仅当您重新打开表单视图时才有效。如果您在不重新加载页面的情况下选择“自由职业者”,则需要立即修改表单,您需要在Javascript中编写它。根据您的需要,这会有很大差异(例如隐藏标签/部分并通过Javascript显示)。

答案 1 :(得分:0)

要做到这一点,我们应该使用这样的java脚本代码:

->tab('Spécifique')
        ->with('Type d\'utilisateur', array('class' => 'col-md-12'))
        ->add('type', 'choice', array('label' => 'Type',
            'choices' => $choises))
        ->end()
        ->with('Freelancer', array('class' => 'col-md-12 userFreelancer'))
        ->add('categories', 'sonata_type_model', array(
                "multiple" => true,
                'label' => 'Catégorie',
                'required' => true,
                'query' => $query))
            ->add('outilsEtTechnologie', 'sonata_type_model', array(
                'required' => false,
                "multiple" => true,
                'label' => 'Outils et Technologie'))
            ->add('niveauCompetences', 'sonata_type_collection', array(
                'required' => false,
                'label' => 'Niveau des compétences',
                'by_reference' => true), array(
                'edit' => 'inline',
                'inline' => 'table'
            ))
            ->add('niveauLangue', 'sonata_type_collection', array(
                'required' => false,
                'label' => 'Niveau des langues',
                'by_reference' => false), array(
                'edit' => 'inline',
                'inline' => 'table'
            ))
            ->add('formation', 'sonata_type_collection', array(
                'required' => false,
                'label' => 'Formations',
                'by_reference' => false), array(
                'edit' => 'inline',
                'inline' => 'table'
            ))
            ->add('mobilite', 'entity', array(
                'class' => 'Pivotal\BOBundle\Entity\Mobilite',
                'required' => true,
                'label' => 'Mobilite'
            ))
            ->add('frequence', 'entity', array(
                'class' => 'Pivotal\BOBundle\Entity\Frequence',
                'required' => true,
                'label' => 'Frequence'
            ))
            ->add('niveauExperience', 'entity', array(
                'class' => 'Pivotal\BOBundle\Entity\NiveauExperience',
                'required' => true,
                'label' => 'Niveau Experience'))
            ->add('missionetranger', 'entity', array(
                'class' => 'Pivotal\BOBundle\Entity\Missionetranger',
                'required' => true,
                'label' => 'Mission Etranger'))
            ->add('note', null, array('label' => 'Note'))
        ->end();

        $formMapper->with('Societe', array('class' => 'col-md-12 Societe'))
            ->add('societe', 'entity', array(
                'class' => 'Pivotal\BOBundle\Entity\Societe',
                'property' => 'nom',
                'label' => 'Societe',
                'required' => false,
            ))

        ->end()
        ->end();

我们应该覆盖模板 那样:

 public function getTemplate($name)
    {
        if ($name == "edit") {
            return 'admin/edit.html.twig';
        }
        return parent::getTemplate($name);
    }

并在edit.html.twig中添加javascript代码:

<script type="text/javascript">

    $(document).ready(function () {
        var type = $("#{{ admin.uniqId }}_type");
        var freelancer = $(".userFreelancer");
        var jobowner = $(".userJobowner");
        var redacteur = $(".userRedacteur");
        var Supperjobowner = $(".userSupperjobowner");
        var Nactif = $(".userNactif");
        var Societe = $(".Societe");
        hideUserBlock()
        type.change(function () {
            displayUserBlock();
        }); // Bind the function to displayBlock
        type.change(); // Manual trigger to display block.


        function displayUserBlock() {
            hideUserBlock();
            var type =$("#{{ admin.uniqId }}_type").val()
            switch(type) {
                case 'Freelancer':
                    freelancer.css("display", "block");
                    freelancer.trigger("create");
                    break;
                case 'Jobowner':
                    Societe.css("display", "block");
                    Societe.trigger("create");
                    break;
                case 'Super Jobowner':
                    Societe.css("display", "block");
                    Societe.trigger("create");
                    break;
                case 'Jobowner non actif':
                    Societe.css("display", "block");
                    Societe.trigger("create");
                    break;
                case 'Rédacteur':
                    Societe.css("display", "block");
                    Societe.trigger("create");
                    break;
            }

        }

        function hideUserBlock() {
            freelancer.css("display", "none");
            freelancer.trigger("create");
            Societe.css("display", "none");
            Societe.trigger("create");
        }

    });

</script>