0

Symfony では、検証をフィールドまたは入力の両方にリンクできるため、エラーはform.vars.errorおよびで返されます。form.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 に答える