Compare two datetime fields

Compare two datetime fields

AlchetecAlchetec Posts: 14Questions: 5Answers: 0

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

Answers

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

    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

  • AlchetecAlchetec Posts: 14Questions: 5Answers: 0

    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.

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

    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:

    Parameter 2: $data - The whole data set that was submitted (useful for multiple field validation)

    Allan

  • AlchetecAlchetec Posts: 14Questions: 5Answers: 0
    edited February 2016

    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!

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

    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

This discussion has been closed.