2

Another CI Validation Error here. I've tried searching and from what I can see, the code I have is OK. The validation runs - if I just echo out a "Validation Failed" string from the controller, it displays.

But I cannot seem to get it to display in an actual view. Even if I have a single line in the view (ie echo validation_errors(); ), there are no errors output even though it fails validation.

Any pointers would be greatly appreciated :)

Controller

public function add() {
    if ($this->form_validation->run('user_add_edit') == FALSE)
    {
        //Validation failed
        $this->load->view('templates/header_generic');
        $this->load->view('templates/navigation');
        $this->load->view('user/add_user_form');
        $this->load->view('templates/footer_generic');
    }
    else
    {
        echo "Form validated!";
    }
}

View (Partial)

                        <div class="panel-body">

                        <?php echo validation_errors(); ?>

                        <?php echo form_open('user/add'); ?>
                            <label for="email">
                                Email Address
                            </label><br />
                            <div class="form-group input-group <?php echo null === form_error('email') || is_null(form_error('email')) ? 'form-group has-error' : ''; ?>">
                                <span class="input-group-addon">@</span>
                                <?php echo form_input($email_attr, set_value('email')); ?>
                            </div>
                            <br />
                            <?php echo form_error('email');?>
                            <br />
                            <?php echo form_fieldset("Password"); ?>
                            jfkdjflkdjflks
                            <?php echo form_fieldset_close(); ?>
                            <br />
                            <?php echo form_submit("submit", "Add New User", "class='btn btn-success'"); ?>
                        </form>
                    </div>

Form Validation

$config = array(
'user_add_edit' => array(
    array(
        'field' => 'email',
        'label' => 'Email Address',
        'rules' => 'trim|required|valid_email|is_unique[user.email]',
        'errors'    => array(
            'required'  => 'You must enter a %s',
            'valid_email'   => '%s is not a valid email address',
            'is_unique'     => 'This email address already exists'
        )
    ),
1
  • Oh, I have come across this as a possible error, so I can confirm base_url is set in the config. Commented May 6, 2018 at 9:22

1 Answer 1

2

Having MY_Form_validation.php improperly set up can mess up with setting of form rules via config file.

Fix

In application/libraries/MY_Form_validation.php - replace your constructor with the below code or just follow the changes below by adding the $config parameter.

function __construct($config = array()){
    parent::__construct($config);
    $this->CI =& get_instance();
}

It's also a possibility that the value of $config variable is being overwritten that's happening inside application/config/form_validation.php. Check for it as well.

Alternative:

Load the form_validation.php config file from the controller method and pass the relevant config item to set_rules(..) like in the following.

public function add() {
    $this->load->config('form_validation');
    $this->form_validation->set_rules($this->config->item('user_add_edit'));

    if ($this->form_validation->run() == FALSE)
    {
        //Validation failed
        $this->load->view('templates/header_generic');
        $this->load->view('templates/navigation');
        $this->load->view('user/add_user_form');
        $this->load->view('templates/footer_generic');
    }
    else
    {
        echo "Form validated!";
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the suggestion - I'm not extending the CI Form Validation library though. I did create the MY_Form_validation library extension and try it. If I just called the usual $this->load->library(form_validation) library, nothing changed. If i called $this->load->library(MY_Form_validation, it told me the class CI Form Validation wasn't present in my library. I don't really want to re-write an entire validation library.
That's fine sir. I added an alternative anyways.
The alternative got it working. Weird, but I'm in business. Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.