Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 81997

Unique Validator

$
0
0

I wanted to use the built-in "unique" validator for Editor but I didn't know how to use it conditionally: The field may have the values "0", "1" and "2". Only the "2" must be unique in the database. The "0"s and "1"s are occuring may times.

I ended up using the code below. Is there a way to use the built-in validator here?

if ($lang === 'de') {     
    $msg[0] = 'Feld darf nicht leer sein.'; 
    $msg[1] = 'Es kann nur einen Marktplatz Betreiber geben.';
} else {
    $msg[0] = 'Field may not be empty.';
    $msg[1] = 'There can only be one marketplace operator.';
}
.............
Field::inst( 'creditor.type' )
    ->validator( function ( $val, $data, $opts ) use ($msg, $db) {
        if ( $val == '2' ) { //marketplace operator: there is only one!
            $row = $db->raw()
                ->exec('SELECT COUNT(*) FROM creditor WHERE type = 2')
                ->fetch(PDO::FETCH_ASSOC);
            if ( (bool)$row["COUNT(*)"] ) {
                return $msg[1];
            }
        }
        return true;
    } ), 

Viewing all articles
Browse latest Browse all 81997

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>