CodeIgniter Forums
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(
    array(
    
"field" => "login",
    
"label" => "Username",
    
"rules" => array("trim""required", array("is_accessable", array($this->users"get_access"))),
    ),
    array(
    
"field" => "password",
    
"label" => "Password",
    
"rules" => "trim|required",
    ),
);
$this->form_validation->set_message("is_accessable""Wrong login and password."); 

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()

        {
                $this->load->helper(array('form''url'));

                $this->load->library('form_validation');

                $this->form_validation->set_rules('username''Username''callback_username_check');
                $this->form_validation->set_rules('password''Password''required');
                $this->form_validation->set_rules('passconf''Password Confirmation''required');
                $this->form_validation->set_rules('email''Email''required|is_unique[users.email]');

                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('myform');
                }
                else
                {
                        $this->load->view('formsuccess');
                }
        }


        public function username_check($username)

        {
                $password $this->input->post("password");
               
                $this
->load->model("users");
               
                
// use a model function to check the credentials
                return $this->users->check_user_pass($username$password);
                
                
// notice: get_access has to return true for success or false when user and/or password are wrong, because you have to tell form validation if your validation checks out or not.
               
        


And here you have a username/password validation workaround for 2 variables. Wink

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)
{
 
  ...

And there is an opportunity to call this function via URL, for example: http://www.mysite.com/my_controller/my_callback_function/aaa

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!

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)
{
 
  ...

And there is an opportunity to call this function via URL, for example: http://www.mysite.com/my_controller/my_callback_function/aaa

How can I hide such functions? I've tried "protected" and "private", but it doesn't work.

Thank you.

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");
$password = $this->input->post("txt_password");

$this->form_validation->set_rules('txt_username', 'Username', 'trim|required|min_length[5]|max_length[18]|is_unique[users.username]|xss_clean');

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');

class 
MY_Form_validation extends CI_Form_validation {
    
    function 
__construct()
    {
        
parent::__construct();
    }

    
//a new custom rule
    
public function custom_rule($input)
    {
        
//$input is the user input for this form field
        
if ($input == "hello")
        {
            return 
TRUE//passed validation
        
} else {
            
//Failed! Set error message (using name of rule)
            
$this->set_message('custom_rule''The %s field can ONLY be "hello".');
        }
        
//If we made it this far, we failed. Send FALSE back
        
return FALSE;
    }


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]'); 
Then $extra will = "something here" in your rule. Useful for passing in extra parameters if you need them (like identifying a db table). Please note that this value will always be a string passed to the rule.

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!