Editor Validation - Accept only dbValues available in another column

Editor Validation - Accept only dbValues available in another column

CapamaniaCapamania Posts: 233Questions: 81Answers: 5
edited January 2017 in Editor

How exactly can I define that for 'column_04' (text input) only values available in column_02 (of the database column) ... or empty ('') are valid? Like this?

Editor::inst( $db, 'users' )
    ->field(
        Field::inst( 'users.column_01' ),
        Field::inst( 'users.column_02' ),
        Field::inst( 'users.column_03' ),
        Field::inst( 'users.column_04' )
            ->validator( 'Validate::dbValues', array( 
                'field' => 'users.column_02'
                'valid' => array(''), 
                'message' => "Value does not exist!"                
            ) )
    )
    ->process($_POST)
    ->json();

I'm not exactly sure https://editor.datatables.net/manual/php/validation

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓
    'field' => 'column_02'
    

    should be all that is needed.

    However there is one very important proviso here - it will check against values which are currently in the database. It will not accept a new value from column 2 that is being submitted at the same time.

    For that you would need to use a server-side event to add the validator and add the value from column 2 to the valid array.

    Allan

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5

    Thanks Allan. ... plus defining the 'table' like below did the trick in my case ...

    ->validator( 'Validate::dbValues', array(
            'table' => 'users',
            'field' => 'column_02',
            'valid' => array(''),
            'message' => 'Value does not exist!'               
     ) )
    

    And thanks for the hint regarding the additional scenario.

    Regards

This discussion has been closed.