[英]Symfony2 choice with static list
I want to use a select box with predefined values. 我想使用具有预定义值的选择框。 I have a Task entity: 我有一个Task实体:
namespace xx\xxx\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Task
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="xx\xxx\Entity\TaskRepository")
*/
class Task
{
////
/**
* @Assert\Choice(choices = {"MONTHLY", "WEEKLY"}, message = "Select frequency")
* @ORM\Column(name="frequency", type="text")
*/
private $frequency;
}
When using TaskType: 使用TaskType时:
$builder->add('frequency');
shows input, which validates correctly - allows only WEEKLY and MONTHLY values. 显示正确验证的输入 - 仅允许WEEKLY和MONTHLY值。 But I want to use select box. 但我想使用选择框。 I have tried: 我努力了:
$builder->add('frequency', 'collection', array(
'type' => 'choice',
'options' => array(
'choices' => array(
'MONTHLY' => 'Monthly',
'WEEKLY' => 'Weekly',
)
)
)
)
or: 要么:
$builder->add('frequency', 'collection', array(
'type' => 'choice',
)
)
but both ways I only get a label with no select box. 但两种方式我只得到一个没有选择框的标签。 What I am doing wrong? 我做错了什么? Maybe there is better approach? 也许有更好的方法?
Try to do it this way: 尝试这样做:
$builder->add('frequency', 'choice', array(
'choices' => array(
'MONTHLY' => 'Monthly',
'WEEKLY' => 'Weekly'
)
));
More on choice
field type here - http://symfony.com/doc/current/reference/forms/types/choice.html 有关choice
字段类型的更多信息,请访问http://symfony.com/doc/current/reference/forms/types/choice.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.