![]() |
Form validation by my own method - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Form validation by my own method (/showthread.php?tid=61557) |
Form validation by my own method - Valery - 04-25-2015 Hello all! I have a very simple task, but I can't make this :-( I have to check login and password in the database. So I have simple web form with two fields: login and password. I have a controller with rules: PHP Code: $rules = array( And I have the model Users with the method get_access. But this method takes two parameters (login, password). The question is, how can I pass second parameter to method get_access()? Thank you very much in advance! P.S.: Sorry for my English. RE: Form validation by my own method - lexxtoronto - 04-26-2015 For example my controller calls a method in login_model like this: $usr_result=$this->login_model->get_user($username,$password); So in your case this would be: $this->users->get_access($username, $password); RE: Form validation by my own method - RogerMore - 04-27-2015 Hey Valery, If your using form validation, you can use your own function as a callback function to do the necessary checks. You would have to change a couple of things, because a validation function can only pass through the form input which is checked. You could set it up like this (taken from the user guide with some changes): PHP Code: public function index() And here you have a username/password validation workaround for 2 variables. ![]() Happy coding! RE: Form validation by my own method - Valery - 04-28-2015 Hello, RogerMore! Thank you for aswer. But callback function doesn't suit me - I'd like to use this function not only in this controller. But this form ($this->input->post("password")) is what I was looking for. Thank you! If you don't mind, I have one more question. If I am using callback function, I have to declare it such: PHP Code: public function my_callback_function($str) How can I hide such functions? I've tried "protected" and "private", but it doesn't work. Thank you. RE: Form validation by my own method - Valery - 04-28-2015 Hello, lexxtoronto! Thank you for your answer! But how can I use method wich takes two parameters in the array with rules? "rules" => array("trim", "required", array("is_accessable", array($this->users, "get_access"))), If I don't pass any parameters it means that this function takes only one parameter "login.value". Isn't? How can I pass second parameter in this $rules array? RE: Form validation by my own method - mwhitney - 04-28-2015 (04-28-2015, 04:12 AM)Valery Wrote: Hello, RogerMore! If you create a public function with an underscore as the first character of the function name, CI won't route that function to a URL, but it will still be visible to the form_validation class. So, you would define the method like this: Code: public function _my_callback_function($str) Then you would set your validation rule as: Code: callback__my_callback_function (Note the double underscore after the first instance of the word 'callback'.) RE: Form validation by my own method - lexxtoronto - 04-28-2015 @Valery, Not sure how to pass 2 parameters. But if your users->get_access($usr,$pwd) takes two parameters and you don't supply any parameters or supply just one then get_access($usr,$pwd) will not be called. For example this is how I check if the username registered already: Code: $username = $this->input->post("txt_username"); It will query users table and see if there is $username. So maybe you could add something like this? Code: $this->form_validation->set_rules('txt_password', 'Password', 'is_unique[users.password]'); Of course you have to hash it first. RE: Form validation by my own method - CroNiX - 04-28-2015 You can always extend the Form_validation library and add your custom rules which would be accessible anywhere the form_validation library is used. http://www.codeigniter.com/user_guide/general/creating_libraries.html#extending-native-libraries /application/libraries/MY_Form_validation.php PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Then in your controller(s): PHP Code: $this->form_validation->set_rules('fieldname', 'field text', 'trim|custom_rule'); You can also create rules that receive extra parameters, which are passed in between the [] when setting the rule, like "min[5]". Anything in [] will automatically be passed to your rule definition as a 2nd parameter PHP Code: function some_rule($input, $extra = '') if you used: PHP Code: $this->form_validation->set_rules('fieldname', 'field text', 'trim|some_rule[something here]'); If you look at how they are creating the native rules in the /system/libraries/Form_validation.php, it could be helpful when creating your custom rules. RE: Form validation by my own method - Valery - 04-29-2015 @lexxtoronto , @CroNiX , @mwhitney , @RogerMore THANK YOU VERY MUCH! |