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

Missing option to set a default value using a template / templates render fields as read-only #1482

Open
simontaurus opened this issue Feb 4, 2024 · 2 comments
Assignees

Comments

@simontaurus
Copy link

General information

  • json-editor version: 2.14.0

Expected behavior

#1141 describes a way to set the current date and time as a default for a field by using a template. Usage of the default value in combination would be even more intuitive.

Actual behavior

setting any template renders a field as read-only. Templates in default values do not work (see #142 (comment))

Steps to reproduce the behavior

Direct link to example: playground

{
  "title": "Person",
  "type": "object",
  "required": [
    "date"
  ],
  "properties": {
    "date": {
      "type": "string",
      "format": "date",
      "template": "2022-02-02",
      "options": {
        "flatpickr": {}
      }
    }
  }
}
@simontaurus simontaurus changed the title Missing option to set a default value using a template / template render fields as read-only Missing option to set a default value using a template / templates render fields as read-only Feb 4, 2024
@germanbisurgi
Copy link
Collaborator

Hi @simontaurus,

for default values there is the default schema property

{
  "type": "string",
  "format": "date",
  "default": "2022-02-02",
  "template": "your_callback"
}

Editors with template are read only because they are "generated" and very often overridden by the template expression / callback.

@simontaurus
Copy link
Author

Okay, so currently default is the way to set a static value until the user overrides it. template will enforce a dynamical value without any option for the user to change it (read-only). For usecases like the receiving_data of a shipment I'm looking for a way in between to set the current date (now) as default but allow the user to change it to any other date (e. g. yesterday).

Setting a now callback as template works but this renders the field as readonly.

{
  "type": "string",
  "format": "date",
  "template": "now"
}
JSONEditor.defaults.callbacks = {
	'now': (jseditor_editor, e) => {
		return new Date(Date.now()).toISOString();
	}
}
//or
Handlebars.registerHelper('now', function (options) {
	return new Date(Date.now()).toISOString();
})

Setting now as default keeps the field editable, but now is not interpreted as template.

{
  "type": "string",
  "format": "date",
  "default": "now",
}

A solution could be to allow templates in default values (see #142 (comment)), so that "default": "now" or "default": "{{now}}" is resolved to the current datetime (e.g. "default": "2024-02-04T04:31:08.050Z") at the initialisation of the editor but the user could still set another datetime.

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