Using external function for validation
Using external function for validation

Hello all;
I've been using an custom validation function with raw SQL queries, and it works fine.
->validator( function ( $val, $data, $field, $host ) use ($db) {
//do validation
[...]
};)
Because I'm using it on more than one field, I wanted to move it to a separate function and call it from the validators. But I can't seem to get the use($db) part to work.
function valstuff ($param, $host, $db) {
//do validation
[...]
};
[...]
->validator( function ( $val, $data, $field, $host ) use ($db) {
//calculate $param based on which field this is
valstuf( $param, $host, $db);
};)
The way it's written here, I get an undefined variable db error. If I move use($db) into the new function, I get an "unexpected use" error.
This might be more of a PHP problem than a DataTables one, but I'm stuck regardless. Any pointers?
This question has an accepted answers - jump to answer
Answers
The
use
in your validator function looks fine.My guess is that you've put
function valstff...
before theuse
statement for the namespaces (mildly annoying thinking about it that PHP have useduse
in two different contexts as a keyword for different things).Allan
That was it. Thanks again.