gpt4 book ai didi

forms - 在symfony2中,是否有单个变量或方法(例如 'hasError')来检查表单是否返回任何错误?

转载 作者:行者123 更新时间:2023-12-03 08:33:37 26 4
gpt4 key购买 nike

在Symfony中,您可以将验证链接到字段或输入,以便在form.vars.errorform.fielName.vars.errors中返回错误

在模板中,我必须进行所有这些检查,以检查表单是否在其任何字段上返回任何错误

{% set oneOrMoreFieldsEmpty = form.title.vars.value == ''
or form.location.vars.value == ''
or form.zip.vars.value == ''
or form.city.vars.value == ''
or form.address.vars.value == ''
or form.description.vars.value == ''
or form.type.vars.value == ''
or form.dressCode.vars.value == ''
or (form.registrationTypeMember.vars.value == 2 and form.costMember.vars.value == '')
or (form.guestAllowed.vars.value == 1 and form.guestPerParticipant.vars.value is empty )
or (form.targetAgeGroup.vars.value == 1 and (form.ageMin.vars.value == '' or form.ageMax.vars.value == ''))
or (form.numberOfPlacesLimited.vars.value == 1 and form.maxTotalParticipant.vars.value == '') %}

{% set oneOrMoreFieldsInvalid = (form.title.vars.value != '' and form.title.vars.errors|length > 0)
or (nInvalidFields > 0 )
or (form.startAt.vars.errors|length > 0)
or (form.endAt.vars.errors|length > 0)
or (form.location.vars.value != '' and form.location.vars.errors|length > 0)
or (form.zip.vars.value != '' and form.zip.vars.errors|length > 0)
or (form.city.vars.value != '' and form.city.vars.errors|length > 0)
or (form.description.vars.value != '' and form.description.vars.errors|length > 0)
or (form.type.vars.value != '' and form.type.vars.errors|length > 0)
or (form.dressCode.vars.value != '' and form.dressCode.vars.errors|length > 0)
or (form.registrationTypeMember.vars.value == 2 and form.costMember.vars.value != '' and form.costMember.vars.errors|length > 0)

or (form.guestAllowed.vars.value == 1 and not(form.guestPerParticipant.vars.value is empty) and form.guestPerParticipant.vars.errors|length > 0)
or (form.targetAgeGroup.vars.value == 1 and form.ageMin.vars.value != '' and form.ageMax.vars.value != '' and (form.ageMin.vars.errors|length>0 or form.ageMax.vars.errors|length>0 or form.targetAgeGroup.vars.errors|length>0))
or (form.numberOfPlacesLimited.vars.value == 1 and not(form.maxTotalParticipant.vars.value is empty) and form.maxTotalParticipant.vars.errors|length > 0) %}

{% if (hasErrors == true and ( oneOrMoreFieldsEmpty or oneOrMoreFieldsInvalid ) ) %}
<div class="notice error" >
{% if (oneOrMoreFieldsEmpty) %}
<div>{{ 'create.msg.emptyfields'|trans({},'activity')|raw }}</div>
{% endif %}

{% if (nInvalidFields>0) %}
<div>{{ 'create.msg.invalidfields'|trans({},'activity')|raw }}</div>
{% endif %}
</div>
{% endif %}

所有这些行仅是为了在表单的开头显示一条通用消息,并在表单的后面部分在字段输入下方显示适当的错误。

挺吓人的吧?

那么,有什么方法可以检查表单是否在表单或字段上返回任何错误?

提前致谢

最佳答案

您必须在 Controller 或 super Controller 中添加此方法:

protected function getErrorMessages(\Symfony\Component\Form\Form $form) {
$errors = array();

if ($form->hasChildren()) {
foreach ($form->getChildren() as $child) {
if (!$child->isValid()) {
$errors[$child->getName()] = $this->getErrorMessages($child);
}
}
} else {
foreach ($form->getErrors() as $key => $error) {
$errors[] = $error->getMessage();
}
}

return $errors;
}

关于forms - 在symfony2中,是否有单个变量或方法(例如 'hasError')来检查表单是否返回任何错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16447399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com