CodeIgniter Forums
single error message for all the validations? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Best Practices (https://forum.codeigniter.com/forum-12.html)
+--- Thread: single error message for all the validations? (/thread-64052.html)



single error message for all the validations? - rakibtg - 01-07-2016

Hi, take a look at the code:
PHP Code:
[
        
'field'  => 'action',
        
'label'  => 'in',
        
'rules'  => 'required|alpha|max_length[5]',
        
'errors' => [
            
'required'   => 'Something missing!',
            
'alpha'      => 'Something missing!',
            
'max_length' => 'Something missing!'
        
]
    ] 

as you can already guess that i am trying to hide the default error messages and override each of them with a custom string, is there any way to do it once? Like this:
PHP Code:
[
        
'field'  => 'action',
        
'label'  => 'in',
        
'rules'  => 'required|alpha|max_length[5]',
        
'default_error' => 'Something missing!';
    ] 



RE: single error message for all the validations? - Diederik - 01-07-2016

You could use the error_array() function for this purpose.

PHP Code:
$error_array $this->form_validation->error_array();
if (
is_array($error_array ) && !empty($error_array)) {

 
   // Generate a single message for all fields combined:
 
   echo '<div class="error">Something is wrong with the form values.</div>';

 
   // Or loop through the errors and output a message per fieldname
 
   foreach($error_array  as $fieldname => $message) {
        
// The $message variable contains the message you want to hide
 
       echo '<div class="error">There is a problem with the value for the ' $fieldname ' field.</div>';
 
   }