Unique date field with formatter

Unique date field with formatter

empasaempasa Posts: 1Questions: 1Answers: 0
edited March 2018 in Editor

Hi, I would like to make my date-field unique, but I'm using set- and get-formatter (server-side) with the editor.

Client-Side:

// ... 
editor = new $.fn.dataTable.Editor( { 
    ajax: "scripts/editor.php", 
    table: "#holidays_overview", 
    fields: [{ 
            label: "Datum", 
            name: "appointments.day", 
            type: "date",
            opts: {
                language: "de",
                format: "dd.mm.yyyy",
                showOn: "focus",
                todayHighlight: "true"
            }
        },{
            label: "Feiertag",
            name: "appointments.event"
        }
    ],
    i18n: DTE_i18n
// ...
});

Server-Side:

// ...
Field::inst( 'appointments.day' )
->validator( Validate::notEmpty( ValidateOptions::inst()
    ->message( 'Bitte geben Sie ein Datum ein.' ) )
)
->getFormatter( Format::dateSqlToFormat( 'd.m.Y' ) )
->setFormatter( function( $val, $data, $opts ) {
    return date( 'Y-m-d', strtotime( $val ));
} )
->validator( Validate::unique( ValidateOptions::inst()
    ->message( 'Das Datum ist bereits vergeben.' ) )
)
// ...

But currently I can still create more entries with the same date.
I guess it's because the unique-method compares the database value in format "YYYY-MM-DD" and client-side value in format "DD.MM.YYYY".

Is it possible to set the formatted values to unique? Like include an formatter into the unique method?
Thank you in advance.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,893Questions: 1Answers: 10,145 Site admin
    Answer ✓

    You are correct - the validator runs before the formatter. What you would need to do is use a custom validator which will do the conversion as well as checking against the database to see if the formatted value is unique I'm afraid.

    Allan

This discussion has been closed.