Search code examples
codeignitervalidationmessageshow

How to show validation errors in the single message Codeigniter?


I have a problem with codeigniter, when checking the fields it shows errors as follows:

The Username field is required. The Password field is required. 
The Another field is required

but I need it so like this:

Required fields a Username, Password, Another.

How to do it?


Solution

  • I wouldn't do that. But you have numerous number of choices. For example: don't use form validation library at all or develop your own. Try something like this:

    $errorArray = array();
    if (!$this->input->post('username')) {
        $errorArray[] = 'Username';
    }
    if (!$this->input->post('Password')) {
        $errorArray[] = 'Password';
    }
    if (count($errorArray)!==0) {
        $allErrorFields = implode(",", $errorArray);
        $errorMessage = 'Required fields: '.$allErrorFields;
        $data['errorMessage'] = $errorMessage;
    }
    
    $this->load->view('myview', $data);