Welcome Guest, Not a member yet? Register   Sign In
Anonymous function Validation
#1

I am trying to use a function from another class as a validation function. The validation is able to call the function just fine and run it, but it will not return the error message for me to display on the screen. I am not sure what I am missing. I am running it in php 7.


PHP Code:
$this->load->library'form_validation' );
$this->load->helper'form' );

$this->form_validation->set_rules'email''Email Address', [ 'trim''required''valid_email', [ 'permission_exists'$this->Usermodel->permission_exists$this->input->post'email' ), $user['id'] ) ] ] ); 

Then in the other class the following function:


PHP Code:
function permission_exists$email$id )
{
 
  $this->load->library'form_validation' );
 
  
   $query 
$this->db->get(); //took out the actual query but the query does work and returns

 
  if ( $query->num_rows() == )
 
  {
 
     return TRUE;
 
  }
 
  else
 
  {
 
     $this->form_validation->set_message'permission_exists''This user already has access.' );

 
     return FALSE;
 
  }


I am following the documentation here, which is apparently not working : https://www.codeigniter.com/user_guide/l...-as-a-rule

The message I get back is: Unable to access an error message corresponding to your field name Email Address.(Anonymous function)

Currently I am going through the framework to try to figure out why I am getting this but if someone could explain why it doesn't work like the documentation says, that would be awesome!
Reply
#2

(This post was last modified: 01-30-2017, 11:47 AM by PaulD. Edit Reason: Added final note )

Hi,

I would put the model call into a callback, something like this:

PHP Code:
$this->form_validation->set_rules'email''Email Address''trim|required|valid_email|callback_permission_check'); 

And your callback function in your controller would look something like:
PHP Code:
private function permission_check() {
 
    // load your model
 
    $this->load->model('usermodel');
 
    
     
// set error message
 
    $this->form_validation->set_message'permission_check''This user already has access.' );
 
    
     
// get your variables
 
    $email $this->input->post('email'TRUE);
 
    $user_id = ? // not sure how you are getting this but you cannot pass it in the function call
 
    
     
// do the check
 
    if($this->usermodel->permission_exists($email$user_id)) return TRUE;

 
    return FALSE;


In that way you can still set an error message. I read you link and have never used that method, so sorry if the above suggestion is a bit off topic.
Reply
#3

@PaulD

It is a callback and does work and run the function but the set_message just is not working. The issue with doing it the way you suggest is that you now have processing stuff in your controller, which you should not do because anyone can hit the public page to run the function. Also, when I make the function private, it tells me I can't do that. I also want to use the validation in more than one class as well.

Regarding passing a value through, you actually can do that with your suggestion by doing this:


PHP Code:
$this->form_validation->set_rules( 'email', 'Email Address', 'trim|required|valid_email|callback_permission_check[passed_value]'); 

The suggestion is not really off topic, but just doesn't help in what I am wanting it to do.
Reply
#4

:-)

I didn't know you could pass a value like that.

Sorry I couldn't help. I hope someone else here will be able to help.
Reply
#5

In case anyone else wants to do this or runs into a similar issue, I found a solution to the problem which is not ideal, but will work. Turns out, you can't directly call another function in another class in the form checker otherwise it has no idea what library is being use for returning the data, but what you can do is put in an anonymous function, then with in that call a separate function to do whatever you want and return that. Basically, I changed the following and now it works the way I want it to:


PHP Code:
$this->load->library( 'form_validation' );
$this->load->helper( 'form' );

$this->form_validation->set_rules( 'email', 'Email Address', [ 'trim', 'required', 'valid_email', [ 'permission_exists', $this->Usermodel->permission_exists( $this->input->post( 'email' ), $user['id'] ) ] ] );  

to this:


PHP Code:
$this->user_id $user['id'];
$this->load->library'form_validation' );
$this->load->helper'form' );

$this->form_validation->set_rules'email''Email Address', [ 'trim''required''valid_email', [ 'permission_exists', function ( $email ){ return $this->Usermodel->permission_exists$email$this->user_id ); } ] ] ); 

I kept everything in the permission_exists function exactly the same.

Would be cool if codeigniter could add something like I was wanting to do directly as it could make form validation a lot more powerful.
Reply
#6

(01-31-2017, 04:37 AM)burgoyn1 Wrote: In case anyone else wants to do this or runs into a similar issue, I found a solution to the problem which is not ideal, but will work. Turns out, you can't directly call another function in another class in the form checker otherwise it has no idea what library is being use for returning the data, but what you can do is put in an anonymous function, then with in that call a separate function to do whatever you want and return that. Basically, I changed the following and now it works the way I want it to:


PHP Code:
$this->load->library( 'form_validation' );
$this->load->helper( 'form' );

$this->form_validation->set_rules( 'email', 'Email Address', [ 'trim', 'required', 'valid_email', [ 'permission_exists', $this->Usermodel->permission_exists( $this->input->post( 'email' ), $user['id'] ) ] ] );  

to this:


PHP Code:
$this->user_id $user['id'];
$this->load->library'form_validation' );
$this->load->helper'form' );

$this->form_validation->set_rules'email''Email Address', [ 'trim''required''valid_email', [ 'permission_exists', function ( $email ){ return $this->Usermodel->permission_exists$email$this->user_id ); } ] ] ); 

I kept everything in the permission_exists function exactly the same.

Would be cool if codeigniter could add something like I was wanting to do directly as it could make form validation a lot more powerful.

 Hi burgoyn1, excuse for my bad English; I speak spanish, thank you for the help, here there is other
 way to use a variable  when You use an anonymos function
PHP Code:
       $inicio $this->input->post('inicio');
 
       $finalizacion $this->input->post('finalizacion');

        
$this->form_validation->set_rules'finalizacion''Finalización'
         
 'required''date'
         
   'compara_fechas'
         
      function () use ($inicio$finalizacion){
         
           return compara_fechas($inicio$finalizacion); 
             
  
         
   
         
 
        ); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB