Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datepicker always invalid #18

Open
juanmav opened this issue May 24, 2016 · 6 comments
Open

Datepicker always invalid #18

juanmav opened this issue May 24, 2016 · 6 comments

Comments

@juanmav
Copy link

juanmav commented May 24, 2016

The datepicker is not working. When the date is selected from the datepicker the form always stays as invalid. I tried it using angular-material 1.0.7.

I also clone the repo and tried the "simple" example that fails too.

Any idea what can be happening?

Thanks
JM

@brianpkelley
Copy link

brianpkelley commented Aug 2, 2016

md-datepicker returns a Date object, and angular-schema-form wants a string. Without calling one of the toString methods on it, it won't matter what we pass to it.

How I fixed it was:
Add this conditional to the /angular-schema-form/src/services/validator.js around line 39

    // Date values
    if ( schema.type === 'string' && schema.format === 'date' ) {
        if ( value === null ) {
            value = undefined;
        } else {
            if ( typeof value.toISOString === 'function' ) {
                value = value.toISOString();
            }
        }
    }

@juanmav
Copy link
Author

juanmav commented Aug 4, 2016

@brianpkelley Great! Thanks! I will try it!

@Anthropic
Copy link
Member

@brianpkelley I'm on holiday this week but I will try to add this change next week, thanks for taking the time to provide the info! :)

@brianpkelley
Copy link

In testing found a bug in the date check, I've updated my comment above to reflect the change.

@brianpkelley
Copy link

brianpkelley commented Aug 13, 2016

The fix above only works dates outside of arrays (i'd say average operation) if they're inside of an array type tv4 is handling the validation and we can't convert the angular <input type="date" directive which is passing back a Date object.

To fix this, I hackishly created another date format/builder with "type": "object" and "format": "date" and applied the tv4 custom format to validate the date. This way tv4 is looking for an object (check) and the custom format validates it.

    // ISO Format - 2016-08-02T17:03:18.608Z - new Date().toISOString()
    var dateFormat = /^[0-9]{4,}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]+|)(?:[+-][0-9]{2}:?(?:[0-9]{2}|)|Z)$/;
    // Standard Format - Tue Aug 02 2016 12:03:59 GMT-0500 (CDT) - new Date().toString()
    var mdDateFormat = /^(:?[A-Z][a-z]{2}\s){2}\d{1,2}\s\d{4}\s(:?\d{2}\:?){3}\s[A-Z]{3}\-\d{4}\s\([A-Z]{3}\)$/;

    var formats = {
        date: function (value) {
            if ( value && typeof value !== 'string' && value.toISOString ) {
                value = value.toISOString() || '';
            }

            if (dateFormat.test(value) || mdDateFormat.test(value) ) {
                return null;
            }

            return 'A valid date expected';
        }
    };
    tv4.addFormat( 'date', formats.date );

    function dateObjectDefault(name, schema, options) {
      if (schema.type === 'object' && (schema.format === 'date' || schema.format === 'date-time')) {
        var f = schemaFormProvider.stdFormObj(name, schema, options);
        f.key  = options.path;
        f.type = 'date';
        options.lookup[sfPathProvider.stringify(options.path)] = f;
        return f;
      }
    };

    schemaFormProvider.defaults.object.unshift(dateObjectDefault);

@hugoalexmartins
Copy link

@brianpkelley hey I am facing a same problem with datepicker.

After place the code above in material-decorator.js, build, and also changed the type object. I did well ?

Because if a use the datepicker and chose a date seems to be fine, but if a have the field required and don't chose any date I just received "Invalid type, expected object", so do I need to change the validator.js also ?

I appreciate any help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants