Global Validator - Undefined Index

Global Validator - Undefined Index

peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

I'm trying to use the global validator as per the example at:

https://editor.datatables.net/manual/php/validation#Examples

I'm getting an error:

Notice: Undefined index: learning_event.low_vision in /var/www/html/curriculum_mapper/programs/cm_dopt/program_data/learning_event_test_only_data.php on line 114
{"error":"Low vision is not 2","data":[]}

The code is:

use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

Editor::inst( $db_cm_dopt, 'learning_event', 'learning_event_pk' )    
    ->field(
        Field::inst( 'learning_event.learning_event_name' ),
        Field::inst( 'learning_event.outline' ),
        Field::inst( 'learning_event.low_vision' ),
        Field::inst( 'learning_event.contact_lenses' ),
        Field::inst( 'learning_event.paediatrics' ),
        Field::inst( 'learning_event.binocular_vision' ),
        Field::inst( 'learning_event.vision_therapy' ),
        Field::inst( 'learning_event.therapeutics' ),
        Field::inst( 'learning_event.research' ),
        Field::inst( 'learning_event.clinical_practice' ),
        Field::inst( 'learning_event.optics' ),
        Field::inst( 'learning_event.broad_vision_sciences' ),
        Field::inst( 'learning_event.other' ),
        Field::inst( 'learning_event.modified' ),
        Field::inst( 'learning_event.modified_by' )->setValue( $user),
        Field::inst( 'learning_event.unit_group_fk' )
            ->options( Options::inst()
                ->table( 'unit_group' )
                ->value( 'unit_group_pk' )
                ->label( 'unit_group' )
                ->where( function ($q) {
            $q->where( 'unit_group.type', 'learning_event' );
                })
            ),
        Field::inst( 'unit_group.unit_group' )
    )
     ->leftJoin( 'unit_group', 'unit_group.unit_group_pk', '=', 'learning_event.unit_group_fk' )
        ->join(
        Mjoin::inst( 'program_outcome' )
            ->link( 'learning_event.learning_event_pk', 'learning_event_program_outcome_lookup.learning_event_fk' )
            ->link( 'program_outcome.program_outcome_pk', 'learning_event_program_outcome_lookup.program_outcome_fk' )
            ->order( 'program_outcome.program_outcome asc' )
            ->fields(
                Field::inst( 'program_outcome_pk' )
                    ->options( Options::inst()
                        ->table( 'program_outcome' )
                        ->value( 'program_outcome_pk' )
        ->label( array('type', 'program_outcome') )
        ->render( function ( $row ) {
            return $row['type'] . ' - ' . $row['program_outcome'];
        } )
                        ->order( 'type' )
                    ),
                Field::inst( 'program_outcome' )
            )
    )
    ->join(
        Mjoin::inst( 'presentation' )
            ->link( 'learning_event.learning_event_pk', 'learning_event_presentation_lookup.learning_event_fk' )
            ->link( 'presentation.presentation_pk', 'learning_event_presentation_lookup.presentation_fk' )
            ->order( 'presentation.presentation_name asc' )
            ->fields(
                Field::inst( 'presentation_pk' )
                    ->options( Options::inst()
                        ->table( 'presentation' )
                        ->value( 'presentation_pk' )
                        ->label( 'presentation_name' )                
                    ),
                Field::inst( 'presentation_name' )
            )
    )
    ->join(
        Mjoin::inst( 'mcondition' )
            ->link( 'learning_event.learning_event_pk', 'learning_event_mcondition_lookup.learning_event_fk' )
            ->link( 'mcondition.mcondition_pk', 'learning_event_mcondition_lookup.mcondition_fk' )
           ->order( 'mcondition.mcondition_name asc' )
            ->fields(
                Field::inst( 'mcondition_pk' )
                    ->options( Options::inst()
                        ->table( 'mcondition' )
                        ->value( 'mcondition_pk' )
                        ->label( 'mcondition_name' )          
                    ),
                Field::inst( 'mcondition_name' )
            )
    )
    ->join(
        Mjoin::inst( 'learning_event_type' )
            ->link( 'learning_event.learning_event_pk', 'learning_event_type_lookup.learning_event_fk' )
            ->link( 'learning_event_type.learning_event_type_pk', 'learning_event_type_lookup.learning_event_type_fk' )
           ->order( 'learning_event_type.learning_event_type_name asc' )
            ->fields(
                Field::inst( 'learning_event_type_pk' )
                    ->options( Options::inst()
                        ->table( 'learning_event_type' )
                        ->value( 'learning_event_type_pk' )
                        ->label( 'learning_event_type_name' )         
                    ),
                Field::inst( 'learning_event_type_name' )
            )
    )
          ->validator( function ( $editor, $action, $data ) {
        if ( $action === Editor::ACTION_CREATE || $action === Editor::ACTION_EDIT ) {
            foreach ( $data['data'] as $pkey => $values ) {
                if ( $values['learning_event.low_vision'] !== '2' ) {
                    return 'Low vision is not 2';
                }
            }
        }
    } )
    ->process($_POST)
    ->json();

