在 twig 中获取我的 select 的内容
Get the content of my select in twig
我正在做我的 Symfony 2 项目,我正在做我的一个表单的树枝。我想知道如何检查我表单中的 select 是否至少有一个选项。
我的表单如下所示:
<?php
namespace AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
Class ProjetsType extends AbstractType
{
public function buildForm(FormBuilderInterface $constructeur, array $options)
{
$constructeur
->add('Ajouter', 'submit', array('label'=>'Ajouter un projet'))
->add('Projet', 'entity', array(
'label'=>false,
'class'=>'PublicBundle\Entity\Projet',
'property'=>'id'
))
->add('Modifier', 'submit')
->add('Supprimer', 'submit');
/*This is the way I do it if I pass my projects as a parameter*/
/*if(!empty($options['choix']))
{
$constructeur
->add('Projet', 'choice', array('label'=>false, 'choices'=>$options['choix']))
->add('Modifier', 'submit')
->add('Supprimer', 'submit');
}*/
}
public function getName()
{
return 'projets_type';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'choix' => null,
));
}
}
我的模板是这样的
{{ form_start(formulaire) }}
{{ form_widget(formulaire.Ajouter) }}
{% if formulaire.Projet is defined %}
{{ form_widget(formulaire.Projet) }}
{{ form_widget(formulaire.Modifier) }}
{{ form_widget(formulaire.Supprimer) }}
{% endif %}
{{ form_end(formulaire) }}
我的目标是,如果没有任何项目,只会出现 'ajouter'(法语表示添加)按钮。我以前用来传递我的项目列表,我得到了另一个功能,如果至少有一个项目,我的表单只是添加了 select,但我被告知最好使用实体类型场地。使用实体类型字段可能会导致 select 字段没有任何选项。如果发生这种情况,我希望不显示 select 和以下两个按钮。
所以我想最好的方法是检查 select 是否包含至少一个选项,如果不包含,请不要在我的模板中添加该部分表单。我该怎么做?
试试这个:
{% if (formulaire.Projet.vars['choices'] | length > 0) %}
// empyty select
{% else %}
// not empty select
{% endif %}
检查这个问题:
和文档:http://symfony.com/doc/current/reference/forms/twig_reference.html#twig-reference-form-variables
我正在做我的 Symfony 2 项目,我正在做我的一个表单的树枝。我想知道如何检查我表单中的 select 是否至少有一个选项。
我的表单如下所示:
<?php
namespace AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
Class ProjetsType extends AbstractType
{
public function buildForm(FormBuilderInterface $constructeur, array $options)
{
$constructeur
->add('Ajouter', 'submit', array('label'=>'Ajouter un projet'))
->add('Projet', 'entity', array(
'label'=>false,
'class'=>'PublicBundle\Entity\Projet',
'property'=>'id'
))
->add('Modifier', 'submit')
->add('Supprimer', 'submit');
/*This is the way I do it if I pass my projects as a parameter*/
/*if(!empty($options['choix']))
{
$constructeur
->add('Projet', 'choice', array('label'=>false, 'choices'=>$options['choix']))
->add('Modifier', 'submit')
->add('Supprimer', 'submit');
}*/
}
public function getName()
{
return 'projets_type';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'choix' => null,
));
}
}
我的模板是这样的
{{ form_start(formulaire) }}
{{ form_widget(formulaire.Ajouter) }}
{% if formulaire.Projet is defined %}
{{ form_widget(formulaire.Projet) }}
{{ form_widget(formulaire.Modifier) }}
{{ form_widget(formulaire.Supprimer) }}
{% endif %}
{{ form_end(formulaire) }}
我的目标是,如果没有任何项目,只会出现 'ajouter'(法语表示添加)按钮。我以前用来传递我的项目列表,我得到了另一个功能,如果至少有一个项目,我的表单只是添加了 select,但我被告知最好使用实体类型场地。使用实体类型字段可能会导致 select 字段没有任何选项。如果发生这种情况,我希望不显示 select 和以下两个按钮。
所以我想最好的方法是检查 select 是否包含至少一个选项,如果不包含,请不要在我的模板中添加该部分表单。我该怎么做?
试试这个:
{% if (formulaire.Projet.vars['choices'] | length > 0) %}
// empyty select
{% else %}
// not empty select
{% endif %}
检查这个问题:
和文档:http://symfony.com/doc/current/reference/forms/twig_reference.html#twig-reference-form-variables