I'm adding validation errors to fields using addErrors($record->getErrors()) which then read like e.g. "My Field cannot be blank."

But I'm wondering where I can translate the field name, which I think is generated from the ID attribute myField?



<input type="hidden" name="action" value="example/saveExample">

{{ forms.textField({
    label: 'My Field'|t,
    id: 'myField',
    name: 'myField',
    value: null,
    errors: example is defined ? example.errors('myField'),
}) }}

Service function called from actionSaveExample:

public function saveExample(ExampleModel $example)
    $exampleRecord = ExampleRecord::model()->findById($example->id);
    $exampleRecord->myField = $example->myField;

    if ($exampleRecord->validate())
        // save

        return false;

I just ran into the same problem.

In the defineAttributes() method in your model, you can set a "label", like so:

class Plugin_ExampleModel extends BaseModel
    protected function defineAttributes()
        return array(
            'quality'  => array(
                'label' => Craft::t('Quality'),
                'min' => 0,
                'max' => 100,
                'required' => TRUE

By using Craft::t() on that label, it will be translated in your template when the error is shown. Of course the corresponding label should be present in your plugin's translations file.

Hope this will help!

  • Definitely helps, thanks! I'm validating my record, so I had to add it to my record attributes. – carlcs May 19 '15 at 13:52

Craft/Yii sees that string as:

'{attribute} cannot be blank.' => '{attribute} darf nicht leer sein.',

Where {attribute} is the dynamic part - in this case your field handle.

If I'm understanding your question correctly, there isn't a way to translate that field handle in this context because you're in charge of what it's called to begin with.

I would assume, for example, if you're using the German locale, that you would already have a German context field handle name in place already.

  • Yes. It's a plugin but its not a custom error message that I can translate but I think something coming from Yii? – carlcs May 13 '15 at 14:40
  • Right... Yii won't know how to translate 'myField' ahead of time, though. By adding this to your plugin, you're effectively overriding Yii's translation with your own with 'myField' translated. – Brad Bell May 13 '15 at 14:42
  • Sounds good, Brad. But it doesn't work, I got everything else translated but having no luck with the error messages. :( – carlcs May 13 '15 at 14:52
  • I just noticed that except element fields non / few of the core field names are being translated either within validation error messages. I checked widgets, users and usergroups. – carlcs May 13 '15 at 15:28
  • Sorry, now that I've had coffee and re-read your question, I've updated my answer. – Brad Bell May 13 '15 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.