Custom Validation on the fly

Custom Validation on the fly

crush123crush123 Posts: 417Questions: 126Answers: 18

I want to create a table as a global reference table, storing variables to use site wide.
The variables, in principle, could be integers, boolean, strings, or dates.
Rather than create different tables for different data types, I would like to use a single table, and depending on the entry, determine the data type validation.
I have in mind, a table structure something like this..

ID, OptionType, OptionName, OptionValue

Where OptionType would be an integer value to determine data type.

When the OptionValue is edited, I want the editor to use the OptionType to determine the validation to apply.
I am guessing I want to use some sort of custom validation, as per https://editor.datatables.net/manual/php/validation#Validation-function, but can’t work out how to approach it

This question has an accepted answers - jump to answer

Answers

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    Think I'm getting there.

    I was struggling to get values back from the data object as my field instances contain the table name as well as the field name.

    The syntax I needed to check for my OptionType value from $data input is $data['tablename']['OptionType'];

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    You want to use a server-side event in order to add validation based on the user input data. Specifically take a look at preCreate and preEdit.

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    cheers Allan, will look at that.

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited October 2016

    Thanks Allan, this was definitely the best approach.

    I added some min and max value fields to my table to limit input for my numeric and boolean fields, (which in the snippet below are dealt with in the same way).

    (The editor in my case only allows editing, there is no option to create or delete a row)

    For anyone else looking at this, here is my preEdit event

    ->on( 'preEdit', function ( $editor, $id, $values ) {
    
        $opttype = $values['refglobaloptions']['OptionType'];
        $optminvalue = $values['refglobaloptions']['MinVal'];
        $optmaxvalue = $values['refglobaloptions']['MaxVal']
    
        switch ($opttype) {
            case 1: //Numeric
                $editor
                ->field( 'refglobaloptions.OptionValue' )
                ->validator( 'Validate::minMaxNum', array(
                  'min' => $optminvalue,
                  'max' => $optmaxvalue,
                  'message' => 'Please enter a number between '.$optminvalue.' and '.$optmaxvalue
                  ));
                break;
            case 2: //Boolean
                $editor
                ->field( 'refglobaloptions.OptionValue' )
                ->validator( 'Validate::minMaxNum', array(
                  'min' => $optminvalue,
                  'max' => $optmaxvalue,
                  'message' => 'Please enter a number between '.$optminvalue.' and '.$optmaxvalue
                  ));
                break;
            case 3: //String
                $editor
                ->field( 'refglobaloptions.OptionValue' )
                ->validator( 'Validate::required', array(
                  'message' => 'Please enter a value for this option'
                  ));
                break;
    
        }
    
    })
    
This discussion has been closed.