Welcome Guest, Not a member yet? Register   Sign In
CI's Form Validation with Uploader class, how?
#1

[eluser]JamieBarton[/eluser]
Hey guys,

I have been looking at http://ellislab.com/forums/viewthread/133581/

I think that's sorta of my issue.

What I have currently in my code before searching, is a form that posts to a controller, all thats inside the controller is something that requests a model. The model does the validation. My model:

Code:
function process_photo()
    {
        $this->form_validation->set_rules('name', 'Photo Name', 'trim|required|min_length[3]|max_length[100]|xss_clean');
        $this->form_validation->set_rules('description', 'Description', 'trim|xss_clean');
        
        if($this->form_validation->run() == FALSE) {
            
            $this->session->set_flashdata('message', validation_errors('<li>','</li>'));
            $this->session->set_flashdata('name', $this->input->post('name'));
            $this->session->set_flashdata('location', $this->input->post('location'));
            $this->session->set_flashdata('description', $this->input->post('description'));
            redirect('upload/'.$this->input->post('album'));
            
        }
        else {
            $data = array(
                'photo_name'            => $this->input->post('name'),
                'photo_description'        => $this->input->post('description'),
                'photo_album_id'        => $this->input->post('album'),
                'photo_member_id'        => 1,
                'photo_family_id'        => 1
            );
            
            $this->db->insert('photo', $data);
            $this->session->set_flashdata('message', '<li>Photo upload</li>');
            redirect('photo/add');
        }
    }

I don't know how to then check the upload validation and inside of this and send back the errors as flashdata too.

Can anyone help me?

I always thought form validation should go into Models, but perhaps I have too much inside my model's function here? Maybe someone can help me clean it up better too?

Thanks for your help.


Regards,

Jame
#2

[eluser]JamieBarton[/eluser]
I've slightly changed my model now to return a TRUE or FALSE. But validation still remains in the Model, as I've been taught in the past to do.

See the model:
Code:
function process_photo()
    {
        $this->form_validation->set_rules('name', 'Photo Name', 'trim|required|min_length[3]|max_length[100]|xss_clean');
        $this->form_validation->set_rules('description', 'Description', 'trim|xss_clean');
        //$this->form_validation->set_rules('album', 'Photo Album', 'trim|xss_clean');
        //$this->form_validation->set_message('album', 'Photo album must be selected');
        
        if($this->form_validation->run() == FALSE) {
            
            $this->session->set_flashdata('message', validation_errors('<li>','</li>'));
            $this->session->set_flashdata('name', $this->input->post('name'));
            $this->session->set_flashdata('location', $this->input->post('location'));
            $this->session->set_flashdata('description', $this->input->post('description'));
        //    redirect('upload/'.$this->input->post('album'));
        return false;
            
        }
        else {
            $data = array(
                'photo_name'            => $this->input->post('name'),
                'photo_description'        => $this->input->post('description'),
                'photo_album_id'        => $this->input->post('album'),
                'photo_member_id'        => 1,
                'photo_family_id'        => 1
            );
            
            $this->db->insert('photo', $data);
            $this->session->set_flashdata('message', '<li>Photo upload</li>');
            $this->sessioon->set_flashdata('message', array('upload_data' => $this->upload->data()));
        //    redirect('photo/add');
        return true;
        }
    }

My controller now looks like this:

Code:
function process()
    {    
        if (!$this->photo->process_photo())
        {
            redirect('upload/'.$this->input->post('album'));
        }
                
    }

Now I don't know what I should do, should I have an else statement for the controller or not ? Since the ELSE insert into DB is handled with the Model?

Where can I drop in the upload class and validation for that? How would I go about making the errors flashdata too.


Regards,

Jamie
#3

[eluser]Muser[/eluser]
Hi,

There is an user community contribution in this forum with an extended MY_Form_Validation class file that performs upload validation. At this moment I can't remember where it is, please search it in this forum or at the wiki.

And the question about flashdata errors I suggest to you another implementation. Only use flashdata to send success messages, because to display them you need to do a request to another page (for example, a redirect). For display error messages use validation_errors() and form_error(), and for repopulate data use set_value() form helper's function.




Theme © iAndrew 2016 - Forum software by © MyBB