1
votes

I'm using Codeigniter framework to create my web application.

I have a login form for the users to input their username and password as well as a remember me checkbox. After the user submits the form it uses jQuery ajax to submit the form and send the post data to the login controller submit function for data checking and manipulation. After the submit function is ran a JSON array is returned if the user was logged in. If the user is successfully logged in he/she is sent to the dashboard and if they weren't logged in successfully then an error message is displayed and under each of the user inputs displays the corresponding message to the rule that didn't pass the CI form validation rules.

As of right now it displays a message saying it didn't pass validation but I'm having an issue with figuring out how to display the validation rule that didn't pass under the user input box.

<div style="display:none"  id="message_container"><button type="button" class="close" data-dismiss="alert">&times;</button></div>
    <div class="login_container">
<?php $attributes = array('id' => 'login_form', 'class' => 'form-horizontal'); ?>
<?php echo form_open('login/submit', $attributes); ?>
    <div class="form-row row-fluid">
        <div class="spa12">
            <div class="row-fluid">
                <?php $attributes = array('class' => 'form_label span12'); ?>
                <?php echo form_label('Username<span class="icon16 icomoon-icon-user-3 right gray marginR10"></span>', 'username', $attributes); ?>
                <?php $attributes = array('name' => 'username', 'id' => 'username', 'value' => '', 'class' => 'span12 text valid'); ?>
                <?php echo form_input($attributes); ?>
            </div>
        </div>
    </div>
    <div class="form-row row-fluid">
        <div class="span12">
            <div class="row-fluid">
                <?php $attributes = array('class' => 'form_label span12'); ?>
                <?php echo form_label('Password<span class="icon16 icomoon-icon-locked right gray marginR10"></span><span class="forgot"><a href="#">Forgot your password?</a></span>', 'password', $attributes); ?>
                <?php $attributes = array('name' => 'password', 'id' => 'password', 'value' => '', 'class' => 'span12 password'); ?>
                <?php echo form_password($attributes); ?>
            </div>
        </div>
    </div>
    <div class="form-row row-fluid">                       
        <div class="span12">
            <div class="row-fluid">
                <div class="form-actions">
                    <div class="span12 controls">
                        <input type="checkbox" id="keepLoged" value="Value" class="styled" name="logged" />Keep me logged in
                        <button type="submit" class="btn btn-info right" id="loginBtn">
                            <span class="icon16 icomoon-icon-enter white"></span>
                            Login
                        </button>
                    </div>
                </div>
            </div>
        </div> 
    </div>
</form>
</div>


public function submit()
{
    $output_status = 'Notice';
    $output_title = 'Not Processed';
    $output_message = 'The request was unprocessed!';

    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_check_username');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
    $this->form_validation->set_rules('remember', 'Remember Me', 'trim|xss_clean|integer');

    if ($this->form_validation->run() == TRUE)
    {

    }
    else
    {
        $output_status = 'Error';
        $output_title = 'Form Not Validated';
        $output_message = 'The form did not validate successfully!';
    }

    echo json_encode(array('output_status' => $output_status, 'output_title' => $output_title, 'output_message' => $output_message));
}

public function check_username($str)
{
    if (preg_match('#[0-9]#', $str) && preg_match('#[a-z]#', $str)) 
    {
        return TRUE;
    }
    $this->form_validation->set_message('check_username', 'This is not have an accepted value!');
    return FALSE;
}

EDIT:

"error_messages":{"username":"This is not have an accepted value!"}}

Since I'm submitting this form via jQuery ajax function I'm trying to figure out how I'm going to make my if statement. Take into consideration I'll have multiple inputs that will have to be placed under each text field unless there's a better idea.

1

1 Answers

1
votes

If all you are trying to do is append the string of error messages to your $output_message you might be able to do this:

$output_message = 'The form did not validate successfully! Errors: ' . $this->form_validation->error_string();

You can set custom error messages using:

$this->form_validation->set_message('username', 'Invalid Username');

Edit as per your comment, if you want to send back the error messages you should try something like this:

echo json_encode(array('output_status' => $output_status, 'output_title' => $output_title, 'output_message' => $output_message, 'error_messages' => $this->form_validation->error_array()));

And to display those error messages I would add placeholders in your html and append the message there

CodeIgniters Form Validation Library Source:

/**
 * Get Array of Error Messages
 *
 * Returns the error messages as an array
 *
 * @return  array
 */
public function error_array()
{
    return $this->_error_array;
}

So it seems this is a new addition coming in Version 3.0, so for now you could just extend the Form Validation Library by adding a file inside your application/libraries/ and name it MY_Form_validation.php and inside it place this:

class MY_Form_validation extends CI_Form_validation {
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Get Array of Error Messages
     *
     * Returns the error messages as an array
     *
     * @return  array
     */
    public function error_array()
    {
        return $this->_error_array;
    }
}