Zend framework 2 callback filter

2019-08-01 08:06发布

问题:

In ZF2, is it possible to pass form values array to the callback filter similar to the callback validator?

//Validator callback works
'callback' => function($value, $context){
  //$context contains form values
}

//Need similar functionality for filter
'callback' => function($value, $context){
  //$context will issue a warning because its not set
}

//I know the following filter works, but I dont know how to pass the form
'callback' => function($value, $context){
  print_r($context); //Prints 'hello world'
},
'options' => array(
  'callback_params' => array(
    'context' => 'hello world' //I need this to be the form values
  )
)

回答1:

InputFilter doesn't have what you want, But you can pass the Form object to your callback using use and get the raw form values from input filter

$form = $this;

'callback' => function($value) use ($form) {
  var_dump($form->getInputFilter()->getRawValues());
}