Custom Validation on the fly
Custom Validation on the fly
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
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'];
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
andpreEdit
.Allan
cheers Allan, will look at that.
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