brutusin / json-forms

JSON Schema to HTML form generator, supporting dynamic subschemas (on the fly resolution). Extensible and customizable library with zero dependencies. Bootstrap add-ons provided
http://brutusin.org/json-forms
Apache License 2.0
606 stars 168 forks source link

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

Open juamar opened 6 years ago

juamar commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 1 year ago

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"
    }
  }
}