Validation with join tables not working

Validation with join tables not working

carlopcarlop Posts: 37Questions: 9Answers: 1

Hi,
I have a joined field with the same structure of the example: https://editor.datatables.net/examples/simple/join.html

I suppose the row
->validator( Validate::dbValues() ),
should fail the validation if no options are selected, but it's not working in my code and in the example too.

My joined table contains no empty rows, what's the correct way to setup the validator to require a user selection?
Thank you,

Replies

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    it's all in the docs; just take a look pls
    https://editor.datatables.net/manual/php/validation

    Field::inst( 'first_name' )
                ->validator( Validate::notEmpty( ValidateOptions::inst()
                    ->message( 'A first name is required' )
                ) ),
    
  • carlopcarlop Posts: 37Questions: 9Answers: 1

    You gave me the idea to mix together validators and I made it working with 'dbValues' and 'required'.
    It never worked with 'notEmpty' or a single validator.

    The validation documentation wasn't explicit enough for me, in the dbValues paragraph the phrase: "Allow only a value that is present in a database column." made me think that empty values were not allowed. It's actually "Allow an empty value or a value that is present in a database column".

    Thank you very much

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    you can always build your own validators without any limitations like this for example:

    Field::inst( 'contract.iban' )
        ->validator( function ( $val, $data, $opts ) {
            if ($val > '') {
                if (! verify_iban($val) ) {
                    if ($_SESSION['lang'] === 'de') {   
                        return 'Bitte geben Sie eine gültige IBAN ein!';
                    } else {
                        return 'Please enter a valid IBAN!';
                    }
                }
            }
            return true;
            })
    

    You can use multiple fields from what is returned to the server as well:

    Field::inst( 'contract_has_infoma.exp_acct_number' )
        ->validator( function ( $val, $data, $opts ) use ( $msg ) {
            if ( $data['contract_has_infoma']['include'] > 0 ) {
                if ( $data['contract_has_infoma']['inc_acct_number'] <= ''
                     && $val <= ''                                          ) {
                    return $msg[2];    
                }
            }
            return true;
        } ),
    

    I think it is also worth looking at global validators in the docs.

This discussion has been closed.