1

I am loading my form on index action of my Add controller and posting that form to submit_post action of the same controller. All I want is that if the post data doesn't fulfill the validation then it should redirect to the index action back rendering my form only and keeping the form fields filled with the values that were posted and also I would be able to get the each field error individually here are my controller actions

class Add extends CI_Controller {
    private $view = "";

    public function __construct()
    {
        parent::__construct();
        $this->view = "add";
    }

    public function index()
    {
      $this->load->view("frontend/default", array("view" => $this->view));
    }

    public function submit_post()
    {
         $this->form_validation->set_rules('description', 'Description', 'required');
         $this->form_validation->set_rules('name', 'Name', 'required');
         if ($this->form_validation->run() == FALSE)
         {
              redirect('/add');
         }
         else
         {
             echo "Validated";
         }
    }
}

I have read many blogs and stackoverflow questions and found that this could be achieved via set_flashdata. Well, I did that and were able to print the all errors altogether.

2 Answers 2

1

The problem is because of the line redirect('/add'); With a redirect the validation class will be reloaded and all the information gathered during form_validation->run() is lost.

Here's my suggested code

     if ($this->form_validation->run() == FALSE)
     {
          $this->index();
     }
     else
     {
         echo "Validated";
     }
Sign up to request clarification or add additional context in comments.

1 Comment

This approach works as far as validation is concerned but what actually happens is that when you hit the submit button the url in the url bar gets changed to ci.local/add/submit_post and never changed to ci.local/add/ but validation works fine. I want to return back to ci.local/add/ along with the validation errors.
0

You should lоаd form_validation in your config file $autoload['libraries'] = array('form_validation'). And then if you want to display the errors individualy, you should put this in your html right under every input field :

    <?php echo form_error('your fieldname');?>

14 Comments

And if you want to keep the fields populated : value="<?php echo set_value('name'); ?>" . You do that for every field
I have already loaded the form_validation. Moreover form_error is working if I post the form on the same action which I am using for rendering it. In my case index is that action but I don't want to post the form on the same action which is being used to render the form.
Why do you want to redirect if the form validation is false? It doesn't send the form until is true so it keep looping over the form_validation until it is true .
Just take this line out redirect('/add'); and live it blank. it should work just fine
I think I am not making sense to you. ci.local/add/index this is the url that shows my form and that form gets redirected to ci.local/add/submit_post when I submit the form. Now I want to go back to ci.local/add/index location so that my gets rendered again with the error messages for each field. So for this purpose I am using redirect("/add").
|

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.