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

Set initial date and don't use the src from release by the moment. #113

Open
juamar opened this issue Jan 10, 2018 · 6 comments
Open

Set initial date and don't use the src from release by the moment. #113

juamar opened this issue Jan 10, 2018 · 6 comments

Comments

@juamar
Copy link

juamar commented Jan 10, 2018

Hi everyone!

How do i set a initial date in a string with date format?

I have tried this in the demo, but no luck, even when setting a proper date for input type date (YYYY-MM-DD) in the initial data like this:

{ date: "2018-1-13" }

Also not working with:

{ date: "2018-01-13T00:00:00" }

With time, it works like this:

{ time: "14:00" }

and schema (minimalist example):

{ "$schema": "http://json-schema.org/draft-03/schema#", "type": "object", "properties": { "time": { "type": "string", "format": "time" } } }

If there is no support, I can work on it.

Thanks in advance!

@juamar
Copy link
Author

juamar commented Jan 10, 2018

I am looking for the lines where initial time is setted if initial data is given. Can someone indicate me where they are?

Thanks!

@juamar
Copy link
Author

juamar commented Jan 10, 2018

I have detected that, at some point when loading form, value for data input is wiped out after setted. I am looking for it. Help appreciated ;D

@juamar
Copy link
Author

juamar commented Jan 10, 2018

When setting BrutusinForms.bootstrap.addFormatDecorator("date", "date") before BrutusinForms.create(schema), when form is loaded, form will override the value for the input with date decorator. I will study it to learn how to keep our decorator with our date picker.

@juamar juamar changed the title Set initial date Set initial date and don't use the src from release by the moment. Jan 10, 2018
@juamar
Copy link
Author

juamar commented Jan 10, 2018

Ok, sorry, i was using the brutusin-json-forms.js from the release, which is older than the one in the last commit on master branch. The one in the master branch supports by default the time and date time without calling any decorator. So i am still having the same problem as before but, at least, i learned something:

By the moment, PLEASE DON'T USE THE SRC FROM THE RELEASE!

@juamar
Copy link
Author

juamar commented Jan 10, 2018

Eureka, I 've done it! i have modified the lines regarding the value set in brutusin-json-forms.js.

if (value !== null && typeof value !== "undefined") {
                if (s.format === "date")
                {
                    try
                    {
                        var date = new Date(value);
                        //console.log(date);

                        var year = date.getFullYear(); // This will work as long as year is 1000 or bigger.
                        var month = date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
                        var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();

                        input.value = year + "-" + month + "-" + day;
                        //console.log(input.value);
                    }
                    catch(err)
                    {
                        console.warn("The date is not valid");
                        input.value = value;
                    }
                }
                else
                {
                    input.value = value;
                }

                // readOnly?
                if (s.readOnly)
                    input.disabled = true;

            }

(I did my best with the formatting, but i don't know what is going on).

It will be nice to commit it to master. I tried to make a pull request, but it seems i got to create a new branch, but the interface here is not letting me (maybe using git on a local machine?). Maybe if you help me out, we can get it done.

Thanks!

@jasonchuah4
Copy link

Actually you can use the "default" field. Eg:

{
  "$schema": "http://json-schema.org/draft-03/schema#",
  "type": "object",
  "properties": {
    "date": {
      "type": "string",
      "format": "date",
      "default": "2023-12-03",
      "description": "date"
    }
  }
}

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

2 participants