我有一个带有一个可选字段(备注)的表单。默认情况下,所有都是必填字段我想让'remark'字段可选。我尝试了很多解决方案。似乎没有人工作。如何使其成为可选项?
这是我的代码
RequestItem.php
/**
* RequestItem
*
* @ORM\Table(name="request_item")
* @ORM\Entity(repositoryClass="InventoryBundle\Repository\RequestItemRepository")
*/
class RequestItem
{
/**
* @var string
*
* @ORM\Column(name="remark", type="text", nullable=true)
*/
private $remark;
RequestItemType.php
class RequestItemType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('item', EntityType::class, array(
'class' => 'InventoryBundle:Item',
'choice_label' => 'name',
'expanded' => false,
'multiple' => false,
),
array('attr' =>array('class' =>'form-control', 'style' =>'margin-bottom:15px; width:100px')))
->add('date', DateType::class, array('attr' =>array('class' =>'form-control', 'style' =>'margin-bottom:15px')))
->add('quantity', TextType::class,array('attr' =>array('class' =>'form-control', 'style' =>'margin-bottom:15px; width:75px')))
->add('client', TextType::class,array('attr' =>array('class' =>'form-control', 'style' =>'margin-bottom:15px; width:200px')))
->add('remark', TextareaType::class, array('attr' =>array('class' =>'form-control', 'style' =>'margin-bottom:15px', 'required' => false), 'empty_data' => null))
;
}
我把'required'=>对于评论领域是假的。
new.html.twig
{% extends 'base.html.twig' %}
{% form_theme form _self %}
{% use 'form_div_layout.html.twig' with form_label as base_form_label %}
{% block form_label %}
{{ block('base_form_label') }}
{% if required %}
<span class="required" title="This field is required" style="color:red">*</span>
{% endif %}
{% endblock %}
{% block body %}
<h1>Request Item creation</h1>
{% if is_granted("ROLE_SUPER_ADMIN") %}
{% if app.session.flashBag.has('warning') %}
<div class="alert alert-danger" role="alert">
{% for msg in app.session.flashBag.get('warning') %}
{{ msg }}
{% endfor %}
</div>
{% endif %}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_row(form.remark, {'required': false}) }}
<input type="submit" value="Create" class="btn btn-primary" />
{{ form_end(form) }}
<hr />
<a href="{{ path('requestitem_index') }}" class="btn btn-success">Back to the list</a>
{% else %}
<h4>Please <a href="{{ path('fos_user_security_login') }}" class="btn btn-default">login</a></h4>
{% endif %}
{% endblock %}
我在twig文件中添加{{form_row(form.remark,{'required':false})}}。
备注字段的页面源视图
<div> <label for="request_item_remark" class="required">Remark</label>
<span class="required" title="This field is required" style="color:red">*</span>
<textarea id="request_item_remark" name="request_item[remark]" required="required" class="form-control" style="margin-bottom:15px" ></textarea></div>
textarea字段是必需的。
我还尝试添加new.html.twig文件
{% extends 'base.html.twig' %}
{% form_theme form _self %}
{% block textarea_widget %}
<div class="textarea_widget">
{% set required=required|default('not required') %}
{{ block('form_widget_simple') }}
</div>
{% endblock %}
奇怪的是,textarea字段成为输入文本字段,仍然需要它。
<div> <label for="request_item_remark" class="required">Remark</label>
<span class="required" title="This field is required" style="color:red">*</span>
<div class="textarea_widget">
<input type="text" id="request_item_remark" name="request_item[remark]" required="required" class="form-control" style="margin-bottom:15px" />
</div>
表格结构
CREATE TABLE `request_item` (
`id` int(11) NOT NULL,
`item_id` int(11) DEFAULT NULL,
`date` date NOT NULL,
`quantity` int(11) NOT NULL,
`client` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`remark` longtext COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
答案 0 :(得分:1)
不要将required
放在attr
中,它应该位于选项的顶层。
...
->add('remark', TextareaType::class, array('required' => false, 'attr' =>array('class' =>'form-control', 'style' =>'margin-bottom:15px'), 'empty_data' => null))
...
否则,如果您通过属性设置了此元素,并且required
选项没有意义。