当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to have a field in my form whitout displaying it in Symfony 2

问题描述:

I

I use a collection field to display twice all the text of a project that I'm creating (one collection for french, the other for english), but I choose to not display the field langague because the user is not suppose to be able to change that. To do this, I wrote:

{% for description in form.descriptions %}

{% if loop.index == 1 %}

French

{% elseif loop.index == 2 %}

English

{% endif %}

<li>

{% do description.language.setRendered %}

...

</li>

{% endfor %}

The thing is that I first tried writting my controller like this:

public function ajoutAction(Request $request)

{

//Crée un nouveau projet

$nouveauProjet = new Projet();

//Crée les descriptions

$descriptionFr = new ProjetInt();

$descriptionEn = new ProjetInt();

//Indique le bon code de langue pour les descriptions

$descriptionFr->setLangue('fr');

$descriptionEn->setLangue('en');

//Associe le projet et les descriptions

$nouveauProjet->addDescription($descriptionFr);

$nouveauProjet->addDescription($descriptionEn);

$descriptionFr->setProjet($nouveauProjet);

$descriptionEn->setProjet($nouveauProjet);

//Formulaires pour la création d'un projet

$formulaire=$this->createForm(new ProjetType(), $nouveauProjet);

//On surveille le formulaire

$formulaire->handleRequest($request);

//Si le formulaire est valide

if($formulaire->isValid())

{

...

}

return $this->render('AdminBundle::ajoutProjet.html.twig',array(

'formulaire'=>$formulaire->createView()

));

}

The problem is that when I submit the form, it doesn't find the language because of what I did inside my template, so insted, my controller looks like this:

public function ajoutAction(Request $request)

{

//Crée un nouveau projet

$nouveauProjet = new Projet();

//Crée les descriptions

$descriptionFr = new ProjetInt();

$descriptionEn = new ProjetInt();

//Associe le projet et les descriptions

$nouveauProjet->addDescription($descriptionFr);

$nouveauProjet->addDescription($descriptionEn);

$descriptionFr->setProjet($nouveauProjet);

$descriptionEn->setProjet($nouveauProjet);

//Formulaires pour la création d'un projet

$formulaire=$this->createForm(new ProjetType(), $nouveauProjet);

//On surveille le formulaire

$formulaire->handleRequest($request);

//Si le formulaire est valide

if($formulaire->isValid())

{

//Indique le bon code de langue pour les descriptions

$descriptionFr->setLangue('fr');

$descriptionEn->setLangue('en');

//Applique les modifications

$em=$this->getDoctrine()->getManager();

$em->persist($nouveauProjet);

$em->flush();

...

}

}

It's just a detail, but it REALLY annoys me. I really want to set my language outside of if($formulaire->isValid())

网友答案:

I found a WAY BETTER way to do this. In the form, I gave the language field the attribute 'read_only'=>true. This allowed me to change my controller to be exactly as I wanted it. I don't need to use the line {% do description.language.setRendered %} and I can just use what is written in the language field to display the language of the description (fr = french and en=english). The only thing that is needed is to use some CSS to hide the field language and it doesn't matter if someone tweak the CSS to display it since they can't edit the field anyway. Voilà!

Here is the code:

the form:

<?php

namespace AdminBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

Class ProjetIntType extends AbstractType
{

    public function buildForm(FormBuilderInterface $constructeur, array $options)
    {
        $constructeur
        ->add('langue', 'text', array('read_only'=>true))
        ->add('nom', 'text')
        ->add('descriptionCours', 'text')
        ->add('descriptionComplete', 'text')
        ->add('roles', 'text')
        ->add('aptitudesDeveloppees', 'text');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'PublicBundle\Entity\ProjetInt',
            ));
    }

    public function getName()
    {

        return 'projetInt';

    }

}

The controller:

public function addAction(Request $request)
  {
    //New project
    $nouveauProjet = new Projet();

    //New descriptions
    $descriptionFr = new ProjetInt();
    $descriptionFr->setLangue('fr');
    $descriptionFr->setProjet($nouveauProjet);

    $descriptionEn = new ProjetInt();
    $descriptionEn->setLangue('en');
    $descriptionEn->setProjet($nouveauProjet);

    //Add the descriptions to the project
    $nouveauProjet->addDescription($descriptionFr);
    $nouveauProjet->addDescription($descriptionEn);

    ...
}

My twig:

{{ form_start(formulaire, {'attr': {'novalidate': 'novalidate'}}) }}
    {#  #}
    {{ form_row(formulaire.imageFile) }}
    {{ form_row(formulaire.technologie) }}
    {{ form_row(formulaire.annee) }}
    {{ form_row(formulaire.type) }}
    {{ form_row(formulaire.fichierFile) }}
    {{ form_row(formulaire.largeur) }}
    {{ form_row(formulaire.hauteur) }}
    {{ form_row(formulaire.categories) }}
    {#  #}
    <h3>Descriptions</h3>
    <ul class="descriptions">
        {% for description in formulaire.descriptions %}
            {% if description.langue.vars.value == 'fr' %}
                Français
            {% elseif description.langue.vars.value == 'en' %}
                Anglais
            {% endif %}
            <li>    
                {{ form_row(description.langue) }}
                {{ form_row(description.nom) }}
                {{ form_row(description.descriptionCours) }}
                {{ form_row(description.descriptionComplete) }}
                {{ form_row(description.roles) }}
                {{ form_row(description.aptitudesDeveloppees) }}
            </li>
        {% endfor %}
    </ul>

    <input type="submit" value="{{ textBtn }}"/>
{{ form_end(formulaire) }}
分享给朋友:
您可能感兴趣的文章:
随机阅读: