Welcome Guest, Not a member yet? Register   Sign In
Trouble with callback and default parameter
#1

[eluser]jojo777[/eluser]
Ok here is my problem. I've got a form and the validation rules for all the inputs, also i´ve got a callback for checking the credentials for the user.

Its like this
Code:
$this->form_validation->set_rules('email', 'Email', 'trim|required|callback__validate_credentials|valid_email|xss_clean');

So the code will call function _validate_credentials that has a default parameter

Code:
function _validate_credentials($only_identity=FALSE){

     if( ! $only_identity ){
      echo 'the param is FALSE!!';
      #...
     }
}

As you can see in the callback i did not set the value of the param so it MUST be false, but this must be magic because is always getting the value in email field. o.O!!

The funny thing is in my index I've tried this

Code:
public function index(){
     $this->_validate_credentials()
    }

And returns the echo (nice...) So what the heck is going on!??

Thanks!
#2

[eluser]solid9[/eluser]
Actually that is normal if you put your codes inside an index function/method,
It will be executed automatically so don't be surprised this is the default and nothing to worry.

Also if you want to check if a duplicate email exist in your database,
You can just simply use the <b>is_unique</b> to check for email duplicate as native rules,
in your form validation.


#3

[eluser]jojo777[/eluser]
Maybe i didnt explain it good, I need the callback in that call took false, as the default value says, because this function is used in other situation but passing TRUE.

The validation rules are in a function that isn't index and its not taking the default nor any value I pass (TRUE for example...)

Why?

Thanks
#4

[eluser]solid9[/eluser]
Actually your codes are confusing.
Not tested but try this,
Code:
function _validate_credentials($only_identity=FALSE){
     if($only_identity == FALSE){
      echo 'the param is FALSE!!';
      #...
     }
}

or you can try isset() also
Not tested but try,
Code:
function _validate_credentials($only_identity){
     if(!isset($only_identity)){
      echo 'the param is not SET!!';
      #...
     }
}


#5

[eluser]jojo777[/eluser]
Sorry if the code looks confused, the main idea is use the same callback in 2 situations:
- If is false: check 2 input values.
- If true: check only 1.

I'll try it later. When i do iĺl give feedback.

Thanks!
#6

[eluser]CroNiX[/eluser]
The first parameter passed to a validation callback function is always the VALUE that that form control sent. In this case the value of the email field, which will not be a boolean FALSE. So $only_identity can't be FALSE which is why this isn't working.

You can only manually pass a parameter to the callback function if you use the square brackets and pass it in there, which will be sent as the 2nd parameter to your callback. This is always a string.

Send the string 'false' as a 2nd parameter to the callback function:
Code:
$this->form_validation->set_rules('email', 'Email', 'trim|required|callback__validate_credentials[false]|valid_email|xss_clean');

Receive the 2nd parameter in the callback function:
Code:
function _validate_credentials($field_value, $only_identity='true')
{
  if ($only_identity == 'false')
  {
     //the parameter was set and it was false
  }
  else
  {
     //the parameter was not set, so it must be true
  }
And to use the same callback without sending the 2nd parameter, just remove the brackets, and $only_identity will be considered 'true':
Code:
$this->form_validation->set_rules('email', 'Email', 'trim|required|callback__validate_credentials|valid_email|xss_clean');
#7

[eluser]jojo777[/eluser]
Thank you very much to both.

It was breaking my mind and then I realized the $only_identity was taking the email value always, now I know why

Thanks again!!




Theme © iAndrew 2016 - Forum software by © MyBB