Welcome Guest, Not a member yet? Register   Sign In
Use anything as a rule and $this->some_model issue
#4

(This post was last modified: 02-02-2018, 05:07 AM by Zeff.)

Hi guys,

Many thanks for your attentiveness and help, but this thread really helped me out: https://stackoverflow.com/questions/4263...bership-nu I hope this thread can save time to others who struggle with this new option in the CI form_validation library.

So, now my (complete) model code looks like this (and works!):

PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
    
class 
User_model extends CI_Model
{    
    protected 
$table_name 'test_table';
    
    protected 
$primary_key 'id';
    
    protected 
$protected_attributes = array('submit');
    
    public function 
__construct()
    {
        
//parent::__construct();
        
$this->load->database();
    }
    
    public function 
insert($data)
    {
        
$data $this->protect_attributes($data);

        
$data $this->validate($data);
        
        if(
$data)
        {
            
$this->db->insert($this->table_name$data);
            
$id $this->db->insert_id();
        
            return 
$id// false or id value
        
}
        return 
false;
    }
    
    public function 
update($id$data)
    {
        
$data $this->protect_attributes($data);

        
$data $this->validate($data);
        
        
$this->db->where($this->primary_key$id);
        
$this->db->set($data);
        
        
$result $this->update($this->table_name);
        
        return 
$result// bool
    
}
    
 
   public function validate($data$type='update')
 
   {
 
       foreach($data as $key => $val)
 
       {
     
       $_POST[$key] = $val// To let form validation library use them
 
       }
 
       $this->load->library('form_validation');
 
       
        $this
->form_validation->set_rules('cn''Common name''required');
 
       $this->form_validation->set_rules('birthdate''Birth Date', [
 
                                                                   'required'
                                                                    [
                                                                        
'whatever_you_name_it', [ $this->usermodel'checkdateformat' ]
                                                                    ]
 
                                                               ]);
 
       $this->form_validation->set_rules('remark''Remark''required');
 
       
        
        if
($this->form_validation->run() === true)
 
       {
     
       return $data;
     
       
        
}
 
       else
        
{
     
       return false;
 
       }
 
       
    
}
 
   
    public 
function protect_attributes($row)
 
   {
     
   foreach($this->protected_attributes as $attr)
     
   {
         
   if(is_object($row))
         
   {
             
   unset($row->$attr);
         
   }
         
   else
            
{
             
   unset($row[$attr]);
         
   }
     
   }
     
   
        return $row
;
 
   }
 
   
    public 
function checkdateformat($date)
 
   {
     
   $this->form_validation->set_message('whatever_you_name_it''Not a valid date');

 
       if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)) {
 
           return true;
 
       } else {
 
           return false;
 
       }
 
   }

Reply


Messages In This Thread
RE: Use anything as a rule and $this-&gt;some_model issue - by Zeff - 02-01-2018, 03:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB