Data Validation not working properly?

Data Validation not working properly?

raspiraspi Posts: 18Questions: 3Answers: 0
edited June 2014 in Editor

I am struggling with a date field in my table - having mutliple issues:

a)
I do not get an error message when providing a wrong date format. I get either a wrong result or an empty entry into the database.

b)
And even worse I see still an entry in the table which will be removed after the page has been refreshed.

c)
And additionally - the script seems to be more intelligent as it should, because when entering a wrong Month it simply adds the number of month to the date. (see below)
Submitted data: data[start_date]=2013-28-13
Server response: "start_date": "2015-04-13"

d)
the DatePicker shows up after the page has refreshed but when I confirm or cancel the lightbox and wnat to edit another field the DatePicker does not show up again until refresh.

My primary questions are:
- Why not getting an error message like I got one for the "required" field? How else should the user figure out that the format is wrong?

my code looks like ( extracts ):

EDITOR INITIALISATION

                 editor = new $.fn.dataTable.Editor( {
                            ajax: "./veranstaltungen_edit.php",
                            table: "#example",
                            fields: [ {
                                    label: "Anmeldeschluss:",
                                    name: "3",
                                    type: "date",
                                    dateImage: "./images/calender.png",                                   
                                    def:        function () { return new Date(); },
                                    dateFormat: $.datepicker.ISO_8601
                                }
                            ]
                        } );           
                        // Activate the bubble editor on dblclick of a table cell
                        $("#example").on( "dblclick", "tbody td", function (e) {
                            var index = $(this).index();
                            if ( index === 3 ){
                                editor.bubble( this, {
                                    title: "Anmeldeschluss",
                                    message: "Ab diesem Datum wird diese Veranstaltung nicht mehr in der Online-Anmeldung angezeigt.<hr/>"
                                });
                        } );
                    '); 

VALIDATOR:
// Build our Editor instance and process the data coming from _POST

Editor::inst( $db, 'ski_veranstaltungen' )
    ->fields(
        Field::inst( 'typ', 0 )->validator( 'Validate::required' ),
        Field::inst( 'veranstaltung', 1 )->validator( 'Validate::required' ),
        Field::inst( 'maxTeilnehmer', 2 )->validator( 'Validate::numeric' ),       
        Field::inst( 'gueltig_bis', 3 )           
            ->validator( 'Validate::dateFormat', array(
                'format'  => Format::DATE_ISO_8601,
                'message' => "Bitte das Datum iM Format JJJJ-MM-TT eingeben"
            ) )
            ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
            ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 ),
        Field::inst( 'zusatz', 4 ),
        Field::inst( 'minHelfer', 5 )->validator( 'Validate::numeric' ),
        Field::inst( 'anzHelfer', 6 )->validator( 'Validate::numeric' )
    )
    ->process( $_POST )
    ->json();
  • Any idea of the datepicker issue?

thanks
Ralf

This question has accepted answers - jump to:

Answers

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

    Hi Ralf,

    When you say "the wrong format" - what data are you providing to the server? When using jQuery UI's datePicker it doesn't allow characters to be entered, so it should always in the form y-m-d - although as you have seen, from the 2013-28-13 jQuery UI does allow that, the PHP is smart enough when creating a date to correct that.

    Like you, I think this is in error - the date entered should be strictly valid rather than "smart". I've just been looking into it, and I think I've uncovered a relative and significant issue in Editor that I'll need to think about a little before fixing (sleep on it). The question I'm having is if the data should be validated and then formatted, or formatted and then validated...

    Once that its resolved I've send you the update to fix this issue as well.

    Allan

  • raspiraspi Posts: 18Questions: 3Answers: 0

    Yes, I, too, think that a software should not try to be smarter than the user.
    I would be alarmed when I type in a date which is not valid instead of getting anything calculated which -in general - might not be what I wanted.

    And what I indeed expect is, that I receive a message, when anything goes wrong. Currently I do not get anything as response. It simply keeps the old data when I provide anything wrong (like 002012-34-21).

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

    I've made the required changes to Editor and it will be included in v1.3.2 which I plan to release next week. Until then, the changes needed are actually quite small:

    php/Editor/Field.php

    In the function validate change:

    $val = $this->val( 'set', $data );
    

    to be:

    $val = $this->_readProp( $this->name(), $data );
    

    php/Editor/Validate.php

    Replace the whole dateFormat function with:

        public static function dateFormat( $val, $data, $opts, $host ) {
            $cfg = Validate::_extend( $opts, 'format', array(
                'message' => "Date is not in the expected format"
            ) );
    
            $common = Validate::_common( $val, $cfg );
            if ( $common !== null ) {
                return $common;
            }
    
            $date = \DateTime::createFromFormat( $cfg['format'], $val) ;
    
            return $date && $date->format( $cfg['format'] ) == $val ?
                true :
                $cfg['message'];
        }
    

    And that should do it! Let me know how you get on with it.

    Regards,
    Allan

  • raspiraspi Posts: 18Questions: 3Answers: 0
    edited June 2014

    Great Allan,
    now this works just fine - as I expected the function to react. A format otehr than yyyy-mm-dd is no longer valid.
    Thanks.

    But please allow me one more question to understand the architecture better.
    Before the validate of the Field settings did not react:

            Field::inst( 'gueltig_bis', 3 )   ->validator( 'Validate::required' )        
                ->validator( 'Validate::dateFormat', array(
                    'format'  => Format::DATE_ISO_8601,
                    'message' => "Bitte das Datum im Format JJJJ-MM-TT eingeben oder auswählen"
                ) )
    

    Now it does. And the error message shown is what is written here.
    You additionally changed the dateFormat function too. But this message does not show up. So why this change? Or better: How does it work now?

    Thank you
    Ralf

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

    You've got two validator methods there, which isn't valid in Editor 1.3-. In 1.4+ you will be able to perform multiple validations, as that is a new feature I'm going to introduce, but it isn't supported yet. So what you need to do is combine the two:

    Field::inst( 'gueltig_bis', 3 )   
        ->validator( 'Validate::dateFormat', array(
            'required'  => true,
            'format'  => Format::DATE_ISO_8601,
            'message' => "Bitte das Datum im Format JJJJ-MM-TT eingeben oder auswählen"
        ) )
    

    There is more about the validation options here: http://editor.datatables.net/manual/php/validation .

    Allan

  • raspiraspi Posts: 18Questions: 3Answers: 0
    edited June 2014

    Oh, yes. I see and changed it.
    But to be honest. This did not cause a problem it reacts in the same way. probably the "required" did not ifr, but as the datepicker is on I cannot leave the field empty as I get the error regarding the format.

    But my question focessed on the message in the dateFormat function.
    When is the message shown? Because currently I just see the message of the field definition. (maybe when no date picker is chosen?)

    public static function dateFormat( $val, $data, $opts, $host ) {
        $cfg = Validate::_extend( $opts, 'format', array(
            'message' => "Date is not in the expected format"
        ) );
    

    But anyway.
    Not the application works as expected.

    Thanks for your help

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

    Sorry to hear this still isn't working quire right. Just to confirm, is the problem that your custom error message Bitte das Datum... is not showing up? I've just tried your code in my local setup and it seems to work as expected.

    Could you show me the full PHP that you are using?

    Allan

  • raspiraspi Posts: 18Questions: 3Answers: 0
    edited June 2014

    sorry- wrong thread

  • raspiraspi Posts: 18Questions: 3Answers: 0

    No, there is not fault. Everything now works fine.
    I just was wondering about the two "layers". Layer one in the field definition, layer two in the dateFormat function itself.
    Yes, I get my custom error message. Is the one defined in the function thrown when I do not have a custom one? Then I would understand.

    Anyway, in my case. I do get the correct result.
    Thanks for all.

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

    Is the one defined in the function thrown when I do not have a custom one?

    Exactly that!

    Good to hear you have it working now!

    Allan

This discussion has been closed.