Editor validator $host

Editor validator $host

Loren MaxwellLoren Maxwell Posts: 469Questions: 114Answers: 10

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

  • allanallan Posts: 65,347Questions: 1Answers: 10,841 Site admin
    Answer ✓

    The $host parameter is this object here. Initially it wasn't really part of the public API - I needed it for the dbUnique validator, 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 $db since 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

  • Loren MaxwellLoren Maxwell Posts: 469Questions: 114Answers: 10

    Ahhh... (insert forehead smack emoji)

    I kept trying $host->db() but didn't think about trying it as an array, although var_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.

Sign In or Register to comment.