0

I have the following entity:

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...
}

As you see, I have a validation on postal code variable, using Assert Regex.

When I send this form with not valid string, I get an error on my Symfony Toolbar. But I am trying to display this error under the input, but seems imposible.

I only can display the error using {{ form_errors(form_agency) }}, but what I want is display using {{ form_errors(form_agency.postal_code) }}, but using this, nothing happends.

This is my twig template:

{{ 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>

So, the problem is that I am getting the error of Postal Code, I can render the error using global form_errors(form), but I'm unable to render specific error of a form field using form_errors(form.field)

What I'm doing wrong?

2 Answers 2

1

You're trying to use field names from your database. This is doctrine. It should be:

{{ form_errors(form_agency.postalCode) }}

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, this change generates the following exception: Neither the property "postalCode" nor one of the methods "postalCode()", "getpostalCode()"/"ispostalCode()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" in agencies/_basicData.html.twig at line 106. Notice that I'm setting on the entity the attribute name=postal_code.
Yeah, the name="postal_code" is telling doctrine that is the field name in the database. The entity named it postalCode, thus your "private $postalCode;" in the entity. Check your getters and setters. From the console try: bin/console doctrine:generate:entities AppBundle . You might need to start that console line with 'php' depending on how your system is setup.
You are right, I solved my problem, I was using in my form postal_code; using in this form postalCode instead, your code works great in Twig. Thanks!
Be sure to use the object names from your entity, not the field names. Single words like "name" or "age" would work fine but when you have field names with underscores such as "postal_code" or "create_date" they get changed to camelcase versions "postalCode" and "createDate". My advice, especially in the beginning is to use the console to create your entities "bin/console doctrine:generate:entity" which will walk you through setting up the entity. It will also create your getters and setters. Then use the command I showed you to update should you make changes
0
{% if form_agency.name.vars.errors|length %}
    <h3>Error</h3>
    <p>{{ form_errors(form_agency.name) }}!</p>
{% endif %}

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.