This question has an accepted answers - jump to answer

Answers

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0
    edited August 2021

    Client Side:


    var editor = new $.fn.dataTable.Editor( { ajax: "program_data/learning_event_test_only_data.php", table: "#learning_event_table", template: '#learning_event_form', fields: [ { label: "Learning Event:", name: "learning_event.learning_event_name" }, { label: "Outline:", name: "learning_event.outline", type: "ckeditor" }, { label: "Program Outcome:", name: "program_outcome[].program_outcome_pk", type: "select", placeholder: 'No selection', placeholderDisabled: false, placeholderValue: 0, multiple: true }, { label: "Unit Group:", name: "learning_event.unit_group_fk", type: "select", placeholder: "Select Unit Group..." }, { label: "Low Vision:", name: "learning_event.low_vision", type: "select", options: [ { label: "0", value: 0 }, { label: "1", value: 1 }, { label: "2", value: 2 }, { label: "3", value: 3 }, { label: "4", value: 4 }, { label: "5", value: 5 }, { label: "6", value: 6 }, { label: "7", value: 7 }, { label: "8", value: 8 }, { label: "9", value: 9 }, { label: "10", value: 10 } ] }, { label: "Contact Lenses:", name: "learning_event.contact_lenses", type: "select", options: [ { label: "0", value: 0 }, { label: "1", value: 1 }, { label: "2", value: 2 }, { label: "3", value: 3 }, { label: "4", value: 4 }, { label: "5", value: 5 }, { label: "6", value: 6 }, { label: "7", value: 7 }, { label: "8", value: 8 }, { label: "9", value: 9 }, { label: "10", value: 10 } ] }, { label: "Paediatrics:", name: "learning_event.paediatrics", type: "select", options: [ { label: "0", value: 0 }, { label: "1", value: 1 }, { label: "2", value: 2 }, { label: "3", value: 3 }, { label: "4", value: 4 }, { label: "5", value: 5 }, { label: "6", value: 6 }, { label: "7", value: 7 }, { label: "8", value: 8 }, { label: "9", value: 9 }, { label: "10", value: 10 } ] }, { label: "Binocular Vision:", name: "learning_event.binocular_vision", type: "select", options: [ { label: "0", value: 0 }, { label: "1", value: 1 }, { label: "2", value: 2 }, { label: "3", value: 3 }, { label: "4", value: 4 }, { label: "5", value: 5 }, { label: "6", value: 6 }, { label: "7", value: 7 }, { label: "8", value: 8 }, { label: "9", value: 9 }, { label: "10", value: 10 } ] }, { label: "Vision Therapy:", name: "learning_event.vision_therapy", type: "select", options: [ { label: "0", value: 0 }, { label: "1", value: 1 }, { label: "2", value: 2 }, { label: "3", value: 3 }, { label: "4", value: 4 }, { label: "5", value: 5 }, { label: "6", value: 6 }, { label: "7", value: 7 }, { label: "8", value: 8 }, { label: "9", value: 9 }, { label: "10", value: 10 } ] }, { label: "Therapeutics:", name: "learning_event.therapeutics", type: "select", options: [ { label: "0", value: 0 }, { label: "1", value: 1 }, { label: "2", value: 2 }, { label: "3", value: 3 }, { label: "4", value: 4 }, { label: "5", value: 5 }, { label: "6", value: 6 }, { label: "7", value: 7 }, { label: "8", value: 8 }, { label: "9", value: 9 }, { label: "10", value: 10 } ] }, { label: "Research:", name: "learning_event.research", type: "select", options: [ { label: "0", value: 0 }, { label: "1", value: 1 }, { label: "2", value: 2 }, { label: "3", value: 3 }, { label: "4", value: 4 }, { label: "5", value: 5 }, { label: "6", value: 6 }, { label: "7", value: 7 }, { label: "8", value: 8 }, { label: "9", value: 9 }, { label: "10", value: 10 } ] }, { label: "Clinical Practice:", name: "learning_event.clinical_practice", type: "select", options: [ { label: "0", value: 0 }, { label: "1", value: 1 }, { label: "2", value: 2 }, { label: "3", value: 3 }, { label: "4", value: 4 }, { label: "5", value: 5 }, { label: "6", value: 6 }, { label: "7", value: 7 }, { label: "8", value: 8 }, { label: "9", value: 9 }, { label: "10", value: 10 } ] }, { label: "Optics:", name: "learning_event.optics", type: "select", options: [ { label: "0", value: 0 }, { label: "1", value: 1 }, { label: "2", value: 2 }, { label: "3", value: 3 }, { label: "4", value: 4 }, { label: "5", value: 5 }, { label: "6", value: 6 }, { label: "7", value: 7 }, { label: "8", value: 8 }, { label: "9", value: 9 }, { label: "10", value: 10 } ] },{ label: "Broad Vision Sciences:", name: "learning_event.broad_vision_sciences", type: "select", options: [ { label: "0", value: 0 }, { label: "1", value: 1 }, { label: "2", value: 2 }, { label: "3", value: 3 }, { label: "4", value: 4 }, { label: "5", value: 5 }, { label: "6", value: 6 }, { label: "7", value: 7 }, { label: "8", value: 8 }, { label: "9", value: 9 }, { label: "10", value: 10 } ] },{ label: "Other:", name: "learning_event.other", type: "select", options: [ { label: "0", value: 0 }, { label: "1", value: 1 }, { label: "2", value: 2 }, { label: "3", value: 3 }, { label: "4", value: 4 }, { label: "5", value: 5 }, { label: "6", value: 6 }, { label: "7", value: 7 }, { label: "8", value: 8 }, { label: "9", value: 9 }, { label: "10", value: 10 } ] }, { label: "Presentations:", name: "presentation[].presentation_pk", type: "select", placeholder: 'No selection', placeholderDisabled: false, placeholderValue: 0, multiple: true }, { label: "Conditions:", name: "mcondition[].mcondition_pk", type: "select", placeholder: 'No selection', placeholderDisabled: false, placeholderValue: 0, multiple: true }, { label: "Type:", name: "learning_event_type[].learning_event_type_pk", type: "select", placeholder: 'No selection', placeholderDisabled: false, placeholderValue: 0, multiple: true, attr: { size: 1 } } ] } ); var table = $( '#learning_event_table' ).DataTable( { responsive: true, "autoWidth": false, "lengthMenu": [ [ 5, 10, 25, 50, -1 ], [ 5, 10, 25, 50, "All" ] ], columnDefs: [ { targets: 1, render: $.fn.dataTable.render.ellipsis( 150, true ) } ], ajax: "program_data/learning_event_test_only_data.php", dom: "Blfrtip", columns: [ { data: "learning_event.learning_event_name", }, { data: "learning_event.outline", }, { data: "program_outcome", render: "[, ].program_outcome" }, { data: "unit_group.unit_group" }, { width: '10%', render: function(data, type, row, meta){ return 'Low Vision - ' + row.learning_event.low_vision + '<br>Contact Lenses - ' + row.learning_event.contact_lenses + '<br>Paediatrics - ' + row.learning_event.paediatrics + '<br>Binocular Vision - ' + row.learning_event.binocular_vision + '<br>Vision Therapy - ' + row.learning_event.vision_therapy + '<br>Therapeutics - ' + row.learning_event.therapeutics + '<br>Research - ' + row.learning_event.research + '<br>Clinical Practice - ' + row.learning_event.clinical_practice + '<br>Optics - ' + row.learning_event.optics + '<br>Broad Vision Sciences - ' + row.learning_event.broad_vision_sciences + '<br>Other - ' + row.learning_event.other; } }, { data: "presentation", render: "[, ].presentation_name" }, { data: "mcondition", render: "[, ].mcondition_name" }, { data: "learning_event_type", render: "[, ].learning_event_type_name" }, { data: "learning_event.modified" }, { data: "learning_event.modified_by" }, { data: null, className: "center", defaultContent: '<a href="" class="editor_edit">Edit</a>' } ], select: { style: 'os', selector: 'td:first-child' }, buttons: [] } );
  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0
    edited August 2021

    What I am wanting to do is sum the values of certain drop-down select lists and if the total is greater than 10, show a message to the user and restrict creation and edit until the issue is fixed.

    The select lists for this are:

    learning_event.low_vision
    learning_event.contact_lenses
    learning_event.paediatrics
    learning_event.binocular_vision
    learning_event.vision_therapy
    learning_event.therapeutics
    learning_event.research
    learning_event.clinical_practice
    learning_event.optics
    learning_event.broad_vision_sciences
    learning_event.other
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    The way PHP parses such input parameters is as a nested array. So to address this, use:

    $values['learning_event']['low_vision']
    

    Allan

Sign In or Register to comment.