0

在 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 %}

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

很吓人吧?

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

提前致谢

4

1 回答 1

1

您必须在控制器或超级控制器中添加此方法:

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;
}
于 2013-05-08T18:11:45.417 回答