translate required fields

edited April 2012 in Modules
public $required = array('title', 'content', 'author_id');

and i get error like: Please fill out the required field 'author id'

how can i translate this fields?. I know that i can use
'author_id' => Please fill out the required field 'Avtor'

but i would like to add this in language file. Can i somehow translate just title to 'Naslov', content to ' Vsebina' and author_id to 'Avtor' that message will be like Please fill out the required field 'Naslov' or 'Vsebina' or 'Avtor',... and not translating whole text?

Comments

  • edited 7:03AM
    There are a couple ways to do it. The first is you can edit the "default_required_message" property on the model like so:
    public $default_required_message = "Please fill out the required field '%1s'"
    The second way is to use a key/value in the required array like so:
    public $required = array('title' => 'Please fill out the title field.', 'content' => 'Please fill out the content field', 'author_id' => 'Please fill out the author field');
  • edited April 2012
    Hi, thank you. I already try second way but 'Please fill out the title field' is static text and i can not translate it later to our language. Functio lang() not working here. So i can not use

    public $required = array('title' => lang('translate'));

    becouse I get error. Can i somehow use lang() function here?

    With first option i also don't know how to translate title, content,... to our language without using static Names. I want to use names in lang files.
  • edited April 2012
    What's the error?

    You need to do it in the __contruct() not the class member definitions.

    So at the top:

    public $required = array();

    __construct():

    function __construct() { $this->required = array( 'title' => lang('string_1'), 'content' => lang('string_2'), 'author_id' => lang('string_3') ); parent::__construct('your_table_name'); }

    Just tested that and it works for me.
  • edited April 2012
    Yes this works, thanks. But I modify MY_Model.php

    // convert required fields to rules foreach($required as $key => $val) { if (is_int($key)) { $field = $val; $lang = lang('form_label_'.$val); if ($lang == '') { $lang = str_replace('_', ' ', $val); } //$msg = sprintf($this->default_required_message, str_replace('_', ' ', $lang)); $msg = sprintf($this->default_required_message, $lang); } else { $field = $key; $msg = $val; } $this->rules[] = array($field, 'required', $msg); }

    so fields name are now selected from language files
Sign In or Register to comment.