Welcome Guest, Not a member yet? Register   Sign In
Form validation by my own method
#1

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.
Reply
#2

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);
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply
#3

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

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_c...nction/aaa

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

Thank you.
Reply
#5

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?
Reply
#6

(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_c...nction/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'.)
Reply
#7

(This post was last modified: 04-28-2015, 10:56 AM by lexxtoronto.)

@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.
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply
#8

(This post was last modified: 04-28-2015, 11:51 AM by CroNiX.)

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/ge...-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.
Reply
#9

@lexxtoronto , @CroNiX , @mwhitney , @RogerMore
THANK YOU VERY MUCH!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB