Compare two datetime fields
Compare two datetime fields
I have two datetime field: 'from' and 'to'. I want to be sure that 'to' is greater than the 'from' date whenever a row is added or updates. I am using the:
editor.on( 'preSubmit', function ( e, o, action )
function but cannot see how to compare two datetime fields.
Field::inst( 'PermissionToEnter.from' )
->validator( 'Validate::dateFormat', array( 'format'=>'m-d-Y' ) )
->getFormatter( 'Format::date_sql_to_format', 'm-d-Y' )
->setFormatter( 'Format::date_format_to_sql', 'm-d-Y' ),
Field::inst( 'PermissionToEnter.to' )
->validator( 'Validate::dateFormat', array( 'format'=>'m-d-Y' ) )
->getFormatter( 'Format::date_sql_to_format', 'm-d-Y' )
->setFormatter( 'Format::date_format_to_sql', 'm-d-Y' ),
Thanks!
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
I would suggest that you use server-side validation with a custom validator. Its trivial for an end user to bypass client-side validation!
On the server-side you could use
strtotime
to convert from the string representation to something that can be directly compared.Allan
Sorry to ask such a basic question, but how do I compare two fields in the validate function. The one is $val, how do I get the other (in my case its 'allow_to').
Thanks.
You would use a custom validator (link above) and simply check the values passed in - the second parameter passed in contains all the data for the row being submitted:
Allan
I read that but what is the structure of $data? How do I get what I want? It would be nice to have it in the documentation; if it is, I am finding the documentation hard to use because of all the clutter from legacy and such. Thanks!
Its simply an associative array of the fields submitted. So if you submitted a field with the name
first_name
you would use$data['first_name']
to get that value. Likewise for the other fields that are submitted.Since you have nested data you might use something like:
$data['PermissionToEnter']['to']
(i.e. it is basically a 2D array).Allan