• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
passing variable into anonymous function - form validation

#1
Hello, i would ask simple question.

is there any possibility to pass own parameter into anonymous function in set_rules for form_validation like its shown in first example below or its just possible to pass it trought callback function ? Thank you

Code:
$this->load->model('admin/currency_symbols_model', 'currency_symbols');

$currency_id = 5;

$this->form_validation->set_rules('symbol_id', 'lang:symbol', array(
                       'trim', 'required',
                       array(
                               'custom_fnc_000002[' . $currency_id . ']',
                               function ($symbol_id, $currency_id)
                               {                                                                                
                                       $symbol = $this->currency_symbols->get_symbol_by_id($symbol_id);

                if ($currency_id == $symbol->currency_id)
                {
                        return TRUE;
                }

                return FALSE;
                               }
                       )
               ));
Reply

#2
Not possible in the way you want it to, but not necessary either:

Code:
$currency_id      = 1; // your custom parameter
$currency_symbols = $this->currency_symbols;

// Use $closure as your rule
$closure = function ($symbol_id) use ($currency_id, $currency_symbols) {

    $symbol = $currency_symbols->get_symbol_by_id($symbol_id);
    return ($symbol->currency_id == $currency_id);
};

EDIT: But actually, your code wouldn't work anyway as $this inside the anonymous function refers to the function itself, not to your controller, so you actually MUST use the use keyword to pass your currency_symbols object ... updated the code above.
Reply

#3
ok in that case it isnt readable very well and not usefull so much (strange way to do that) .. i will use rather callback function .. there i can pass any variable and inside callback function $this refers to controllers class ... thanks for answer
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.