-
Notifications
You must be signed in to change notification settings - Fork 652
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
'date' as input field type produces 'Invalid type, expected date' #709
Comments
Sounds like a validation error? Ie, your decorator is creating a date input field that is throwing a date as a string on the model, but validation is expecting an actual js Date object on the model? Make sure whatever date input being generated for that schema object is setup to add a date object on the model. Generally date inputs will be of type string with format of datetime or something along those lines. This will allow validation to not throw that error when the string of the date gets applied to the model. |
The reason why i do not want to use input of type
Now the date from the input field is successfully saved in the model. I do not know if it is the final solution that I will use, but is is working for now. |
How I fixed it was: // Date values
if ( schema.type === 'string' && schema.format === 'date' ) {
if ( value === null ) {
value = undefined;
} else {
if ( typeof value.toISOString === 'function' ) {
value = value.toISOString();
}
}
} |
@brianpkelley This fixes the issue, but introduces an issue with the validation. After you clear the field the validation sticks saying it's successful. I was able to resolve the validation remaining green by adding this:
to line 2769 in the angular-schema-form/dist/schema-form.js of the release from the master branch. Line 2769 starts with the following code for checking if the result is valid:
|
json-schema-form/angular-schema-form-material#18 (comment) |
@brianpkelley I'm having a hard time thinking it's best to solve this issue by adding a tv4 format and not adding a solution directly into ASF. It seems @Anthropic really favors your choice. But it seems like creating a decorator to resolve an issue isn't the right path, for something that is provided by default. However, because I am using a decorator for my date fields ANYWAYS, I have chosen to implement your changes. While this does properly validate the date, with the exception of the validation message not applying the correct message as stated by @brianpkelley's solution in the other thread ema-form/angular-schema-form-material#18 (comment). It is expecting a valid of type object even if the field is not required and shows the field as invalid. Here is my implementation:
Above, I used @brianpkelley's solution. While adding my solution to line 2890 of Angular-schema-form.js Or rather line 41 of src/directives/schema-validate.directive.js
This only solves one of the many notable issues, you can see while playing around with validation on a date field. For instance, you can even have a date required, and not provide a value and still have it validate successfully. |
@krptodr I would love to come up with a solution to this that works for everyone, dates are a pain, I assume that's why Textalk made the original datepicker add-on for the library as a separate item. I would like to add a stable datepicker to the bootstrap repo and whatever changes are required here, ideally if there was a main datepicker as part of bootstrap (I don't use bootstrap much so I have never checked) it would be the preferred choice and any behaviour here would have to cater for that. It just isn't high on my TODO list so it wouldn't be for a while unless someone wants to make a PR with one. |
Since json schema draft 4 does not support custom types AND has a built in string date-time format (ISO8601), maybe the best way to tackle this is to force conversion of date objects to strings prior to validation. So, if a jsonSchema is defined as this: "mydate": {
"id": "/properties/mydate",
"type": "string",
"format": "date-time"
} angular-schema-form could coerce the date object into a string, somewhere prior to calling tv4 validation: if (value instanceof Date) {
value = value.toISOString();
} |
First: Thanks for this really cool and easy to use framework!
I would like to use a input field of type 'date' to support browser specific date picker implementations, but it always shows the error 'Invalid type, expected date' and i do not know where this comes from. If I insert an date input field manually and bind it with
ng-model
to a date object, it seems no problem.I use the following schema
form
and configure the
schemaFormProvider
like this:Could you tell me where I am going wrong?
The text was updated successfully, but these errors were encountered: