Welcome Guest, Not a member yet? Register   Sign In
Set Rules in model.
#1

This is the code I have made so far.
It works but the whole if/check slug thing is ugly and I don't think it meets decent coding practices.
I don't want any check functions inside the controller. I want all check functions inside my model.
Is this the only way to make it possible?

As I've wrote this I've thought about adding a function called "function check slug()" and then call it as $this->pages_model->check_slug();

Anyways, I'd appreciate your feedback nether the less.

PHP Code:
<?php    

class Pages_model extends MY_Model {
    
    protected 
$_table_name 'pages';
    protected 
$_primary_key 'page_id';
    protected 
$_primary_filter 'intval';
    protected 
$_order_by '';
    protected 
$_rules = array();
    protected 
$_timestamps FALSE;

    public function 
set_rules($method$slug_check false)
    {
        if(
$method === 'edit')
        {
            
$id $this->input->post('page_id');
            
$slug $this->input->post('slug');
            
            
$page $this->pages_model->get_by(array('page_id' => $id), TRUE);
            
$check get_object_vars($page)['slug'];
            
            if(
$check !== $slug)
            {
                
$slug_check '|is_unique[pages.slug]';
            }            
        }

        
$config = array(
            
'add' => array(
                array(
                    
'field' => 'title'
                    
'label' => 'Title'
                    
'rules' => 'trim|min_length[5]|required|xss_clean'
                
),
                array(
                    
'field' => 'slug'
                    
'label' => 'Slug'
                    
'rules' => 'trim|min_length[5]|alpha_dash|required|xss_clean|is_unique[pages.slug]',
                ),
                array(
                    
'field' => 'order'
                    
'label' => 'Order'
                    
'rules' => 'trim|numeric|required|xss_clean'
                
),
                array(
                    
'field' => 'body'
                    
'label' => 'Body'
                    
'rules' => 'trim|min_length[5]|required|xss_clean'
                
)
            ),
            
'edit' => array(
                array(
                    
'field' => 'title'
                    
'label' => 'Title'
                    
'rules' => 'trim|min_length[5]|required|xss_clean'
                
),
                array(
                    
'field' => 'slug'
                    
'label' => 'Slug'
                    
'rules' => 'trim|min_length[5]|alpha_dash|required|xss_clean'.$slug_check.'',
                ),
                array(
                    
'field' => 'order'
                    
'label' => 'Order'
                    
'rules' => 'trim|numeric|required|xss_clean'
                
),
                array(
                    
'field' => 'body'
                    
'label' => 'Body'
                    
'rules' => 'trim|min_length[5]|required|xss_clean'
                
)
            ),
        );
        
$this->form_validation->set_rules($config[$method]);
    }

Reply


Messages In This Thread
Set Rules in model. - by miiikkeyyyy - 03-03-2015, 09:17 AM
RE: Set Rules in model. - by mwhitney - 03-03-2015, 01:48 PM
RE: Set Rules in model. - by spjonez - 03-03-2015, 05:06 PM
RE: Set Rules in model. - by tapan.thapa - 03-03-2015, 09:18 PM
RE: Set Rules in model. - by spjonez - 03-04-2015, 06:45 AM
RE: Set Rules in model. - by mwhitney - 03-04-2015, 01:16 PM
RE: Set Rules in model. - by cartalot - 03-06-2015, 05:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB