How to make all translations required for the given field?
Created 6 years ago by vargvinter

Hello,

Example: I have three languages enabled in my site: English, German, Polish. English is default language. I have translatable text field in the form called "name". To submit this form I need to enter "name" value for english locale. But I want also german and polish locale to be required as well. So I put this code in FormBuilder class:

    /**
     * Additional validation rules.
     *
     * @var array|string
     */
    protected $rules = [
        'name_de' => [
            'required'
        ],
        'name_pl' => [
            'required'
        ],
    ];

It works. Is it correct approach?

Next I want to have red asterix (star) for german and polish locale in the label (like for english locale) How to do that?

piterden  —  6 years ago

I believe it should be

    /**
     * Additional validation rules.
     *
     * @var array|string
     */
    protected $rules = [
        'de.name' => [
            'required',
        ],
        'pl.name' => [
            'required',
        ],
    ];

Or even

    /**
     * Additional validation rules.
     *
     * @var array|string
     */
    protected $rules = [
        'de' => [
            'name' => [
                'required',
            ],
        ],
        'pl' => [
            'name' => [
                'required',
            ],
        ],
    ];
ryanthompson  —  6 years ago

@piterden nice one! @vargvinter let us know if that does the trick. This portion of the rules is generally automated but we'll sort it out 😊

vargvinter  —  6 years ago

Nope. Both snippets provided by @piterden don't work. Take a look into source code of generated form. There are text inputs with names: name_en, name_de, name_pl. So we must use them in validation rules.

piterden  —  6 years ago

Take a look into source code of generated form. +

piterden  —  6 years ago

vendor/anomaly/streams-platform/src/Ui/Form/FormRules.php#L92