Anonymous function Validation |
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' ); Then in the other class the following function: PHP Code: function permission_exists( $email, $id ) 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!
01-30-2017, 11:40 AM
(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() { 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.
@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.
:-)
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.
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' ); to this: PHP Code: $this->user_id = $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.
(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: 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'); |
Welcome Guest, Not a member yet? Register Sign In |