0
public function blog_create()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules("email", 'Email', "trim|required|valid_email|max_length[100]");
    $this->form_validation->set_rules("password", "Password", "trim|required|min_length[8]");
    $blog_name = $this->input->post('name');
    $blog_des = $this->input->post('description');
    $user_id = $this->session->userdata('user_id');

    if ($this->form_validation->run() == TRUE){
        $pass = $this->Blog_Model->blog_insert($blog_name,$blog_des,$user_id);
        if ($pass === TRUE) {
             $this->session->set_flashdata('blogin','Blog Was Inserted');
             return redirect('dashboard?success="success"');

        }

    }
    redirect('dashboard?rr="error"');
}

Form validation is not working i don't know what wrong i've put validation_error in my index file but it still does'nt show any error

3 Answers 3

1

So, I created a demo with your code and it worked for me. The problem here as I see should be that you're not printing the success or error message in the view. I've written the code below and done the explaining in the comments wherever necessary. See if it helps you.

View(Blog Form)

<?php $this->load->view('your-path/vwError'); // load a view to show errors ?> 

<form method="POST" action="<?php echo base_url('form'); ?>">
    <input type="text" name="email">
    <input type="password" name="password">
    <input type="submit" value="Submit" > 
</form>

View(Error)

if($this->session->flashdata('message')){
    $message = $this->session->flashdata('message');
}

if($this->session->flashdata('error')){
    $error = $this->session->flashdata('error');
}

<?php if (!empty($message)): ?>
<div class="alert alert-success"> <!-- your-style-here, I've used bootstrap here(Green background) -->
    <a class="close" data-dismiss="alert">×</a>
    <?php echo $message; ?>
</div>
<?php endif; ?>

<?php if (!empty($error)): ?>
<div class="alert alert-danger"> <!-- your-style-here, I've used bootstrap here(Red background) -->
    <a class="close" data-dismiss="alert">×</a>
    <?php echo $error; ?>
</div>
<?php endif; ?>

Controller

function form(){

    $this->form_validation->set_rules("email", 'Email', "trim|required|valid_email|max_length[100]");
    $this->form_validation->set_rules("password", "Password", "trim|required|min_length[8]");

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

        // get the value if the validation is successful otherwise it's just extra code
        $blog_name = $this->input->post('name');
        $blog_des = $this->input->post('description');
        $user_id = $this->session->userdata('user_id');

        $pass = $this->Blog_Model->blog_insert($blog_name,$blog_des,$user_id); // your-logic

        if ($pass === TRUE) {

            $this->session->set_flashdata('message', 'Blog Was Inserted');

            redirect('your-url-here');

        } // you can also set a flashdata here in {else} condition if the data isn't saved ie returns FALSE
    }else{  // validation failed

        $this->session->set_flashdata('error', validation_errors()); // store the validation errors in flashdata

        $this->load->view('your-blog-form-view');   // You probably don't want to redirect as you'd want to retain the previous input value from the user.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

first debug your code and tell me if validation is running properly or not

$user_id = $this->session->userdata('user_id');

if ($this->form_validation->run() == FALSE) {
  echo validation_errors();     
}

Comments

1

Rather than checking:

If($this->form_validation->run() == true)
{
   // Do what needs to be done when validation passes
}

Try checking the opposite:

if($this->form_validation->run() == false)
{
  // Do something if validation fails
}

else
{
   // Do what needs to be done when validation passes
 }

In most loosely-typed language, something that is not false not necessarily is true. The form validation is guaranteed to respond false if validation fails but you must not assume, based on that fact alone, that a successful validation will respond true

Comments

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.