Editor validator $host
Editor validator $host
Loren Maxwell
Posts: 469Questions: 114Answers: 10
in Editor
From: https://editor.datatables.net/manual/php/validation#Custom-field-validators
Is there an example of what $host does?
Field::inst( 'last_name' )
->validator( function ( $val, $data, $field, $host ) {
return strlen( $val ) > 50 ?
'Name length must be 50 characters or less' :
true;
} );
Can I access the database with that?
Right now I'm doing the below but was wondering if $host offered a different option:
Field::inst( 'last_name' )
->validator( function ( $val, $data, $field, $host ) use ($db) {
$db->stuff();
return strlen( $val ) > 50 ?
'Name length must be 50 characters or less' :
true;
} );
This question has an accepted answers - jump to answer
Answers
The
$hostparameter is this object here. Initially it wasn't really part of the public API - I needed it for thedbUniquevalidator, and it has expanded a bit since then. I will look at adding it to the public API documentation as it is useful.You can get the transaction db resource using
$host['db'], which can be a little more useful than the global$dbsince the default is to use transactions (how useful that is depends on the full setup and what you are doing, but it is worth being aware of).Allan
Ahhh... (insert forehead smack emoji)
I kept trying
$host->db()but didn't think about trying it as an array, althoughvar_dump($host)clearly showed it was.In my mind, just to keep the code more encapsulated it'd be nice to be able to access the database without the global
$db.