How to use required validation on editor.dependent (show) function

How to use required validation on editor.dependent (show) function

Helpdesk LionesaHelpdesk Lionesa Posts: 21Questions: 3Answers: 1

HI,
I am new to Datatables and have spent a couple of days trying to find the solution without making any head way. So please forgive me if this is a newbie question.

In the insert\edit function i have 2 fields that only appears when one specific value on other field is selected:

editor.dependent( 'p2b_users.naccessusers', function ( val ) {
return val === '1' ?
{ show: ['p2b_users.supervisor_id','p2b_users.sfid'] } :
{ hide: [] }; } );

I´m trying to use the required\notEmpty function, but if i use this way i'm not allowed to submit the form even without triggering the editor.dependent (show) event.

Field::inst( 'p2b_users.sfid' )
->validator( Validate::required() )
->validator( Validate::numeric() )
->validator( Validate::unique(ValidateOptions::inst()
->message('SFID já atribuído.')
))

Is it possible to use the required validator ONLY if those fields where visible triggered by the editor.dependent (show) event?

Can someone help me please?
Thank you very much.

Replies

  • allanallan Posts: 57,229Questions: 1Answers: 9,111 Site admin

    Hi,

    Is it possible to use the required validator ONLY if those fields where visible triggered by the editor.dependent (show) event?

    Yes, but it requires a little change to how your server-side code works. Basically you want to conditionally add the validation for those optional fields. If the value is such that it needs the validation, only then would you add them.

    That can be done with server-side events. In this case the preEdit and preCreate events are the two you want to listen for.

    Inside the function add the validation e.g.:

    ->on('preCreate', function ($editor, $values) {
      if ($values['p2b_users']['naccessusers'] == 1) {
        $editor->field('p2b_users.sfid')->validate( ... ); // add as many as needed
      }
    })
    

    Regards,
    Allan

  • Helpdesk LionesaHelpdesk Lionesa Posts: 21Questions: 3Answers: 1

    Hi Colin,

    Thank you for your help. Already applied and functional. Simple as shared.
    Regards,

    João

  • Helpdesk LionesaHelpdesk Lionesa Posts: 21Questions: 3Answers: 1

    Hi Allan,

    After all, I couldn't get it to work on my project.
    I can get it up and running if I don't use leftjoin, but unfortunately I do.
    I don't know where I went wrong ... as soon as I add the leftjoin and it stops working giving the following error on submit:

    PHP:

            Editor::inst( $db, 'tlmk_users', 'idUsers' )
                ->fields(
                    Field::inst( 'tlmk_users.uidusers' )
                        ->validator( Validate::notEmpty(ValidateOptions::inst()
                                ->message('Insira um username.')
                            ))
                        ->validator( Validate::unique(ValidateOptions::inst()
                                ->message('Username já existe.')
                            )),
    
    
    
    
                    Field::inst( 'tlmk_users.emailusers' )
                        ->validator( Validate::notEmpty(ValidateOptions::inst()
                                    ->message('Insira um endereço de e-mail válido <br>
                                                    @vodafone.com ou @corp.vodafone.pt')
                                    ))
                        ->validator( Validate::unique(ValidateOptions::inst()
                                    ->message('E-mail já existe.')
                                    ))
                            ->setFormatter( function ( $val, $data ) {
                                        return strtolower( $val );
                                        })
                        ->validator( function ( $val, $data, $opts ) {
                                    return !preg_match("/^[a-zA-Z0-9_.+-]+@(corp.vodafone.pt|vodafone.com)$/", $val) ?
                                                    'Insira um endereço de e-mail válido <br>
                                                    @vodafone.com ou @corp.vodafone.pt' :
                                        true;
                                    })
                        ->validator( Validate::email() ),
    
    
    
    
                    Field::inst( 'tlmk_users.naccessusers' )
                        ->options( Options::inst()
                                      ->table( 'tlmk_tipo_acesso' )
                                      ->value( 'tipo_acesso_id' )
                                      ->label( 'tipo_acesso' )
                                   )
                                ->validator( Validate::notEmpty(ValidateOptions::inst()
                                                     ->message('Selecione tipo de acesso.')
                                                     )),
                    Field::inst( 'tlmk_tipo_acesso.tipo_acesso' ),
    
    
    
    
                    Field::inst( 'tlmk_users.equipa_id' )
                        ->options( Options::inst()
                                                    ->table( 'tlmk_equipas' )
                                                    ->value( 'equipa_id' )
                                                    ->label( 'equipa' )
                                         ),
                Field::inst( 'tlmk_equipas.equipa' ),
    
    
    
    
                    Field::inst( 'tlmk_users.status' )
                        ->validator( Validate::notEmpty() )
                )
    
    
                    ->on('preCreate', function ($editor, $values) {
                        if ($values['tlmk_users.naccessusers'] == 1) {
                            $editor->field('tlmk_users.equipa_id')->validator( Validate::notEmpty(ValidateOptions::inst()
                                                         ->message('Selecione a equipa.')
                                                         ));
                        }
                        })
    
    
                ->leftJoin( 'tlmk_tipo_acesso', 'tlmk_tipo_acesso.tipo_acesso_id', '=', 'tlmk_users.nAccessUsers' )
                ->leftJoin( 'tlmk_equipas', 'tlmk_equipas.equipa_id', '=', 'tlmk_users.equipa_id' )
                ->process( $_POST )
                ->json();
    

    JS:

        (function($){
    
        $(document).ready(function() {
            var editor = new $.fn.dataTable.Editor( {
                ajax: 'php/table.tlmk_users.php',
                table: '#tlmk_users',
                fields: [
                    {
                        "label": "uidUsers:",
                        "name": "tlmk_users.uidusers"
                    },
                    {
                        "label": "emailUsers:",
                        "name": "tlmk_users.emailusers"
                    },
                    {
                        "label": "nAccessUsers:",
                        "name": "tlmk_users.naccessusers",
                        "type": "select",
                        "placeholder": "Selecione o tipo de acesso"
                    },
                    {
                        "label": "equipa_id:",
                        "name": "tlmk_users.equipa_id",
                        "type": "select",
                        "placeholder": "Selecione a equipa"
                    },
                    {
                        "label": "status:",
                        "name": "tlmk_users.status",
                        "type": "select",
                        "options": [
                            "Ativo",
                            "Inativo"
                        ]
                    }
                ]
            } );
    
    
            editor.dependent( 'tlmk_users.naccessusers', function ( val ) {
                 return val === 'Selecione o tipo de acesso' ?
                 { show: [] } :
                 { hide: ['tlmk_users.equipa_id'] };    } );
    
            editor.dependent( 'tlmk_users.naccessusers', function ( val ) {
                    return val === '1' ?
                    { show: ['tlmk_users.equipa_id'] } :
                    { hide: [] };   } );
    
            var table = $('#tlmk_users').DataTable( {
                dom: 'Bfrtip',
                ajax: 'php/table.tlmk_users.php',
                columns: [
                    {
                        "data": "tlmk_users.uidusers"
                    },
                    {
                        "data": "tlmk_users.emailusers"
                    },
                    {
                        "data": "tlmk_tipo_acesso.tipo_acesso"
                    },
                    {
                        "data": "tlmk_equipas.equipa"
                    },
                    {
                        "data": "tlmk_users.status"
                    }
                ],
                select: true,
                lengthChange: false,
                buttons: [
                    { extend: 'create', editor: editor },
                    { extend: 'edit',   editor: editor },
                    { extend: 'remove', editor: editor }
                ]
            } );
        } );
    
        }(jQuery));
    

    Can anyone help?
    Project link:http://tlmk.qualportal.com/testes/

    Regards,
    João

  • Helpdesk LionesaHelpdesk Lionesa Posts: 21Questions: 3Answers: 1

    Hi,

    Problem solved.

    ->on('preCreate', function ($editor, $values) {
                if ($values['tlmk_users']['naccessusers'] == 1) {
                    $editor->field('tlmk_users.equipa_id')->validator( Validate::notEmpty(ValidateOptions::inst()
                                                 ->message('Selecione a equipa.')
                                                 ));
                }
                })
    

    Line 2 was wrong,I was not setting the value correctly.
    Case closed.
    Thanks for the help Allan.

    Regards,
    João

  • colincolin Posts: 14,520Questions: 1Answers: 2,481

    Glad all sorted, thanks for reporting back,

    Colin

This discussion has been closed.