SnelleJelle
SnelleJelle

Reputation: 963

how get form field label from translation?

I'm trying to implement translations for my forms. I've already translated error messages, but I can't seem to translate form field labels. This is what I have:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add("title", TextType::class, array(
            "label" => ""vacancy.label.title"",
            "constraints" => array(
                new Length(array(
                    "min" => 4,
                    "max" => 100,
                    "minMessage" => "vacancy.title.min_message",
                    "maxMessage" => "vacancy.title.max_message"),
                new NotBlank(array("message" => "vacancy.not_blank"))
        ))))
        ->add("description", TextareaType::class, array(
            "label" => "Omschrijving",
            "constraints" => array(
                new Length(array(
                    "min" => 20,
                    "max" => 2000,
                    "minMessage" => "vacancy.description.min_message",
                    "maxMessage" => "vacancy.description.max_message"),
                new NotBlank(array("message" => "vacancy.not_blank"))
        ))))
        ->add("startdate", DateType::class, array(
            "label" => "Begindatum",
            "widget" => "single_text",
            "constraints" => array(
                new Date(array(
                    "message" => "vacancy.date.message"
        )))))
        ->add("enddate", DateType::class, array(
            "label" => "Einddatum",
            "widget" => "single_text",
            "constraints" => array(
                new Date(array(
                    "message" => "vacancy.date.message"
        )))))
        ->add("submit", SubmitType::class, array(
            "label" => "Opslaan"
        ));
}

translation:

vacancy:
not_blank: Gelieve een waarde op te geven.
title:
    min_message: Gelieve minimum {{ limit }} tekens in te geven.
    max_message: Gelieve maximum {{ limit }} tekens in te geven.
description:
    min_message: Gelieve minimum {{ limit }} tekens in te geven.
    max_message: Gelieve maximum {{ limit }} tekens in te geven.
date:
    message: Gelieve een geldige datum op te geven.
    name: Opslaan
label:
    title: Titel
    description: Omschrijving
    startdate: Begindatum
    enddate: Einddatum
    submit: Opslaan

From what I have gathered so far I can deduct that this should be done in twig like so:

{{ form_start(form) }}
    {{ form_errors(form) }}

    <div>
    {{ form_label("vacancy.label.title"|trans }}
    {{ form_errors(form.title) }}
    {{ form_widget(form.title) }}
    </div>
    <div>
    {{ form_label(form.description) }}
    {{ form_errors(form.description) }}
    {{ form_widget(form.description) }}
    </div>
    <div>
    {{ form_label(form.startdate) }}
    {{ form_errors(form.startdate) }}
    {{ form_widget(form.startdate) }}
    </div>
    <div>
    {{ form_label(form.enddate) }}
    {{ form_errors(form.enddate) }}
    {{ form_widget(form.enddate) }}
    </div>
    <div>
    {{ form_widget(form.submit) }}
    </div>
{{ form_end(form) }}

But that doesn't work, what am I doing wrong here?

Upvotes: 0

Views: 219

Answers (1)

SnelleJelle
SnelleJelle

Reputation: 963

found it, I needed to tell it to fetch from translations like so:

->add("enddate", DateType::class, array(
            "label" => "vacancy.label.enddate",
            "translation_domain" => "validators", // tell it to look in translations
            "widget" => "single_text",
            // stuff ...

Upvotes: 1

Related Questions