Using external function for validation

Using external function for validation

andrewbellandrewbell Posts: 12Questions: 3Answers: 0

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

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    Answer ✓

    The use in your validator function looks fine.

    My guess is that you've put function valstff... before the use statement for the namespaces (mildly annoying thinking about it that PHP have used use in two different contexts as a keyword for different things).

    Allan

  • andrewbellandrewbell Posts: 12Questions: 3Answers: 0

    That was it. Thanks again.

This discussion has been closed.