codeigniter如何知道传递给用户制定规则的参数(在form_validation中)?

Codeigniter allows users to create their own rules for validation, for instance

array (
            'field'=>'username' ,
            'label'=>'Username' ,
            'rules'=>'callback_check_username' 
        ) 

I can use the above code to check if a username already exists, creating a function check_username.

 function check_username($uname) //$uname has the username taken from the post data
    {
      //Checks if username exists or not, returns true or false  
    }

I can't comprehend as to how does check_username know's what $uname is, as the rule i have created(the code above) calls it and it has no parameters? So can anyone enlighten me on the same?

Well, the callback function obtain at least 1 param - the value posted. If you want to add more params you would add to rules this: callback_check_username[anotherParam] Then the function would have 2 params: postedValue and anotherParam. However this can be checked with codeigniter build-in function: is_unique[table.column] See the doc: http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#rulereference

CodeIgniter validation rules have three parameters:

The field name - the exact name you've given the form field.

A "human" name for this field, which will be inserted into the error message. For example, if your field is named "user" you might give it a human name of "Username". Note: If you would like the field name to be stored in a language file, please see Translating Field Names.

The validation rules for this form field.

The first, the field name, which is set as the field property in a validation rule, is what is passed to the function.

If you want to fully understand the mechanics of how this works, then you could look at CodeIgniter's code, particularly system/libraries/Form_validation.php

it looks like Codeigniter automatically passes the form value if you follow the naming convention. put the string 'callback_' before the method name like callback_username_check

and then codeigniter automatically passes the form value. so in your example '$uname'

     function check_username($uname) 

could also be function check_username($str) like in the CI docs - the variable name is just a placeholder for whatever testing you are going to do in the method.