Welcome Guest, Not a member yet? Register   Sign In
single error message for all the validations?
#1

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!';
    ] 
Reply
#2

(This post was last modified: 01-07-2016, 05:34 AM by Diederik.)

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>';
 
   }


Reply




Theme © iAndrew 2016 - Forum software by © MyBB