我有以下实体:
class Agency extends AbstractType {
const DELEGACION = 1;
const AGENCIA_TERRITORIAL = 2;
const NOT_DELETED = 0;
const DELETED = 1;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, unique=true)
*
* @Assert\NotBlank()
*/
private $name;
/**
* @var int
*
* @ORM\Column(name="type_agency", type="integer")
*
* @Assert\NotBlank()
*/
private $typeAgency;
/**
* @var string
*
* @ORM\Column(name="phone", type="string", length=255, nullable=true)
* @Assert\Regex(
* pattern="^[0-9]*$",
* match=true,
* message="error.type_message"
* )
*/
private $phone;
/**
* @var int
*
* @ORM\Column(name="postal_code", type="string", nullable=true)
* @Assert\Regex(
* pattern="/^\d+/",
* match=true,
* message="error.type_message"
* )
*/
private $postalCode;
//Getters and setters...
}
如您所见,我使用Assert Regex对邮政编码变量进行了验证。
当我使用无效字符串发送此表单时,我的Symfony工具栏上出现错误。但我试图在输入下显示这个错误,但似乎是不可能的。
我只能使用{{ form_errors(form_agency) }}
显示错误,但我想要的是使用{{ form_errors(form_agency.postal_code) }}
显示,但使用此功能时,不会发生任何事情。
这是我的树枝模板:
{{ form_start(form_agency) }}
{#{{ form_errors(form_agency) }}#}
<div>
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>{% trans %}agencies.basic_data{% endtrans %}</h5>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
</a>
</div>
</div>
<div class="ibox-content">
<div class="row">
<div class="col-lg-6">
<div class="mb-10">
<label>{% trans %}agencies.name{% endtrans %}</label><span class="field_required">*</span>
{{ form_widget(form_agency.name) }}
{{ form_errors(form_agency.name) }}
</div>
</div>
<div class="col-lg-6">
<div class="mb-10">
<label>{% trans %}agencies.type_agency{% endtrans %}</label><span
class="field_required">*</span>
{{ form_widget(form_agency.type_agency) }}
{{ form_errors(form_agency.type_agency) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="mb-10 chosen-container2">
<label>{% trans %}agencies.territories{% endtrans %}</label><span
class="field_required">*</span></br>
{{ form_widget(form_agency.territories) }}
{{ form_errors(form_agency.territories) }}
</div>
</div>
<div class="col-lg-6">
<div class="mb-10">
<label>{% trans %}agencies.parent{% endtrans %}</label>
{{ form_widget(form_agency.parent) }}
{{ form_errors(form_agency.parent) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="mb-10">
{% if agency.picture != null %}
<img alt="logo"
src="{{ asset('/bundles/AppBundle/images/uploads/pictures/'~agency.picture.filename) }}"
style="width: 100px;"/>
<br><br>
{% endif %}
<label>{% trans %}agencies.picture{% endtrans %}</label>
{{ form_widget(form_agency.picture) }}
{{ form_errors(form_agency.picture) }}
</div>
</div>
<div class="col-lg-6">
<div class="mb-10">
<label>{% trans %}status{% endtrans %}</label><span
class="field_required">*</span><br>
{{ form_widget(form_agency.status) }}
{{ form_errors(form_agency.status) }}
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="ibox">
<div class="ibox-title">
<h5>{% trans %}agencies.location_data{% endtrans %}</h5>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
</a>
</div>
</div>
<div class="ibox-content">
<div class="row">
<div class="col-lg-6">
<div class="mb-10">
<label>{% trans %}agencies.address{% endtrans %}</label>
{{ form_widget(form_agency.address, {'id':'address'}) }}
{{ form_errors(form_agency.address) }}
</div>
</div>
<div class="col-lg-6">
<div class="mb-10">
<label>{% trans %}agencies.postal_code{% endtrans %}</label>
{{ form_widget(form_agency.postal_code, {'id':'zip_code'}) }}
{{ form_errors(form_agency.postal_code) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="mb-10">
<label>{% trans %}agencies.city{% endtrans %}</label>
{{ form_widget(form_agency.city, {'id':'city'}) }}
{{ form_errors(form_agency.city) }}
</div>
</div>
<div class="col-lg-6">
<div class="mb-10">
<label>{% trans %}agencies.country{% endtrans %}</label>
{{ form_widget(form_agency.country) }}
{{ form_errors(form_agency.country) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="mb-10">
<label>{% trans %}agencies.phone{% endtrans %}</label>
{{ form_widget(form_agency.phone) }}
{{ form_errors(form_agency.phone) }}
</div>
</div>
<div class="col-lg-6">
<div class="mb-10">
<label>{% trans %}agencies.email{% endtrans %}</label>
{{ form_widget(form_agency.email) }}
{{ form_errors(form_agency.email) }}
</div>
</div>
</div>
<div class="row pt-10">
<div class="col-lg-12 pb-10">
<div class="mb-10">
<button id="find_map" class="button button ">{% trans %}map{% endtrans %}</button>
</div>
</div>
{{ form_widget(form_agency.latitude, {'id':'latitude'}) }}
{{ form_widget(form_agency.longitude, {'id':'longitude'}) }}
<div class="pt-10">
<div class="col-sm-12">
<div id="map-canvas2" style="height:300px; border:1px solid black;"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
所以,问题是我收到邮政编码的错误,我可以使用全局form_errors(form)
呈现错误,但我无法使用form_errors(form.field)
呈现表单字段的特定错误
我做错了什么?
答案 0 :(得分:1)
您正在尝试使用数据库中的字段名称。这是学说。它应该是:
{{form_errors(form_agency.postalCode)}}
答案 1 :(得分:0)
{% if form_agency.name.vars.errors|length %}
<h3>Error</h3>
<p>{{ form_errors(form_agency.name) }}!</p>
{% endif %}