Welcome Guest, Not a member yet? Register   Sign In
Form validation and redirecting to different controller
#1

[eluser]sdotsen[/eluser]
I have a search field under my "home" page, the controller is called "home"

All search results end up at http://domain.com/tags/<keyword>

In the event, the user doesn't properly adhere to the requirements (i.e. field not populated or min_character not met), I would like to redirect them back to the main page with the error message. I followed the blog tutorial on CI's website BUT realized it's not what I'm looking for as I'm using two different controllers. I got it to work by inserting the code "twice" See below. However, it doesn't seem like that's the appropriate way to do it.

Home controller:

Code:
function index() {

    $data['records'] = $this->home_model->getAll();

    $data['myprofile'] = $this->home_model->getMyProfile();

    $data['similar'] = $this->profile_model->getSimilarProfile("sam", true);

    $this->load->view('home_view', $data);
}

Tag controller (this works but doesn't display the error message (if any):

Code:
function search() {

    $this->load->library('validation');
        
    $rules['tag']    = "trim|required|min_length[4]|xss_clean";
        
    $this->validation->set_rules($rules);
        
    if ($this->validation->run() == FALSE)
    {
        redirect('home');
    }
    else
    {
        redirect('tags/view/'.$this->input->post('tag'));
    }
}


Tag controller (this works and displays any error messages but look at extra code):

Code:
function search() {

    $this->load->library('validation');
        
    $rules['tag']    = "trim|required|min_length[4]|xss_clean";
        
    $this->validation->set_rules($rules);
        
    if ($this->validation->run() == FALSE)
    {
        $data['records'] = $this->home_model->getAll();
        
        $data['myprofile'] = $this->home_model->getMyProfile();

        $data['similar'] = $this->profile_model->getSimilarProfile("sam", true);

        $this->load->view('home_view', $data);
    }
    else
    {
        redirect('tags/view/'.$this->input->post('tag'));
    }
}
#2

[eluser]theprodigy[/eluser]
Instead of sending the form to the tags controller, try sending the form to main, and redirect if everything was met correctly, rather then assuming correct and redirecting if incorrect. That way, if incorrect data was submitted, you are already on the page necessary to display the errors. If everything is good to go, redirect to the proper "tags" page
#3

[eluser]sdotsen[/eluser]
So, I apologize if my question(s) are newbish, but this is my first time using CI and definitely my first with OOP. So far I'm liking it but form validation is confusing me a bit. For example, let's look at the following example. If one wanted to update their account setting, they would go to www.domain.com/account/settings. I have a view that's being called from "settings_view.php." This page pulls information from the DB with the relevant info. So let's say I have a method called "settings_save" that takes the form value from the view (settings_view.php) and saves the info back into the DB. However, what if the user forgot to fill in a field or modified a certain field but the value doesn't meet the correct criteria.

With the redirect above, I was able to "redirect" the user back to the original page, BUT it pulls the "old" data from the DB. Would I need to create another "view" to display what the user had entered or didn't enter?
#4

[eluser]theprodigy[/eluser]
if you do it the way I described
Quote:1. form submits to main.
2. main validates form submission.
3a. if all is good, redirect to tags
3b. if invalid, repopulate form and display error
it will make it a lot easier. If you REALLY want to continue the way you currently have it, then you need to pass the user's form submissions to main in order for main to have access to it.




Theme © iAndrew 2016 - Forum software by © MyBB