Welcome Guest, Not a member yet? Register   Sign In
Form Validation and CI/MVC Developing Style
#1

[eluser]Unknown[/eluser]
Hey ya'll!

I want to use form_validation on a type:

Code:
+---------------------+
| Controller: Service |
+---------------------+
| fnc: page()         |
| fnc: addService()   |
| ...                 |
| etc                 |
+---------------------+

page() has views with forms
one of the forms has action to addService() (service/addService)
addService() redirect when finish with insert in db to page()

so! i have problem with validation_errors() cause the redirection.
is there a way like keeping alive the validation_errors() to next page??

1st I try flashdata but I dont like it very much.
2nd I try calling $this->page(); inside the addService() on the if ($this->form_validation->run() == FALSE) part. but i think its the worst thing and out of MVC philosophy to call a page() function inside addService() function. or it isnt??

I want to hear other opinions about this and also opinions on how to develop in CI and MVC pattern.

My style of dev was:
- A controller on each section (Services,Departments,Users etc)
|-- functions with showing Views (like pages,edit,delete_confirmation etc)
|-- functions doing things via my database_model (delete,insert,moveUp or Down Items etc)
- One model with database reaction ONLY (insert,update,delete,get_list etc)
- Views

1) Its better for forms to action on the same functions where it stared?
2) Its bad developing method to have separate functions on Controller to do some work and after that redirecting to first function?
3) What is exactly the work of Model? I must have a Model for each Controller? Model must have an operation to connect Controller with Database?

I'm bit confused about how to develop a site in CI/MVC style. but after reading and viewing others i conclude to this (maybe its wrong). i'd like to know how you develop your CI projects.


PS: I would be very glad if you have any links or any books to suggest.
#2

[eluser]Phil Sturgeon[/eluser]
You might be better off showing some examples of what you are trying to do as your explanations have left me slightly confused.

I'll answer what I can:

1.) It depends on what you need but for the most part, yes forms posting back to themselves makes a lot of sense.

3.) Model is for fetching data and light manipulation of that data. You don't dont need one model per controller, you can have as many as you like, they don't rely on each other at all.

I sugest you start with the user guide and videos and take it from there.
#3

[eluser]Unknown[/eluser]
Code:
<?php

class Service extends Controller {
    
    function __construct()
    {
        parent::Controller();
        $this->load->helper('url');
        $this->load->model('model_database');
        $this->load->library('session');    
    }
    
    function index()
    {
        redirect('service/page/1');
    }
    
    function page()
    {
        $this->load->helper('cookie');
        $this->session->set_flashdata('http_referer',$this->uri->uri_string());

        //here is a lot of stuff for $data2
        //reading cookies, define some var like show,start,page,maxPages,rows etc

        $data2['db'] = $this->model_database->get_list('service',$show,$start,'sort','asc');
                
        $data['title'] = $this->title .' - Services';
        $this->load->view('header', $data);
        $this->load->view('service/service',$data2);
        $this->load->view('footer');
    }
    
    function add()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Service Name', 'required');
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->page(); //Temporary bad way
        }
        else
        {
            echo "TRUE";
        }
        
        $max = $this->model_database->max('service','sort') + 1;
        
        $new_data = array(
            'name' => $_POST['name'],
            'sort' => $max
        );
        
        //insert( TABLE, DATA_SET )
        $this->model_database->insert('service', $new_data);
        
        $data['title'] = $this->title .' - Add Services';
        
        //This was before $this->page();
        /*
        if($this->session->flashdata('http_referer')) {
            redirect($this->session->flashdata('http_referer'));
        } else {
            redirect('service');
        }*/
    }
    
    function edit()
    {    
        $this->session->keep_flashdata('http_referer');
        $id = $this->uri->segment(3);

        $db['row'] = $this->model_database->get_item('service',$id);
        $this->load->view('header', $data);
        $this->load->view('service/edit',$db);
        $this->load->view('footer');
    }
    
    function editSave()
    {
        $new_data = array('name' => $_POST['name']);

        $this->model_database->update('service', $new_data, 'id = '.$_POST['id']);
        
        redirect($this->session->flashdata('http_referer'));
    }
    
    function delete()
    {
        //like edit but for delete
    }
    
    function deleteNow()
    {
        //like editSave but for delete from db
    }
    
    function moveUp()
    {
        //moving up an item (sorting)
        redirect($this->session->flashdata('http_referer')
    }
    
    function moveDown()
    {
        //moving down an item (sorting)
        redirect($this->session->flashdata('http_referer')
    }
    
    function resetSortInPage()
    {
        //like the fnc name
    }
}

Hey Phil! thanks for the reply. As you can see my problem is that I have on page() views not only a db list with items but a Form for adding items. I thought it would be very complicated to also handle Inserting data in page() so i made add(). In the same logic is the edit(), editSave() and delete(), deleteNow(). Is this a bad way developing apps? Thats also what i meant in no.2 question with redirecting. (see: editSave redirects to edit)

I had read most of basics and intro from User Guide and now i use it more like a reference.
Also I had watched both video tuts and thats why I'm confused. There uses same logic comments() and comments_insert(). Think trying to put form_validation there..
in comments() where the comments_view <FORM> action is pointing on comments_insert() who redirects you back to comments()
the <?php echo validation_errors(); ?> no longer exist after redirect..

thats why i ask maybe silly questions about the logic of developing. i know there isn't clear about the right or the wrong way developing but i want to know how other people develop. also i want code to be as much clearer as it can, for feature edits or readings.

I attach a light screenshot from 'service/service' view in page()
#4

[eluser]Phil Sturgeon[/eluser]
Not got a full solution just yet - im at work and its a big read - but here is a quicky:

Code:
function add()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Service Name', 'required');
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->page();
            return;
        }
        else
        {
            echo "TRUE";
        }

There is nothing wrong with calling other methods and its much better than a redirect in most situations.

The one change I made here is to add return after it. This stops the function from running and lets CodeIgniter do its thing after the controller. It is much like running exit() in a normal PHP application but does not break the CodeIgniter application flow.




Theme © iAndrew 2016 - Forum software by © MyBB