• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use anything as a rule and $this->some_model issue

#1
I'm trying to perform form validation from within a specific method ('my_user_model', in fact I'm extending on Lonnie Ezell's MY_Model using 'in model' validation), but I'm getting issues with custom validation rules. Callbacks didn't work because the callback could not be found in 'my_user_model'...
So I've gone through the new user guide, where I read about 'use-anything-as-a-rule'
in the form_validation user guide.
But when I try to validate a field in my controller in this way:

PHP Code:
$this->form_validation->set_rules('birthdate''Birthdate'
array(
'required', array('birthdate_callable', array($this->my_user_model'checkdateformat'))); 

I'm getting a parse error on '$this' : "syntax error, unexpected '$this' (T_VARIABLE), expecting ')'"

Does somebody has experience with this new extension ('Use anything as a rule')?

Thanks!
Reply

#2
You forget last ')'
Reply

#3
Any attempt at formatting/indenting your code would have shown you the problem.

PHP Code:
$this->form_validation->set_rules'birthdate''Birthdate', [
    
'required'
    [
        
'birthdate_callable'
        [
            
$this->my_user_model
            
'checkdateformat'
        
]
    ]
]); 
Reply

#4
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

#5
UPDATE:

I tested again an it seems to be working perfectly if you call a validation array too. Angry 
The only difference with my live example (which throws 'T_VARIABLE' parse error on $this) is that I assign my validation array (cfr. MY_Model - L. Ezell) as the property $validation_rules at the beginning of my extending child class.

PHP Code:
// eg.
Class User_model extends MY_Model
{
 
   protected $table_name 'test_table';
 
   protected $primary_key 'id';
 
   protected $skip_validation false;
 
   protected $protected_attributes = array('submit');

 
   protected $validation_rules   
    array
(
 
       array(
 
           'field' => 'cn',
 
           'label' => 'Common name',
 
           'rules' => 'required'
 
       ),
 
       array(
 
           'field' => 'birthdate',
 
           'label' => 'Birth Date',
 
           'rules' => [
 
                          'required',
 
                          [
 
                          'birthdate_callable', [ $this'checkdateformat' ]
 
                          ]
 
                      ]
 
       ),
 
       array(
 
           'field' => 'remark',
 
           'label' => 'Remark',
 
           'rules' => 'required'
 
       )
 
   );
 
   }


 
   public function __construct()
 
   {
 
       //parent::__construct();
 
       $this->load->database();

 
   }

...



Now I found this thread: 'Can't concatenate on an array' and the proposed solution was that you have to assign such arrays (containing concanations and even methods starting with $this->) in a method...

So I changed my code (assigned the array in the constructor) to:
PHP Code:
Class User_model extends MY_Model
{
 
  protected $table_name 'test_table';
 
  protected $primary_key 'id';
 
  protected $skip_validation false;
 
  protected $protected_attributes = array('submit');

 
  protected $validation_rules = array();

 
  
   
public function __construct()
 
  {
 
      $this->load->database();
 
      $this->validation_rules 
 
          array(
 
              array(
 
                 'field' => 'cn',
 
                  'label' => 'Common name',
 
                  'rules' => 'required'
 
              ),
 
              array(
 
                  'field' => 'birthdate',
 
                  'label' => 'Birth Date',
 
                  'rules' => [
 
                                 'required',
 
                                 ['birthdate_callable'
 
                                     $this'checkdateformat' ]
 
                                 ]
 
                             ]
 
             ),
 
             array(
 
                  'field' => 'remark',
 
                  'label' => 'Remark',
 
                  'rules' => 'required'
 
            )

 
         );
 
  }

...



 And YES: the problem was solved ...

Does anyone has an explanation for this?

Best regards,

Zeff
Reply

#6
$this only get's populated/generated after a class have been initiated. And therefor you get an error message when PHP tries to parse your class. It's looking for $this before it exists.
Reply

#7
Of course... I was dull searching for an explanation, I missed that one. Many thanks jreklund!
Reply

#8
How do you proceed if you have to send two parameters to your callable?
I am trying since hours and I don't get any solution, since I need to compare two dates (start <= end).

In your example, how would you proceed to call checkdateformat(date1,date2)?

Thank you
Reply

#9
Send the two parameters in an array.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.