Welcome Guest, Not a member yet? Register   Sign In
Using the form_validation when your passing an ID in the uri
#1

[eluser]James Mills[/eluser]
Hi,

This might take a me a while to explain and I might not get it right first time but this is bugging me and I am sure there has to be a way to do what I am doing without a big work around.

So I am building a very very simple Document Management System. The process in the admin is very simple. you create a folder. You can view a list of all folders. You then open a folder and you can upload documents inside this folder.

When you click on a folder to open the folder you go to /folders/view/1. The controller folder and then the function view uses $folder_id = $this->uri->segment(3); to get the ID of the folder. I then get the folder info from the folder table in the database and I can also get the documents where FOLDERS_id = the ID we get above. Simples!

So where I am stuck is this... In the view where you see inside the folder and you see the folder info and you see the documents inside this folder I am also putting a form that people can use to upload new documents into the folder.

I want to put some validation on this form. My problem comes when there is an error... where do you send it back to?

This will not work because it has no idea what the original folder ID is.

Code:
if($this->form_validation->run() == FALSE)
        {
            // form has not been run or there are validation errors    
            // Load form
            $this->load->view('folder_view', $this->view_data);

        }


This will not work for the same reason...

Code:
if($this->form_validation->run() == FALSE)
        {
            // form has not been run or there are validation errors    
            // Load form
            $this->view();

        }

So how do I do it? Am I making sense?

Full code below and also here http://pastie.org/1882628

Screen shot of my view with form is here http://www.jamesmills.co.uk/screen_shots...9_2134.png

Code:
function view_all()
    {
        $folders = $this->Folders_model->get_all();
        
        
        
        $this->view_data['html_title'] = 'All Folders';
        $this->view_data['folders'] = $folders;
        
        $this->load->view('folders', $this->view_data);
    }
    
    
    public function view()
    {
        $folder_id = $this->uri->segment(3);
        
        $folder = $this->Folders_model->get_single($folder_id);
        $documents = $this->Documents_model->get_folder_docs($folder_id);
        
        
        $this->view_data['html_title'] = 'All Folders';
        $this->view_data['folder'] = $folder[0];
        $this->view_data['documents'] = $documents;
        
        $this->load->view('folder_view', $this->view_data);
    }
    
    
    
    public function upload()
    {

        
        $this->view_data['html_title'] = 'My title';
        
        // Validate the form input
        $this->form_validation->set_rules('DOCS_title', 'title', 'trim|required|xss_clean');
        $this->form_validation->set_rules('DOCS_summary', 'Summary', 'trim|required|xss_clean');
        
        
        if($this->form_validation->run() == FALSE)
        {
            // form has not been run or there are validation errors    
            // Load form
            $this->load->view('folder_view', $this->view_data);

        }
        else
        {        
            
            // show user confirmation
            $this->session->set_flashdata('success_message', 'Temp message');
            
            $this->load->view('folder_view', $this->view_data);
        }
        
    }
#2

[eluser]Brad K Morse[/eluser]
In the screenshot, I see the URL is /folders/view/1

In the form_open(), as of ci 2.0, if it is left blank, it will automatically post to itself, so it will post to /folders/view/1

So if that number '1' within that URL /folders/view/1 is the ID of the folder you want the file to upload to, then leaving form_open() blank or if you need to pass something in it, then:

Code:
<?=form_open(uri_string())?>

I doubt it is a little more complex than this, so if my response does not work, please explain why that is not the solution so I may be able to better understand it.
#3

[eluser]James Mills[/eluser]
Hiya,

So this actually make sense.

Just to make sure I am getting this right because I am still not sure this is 100% the best way.

What I think your suggesting is that in my folder controller I have a function called view. If the upload form has not been submitted or there is errors then you get the folder info, documents for that folder and form and show that......

I think my main issue is that I get a dirty feeling when I merge function view and function upload together. Maybe that is what I have to do?

It just does not make much sense to me. This way you can only have one form in any one page with validation...
#4

[eluser]James Mills[/eluser]
Hiya,

OK so I have it working now... using some of your advise above. This does mean that I can only have one from on the page but for this instance it does not matter. I will have to research and test a little more about this in the future.

Thanks
#5

[eluser]d1a8lo24[/eluser]
The best way to get the errors or validation errors like the last person said is to submit the form to the same controller see the following example;
Code:
// I also recommend to just use the passing parameters for your folder id instead of
// getting the uri.
function somefunction($vars = 0, $vars2 = 0)
{
     $data['something']     = $this->get_someinfo_to_display();

     $this->load->library('form_validation');
     // I'm using the validation config on the config folder    
     if($this->form_validation->run('config_settings'))
     {
       $this->model->run_some_function($vars, $vars2);
     } else {
    
       $this->load->view('someview', $data);
     }    
}

If you have another function to update the information then just load the same view and the errors will appear.

ALso any fields that are not validated will not throw an error and you also won't be able to use the set_value() function or i mean it will not return anything.
#6

[eluser]Brad K Morse[/eluser]
[quote author="James Mills" date="1304993691"]Hiya,

OK so I have it working now... using some of your advise above. This does mean that I can only have one from on the page but for this instance it does not matter. I will have to research and test a little more about this in the future.

Thanks[/quote]

Glad to hear you got things working. In theory, I would do it this way:

Code:
function upload($id) {
  $this->load->your_model
  $data['files'] = $this->your_model->contents_of_folder($id);

  /*
  all your form validation rules and loading form_validation library
  */

  if(form validates && $this->input->post('upload_form_submitted')) {
     $this->your_model->upload_document($id);
     if($this->upload_file($id, $this->input->post('file')) {
       $this->load->view('upload_success', $data); // success, display all files in that folder
     } else {
       print 'file did not upload';
     }
  } else {
    $this->load->view('contents_of_folder_view', $data);
  }
}

private function upload_file($id, $file) {
  // load upload library

  if(file uploads) {
    return TRUE;
  } else {
    return FALSE;
  }
}

There is probably a better way, but this should give you an idea of what I was thinking, also...I may of left some things out, thats me being lazy.
#7

[eluser]Nick_MyShuitings[/eluser]
You can also post all your forms to a master controller method called submit, or somesuch, which will then divy them out for handling to various other methods based on the form id that was posted, or the submit button used, etc.

This is my preferred method since it allows me to keep all form related magic, validation etc, in one centralized method.
#8

[eluser]James Mills[/eluser]
Hiya,

Thanks for all feedback.

I am sure there are loads of different ways of doing this. I am still not 100% sure I have got an answer in my head to my initial problem but I have found a way around for now and I am sure with more and more use of CI I will get a better solution in time.

Cheers,

James




Theme © iAndrew 2016 - Forum software by © MyBB