Welcome Guest, Not a member yet? Register   Sign In
Form validation error messages from language file?
#1
Question 

Hi guys,

I'm trying to pull error messages from a lang file for my form validation. I've got the validation in a config file which looks like this:

PHP Code:
$config = array(
    
'auth_c/login' => array(
        array(
            
'field' => 'identity',
            
'label' => 'lang:identity',
            
'rules' => array(
                
'required',
                array(
                    
'is_valid_identity_callable',
                    function(
$str) {
                        
$ci =& get_instance();
                        return 
$ci->auth->is_valid_identity($str);
                    }
                )
            ),
            
'errors' => array(
                
'required' => 'lang:error_missing_param',
                
'is_valid_identity_callable' => 'lang:error_auth_identity_used'
            
)
        ),
        array(
            
'field' => 'password',
            
'label' => 'lang:password',
            
'rules' => array(
                
'required'
            
),
            
'errors' => array(
                
'required' => 'lang:error_missing_param',
            )
        )
    )
); 

From the docs, I saw I could do "lang:identity" for the field label - and that works great - but the same thing doesn't work for error messages.

I've tried a few different ways but can't seem to get it to load from the lang file with a setup like this in a form_validation.php config file.

This seems like a common scenario, so I'm sure there's a way to do it, but I just can't seem to find it.

Any help would be greatly appreciated.


Thanks,
Jay.
Reply
#2

Error messages for form_validation are always loaded in language specified in config. Therefore if you need to change the language for form_validation messages, you need to change this value using
PHP Code:
$this->config->set_item('language''slovak'); 
Reply
#3

Hi,

I think you misunderstood my question - I'm not trying to change the language of error messages.

I'm trying to set my own custom error messages for my custom form validation rules and I want them to pull from the lang files, rather than have to hard-code them into the form_validation.php config file.

Appreciate your time.
Reply
#4

Does anyone have a solution to this issue? This seems like a necessity for sites supporting multiple languages?
Reply
#5

When you say you have a custom validation rule. Have you created it as a callback function or have you extended the library with more rules?

If you do the latter, extending the form validation library, you can just add the error message to the form_validation_lang.php lang file.

Here is an example of extending the form validation library
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
MY_Form_validation extends CI_Form_validation
{
 
    function __construct($config = array())
 
    {
 
         parent::__construct($config);
 
    }

 
   /**
     * Alpha Dash Space
     *
     * Validates form input, allows alpha, numeric, dash, underscore and spaces
     *
     * @param  string
     *
     * @return   bool
     */
 
   function alpha_dash_space($str)
 
   {
 
       return (bool) preg_match("/^([-a-z0-9_ ])+$/i"$str);
 
   }


Then copy the form_validation_lang.php language file from system/languages/<lang>/ to the language folder in the application folder, application/languages/<lang>/

Then add a new line below the current lines. It would look like this for the rule above.
PHP Code:
$lang['form_validation_alpha_dash_space'] = 'my error message'

Hope that helps.
Reply
#6

(04-12-2015, 10:27 AM)silentium Wrote: When you say you have a custom validation rule. Have you created it as a callback function or have you extended the library with more rules?

If you do the latter, extending the form validation library, you can just add the error message to the form_validation_lang.php lang file.

Here is an example of extending the form validation library


PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
MY_Form_validation extends CI_Form_validation
{
 
    function __construct($config = array())
 
    {
 
         parent::__construct($config);
 
    }

 
   /**
     * Alpha Dash Space
     *
     * Validates form input, allows alpha, numeric, dash, underscore and spaces
     *
     * @param  string
     *
     * @return   bool
     */
 
   function alpha_dash_space($str)
 
   {
 
       return (bool) preg_match("/^([-a-z0-9_ ])+$/i"$str);
 
   }


Then copy the form_validation_lang.php language file from system/languages/<lang>/ to the language folder in the application folder, application/languages/<lang>/

Then add a new line below the current lines. It would look like this for the rule above.


PHP Code:
$lang['form_validation_alpha_dash_space'] = 'my error message'

Hope that helps.

Hello,

I just moved from CI 2.2 to CI 3 and I have the probleme in my MY_Form_validation. I tryed your code, but I ever have the same problem.
My code :
PHP Code:
class MY_Form_validation extends CI_Form_validation {

 
/**
 * Valid a Date
 * Contrôle qu'une date soit bien une chaine valide au format passé en paramètre
 *
 * @access public
 * @param string - $str
 * @param   string - $format (ex : 'j/m/Y')
 * @return bool
 */
 
public function valid_date($str$format)
 
   {
 if (empty(
$str)) {
 
           return TRUE;
 
       }

 
       if (DateTime::createFromFormat($format$str) == FALSE) {
 
           // $this->set_message('valid_date', 'Le champ {field} n\'est pas une date valide.');
 
           return FALSE;
 
       }
 
       return TRUE;
 }



An I added this in my form_validation_lang.php :
PHP Code:
$lang['form_validation_valid_date'           "The field {field} must be a valid date."
Always the same problem.

Any idea ?
Reply
#7

You are missing the __construct() function in your MY_Form_validation class.

I used this method for error translation myself just a few days ago with no issue. So it should work just fine.
Reply
#8

(04-13-2015, 09:24 AM)silentium Wrote: You are missing the __construct() function in your MY_Form_validation class.

I used this method for error translation myself just a few days ago with no issue. So it should work just fine.

Thanks for your answer, but it's not the probleme.

The constructor is not necessary : https://php.net/manual/en/language.oop5.decon.php

I tried it, and no change :-(

Another idea ?
Reply
#9

(04-12-2015, 10:27 AM)silentium Wrote: When you say you have a custom validation rule. Have you created it as a callback function or have you extended the library with more rules?

If you do the latter, extending the form validation library, you can just add the error message to the form_validation_lang.php lang file.

Here is an example of extending the form validation library

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
MY_Form_validation extends CI_Form_validation
{
 
    function __construct($config = array())
 
    {
 
         parent::__construct($config);
 
    }

 
   /**
     * Alpha Dash Space
     *
     * Validates form input, allows alpha, numeric, dash, underscore and spaces
     *
     * @param  string
     *
     * @return   bool
     */
 
   function alpha_dash_space($str)
 
   {
 
       return (bool) preg_match("/^([-a-z0-9_ ])+$/i"$str);
 
   }


Then copy the form_validation_lang.php language file from system/languages/<lang>/ to the language folder in the application folder, application/languages/<lang>/

Then add a new line below the current lines. It would look like this for the rule above.

PHP Code:
$lang['form_validation_alpha_dash_space'] = 'my error message'

Hope that helps.

Thanks man - didn't think to extend the Form Validation class. I was just trying to set it all in the config file.

Your suggestion worked Smile
Reply
#10

Wonderful. Happy to help.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB