json-editor icon indicating copy to clipboard operation
json-editor copied to clipboard

[QUESTION] how to prevent submission of empty value field

Open PydPiper opened this issue 3 years ago • 7 comments

JSON Schema is allowing the submission of POST request even though the form value is empty. Here is what i tried:

{
"type": "object",
"properties": {"field1": {
  "type": "string",
  "pattern": "[A-Za-z]+",
},
"required": ["field1"]
}

live demo link: LINK

The patter requires at least 1 character to be entered (when you click in the field then off of it the error message does come up stating that pattern is not matched), however the form is allowed to be submitted even though it has an error in it through POST.

I also tried to intercept the button with

          console.log(editor.length)
          if (editor.length == 0) {
               form.submit()
          } else {
               alert("Invalid Form, please double check inputs and resubmit")
          }

Any suggestions as to how to prevent the submission of empty values since regex does not seem to work.

but it still shows me that there are 0 errors

PydPiper avatar Jan 31 '22 20:01 PydPiper

The editor just tells you that there are validation problems. It's up to you to allow or not the form submition. Also Backend validation should be a must.

germanbisurgi avatar Feb 03 '22 07:02 germanbisurgi

Does anyone else have any suggestions? I guess i don’t see the point if required field then if the form doesnt even pickup that a field was submitted empty. I can do backend checks but i thought i was missing some nice capability in json schemas here

PydPiper avatar Feb 03 '22 09:02 PydPiper

Hi :) It's a problem for me as well.

It's up to you to allow or not the form submition.

I think preventing submission of invalid forms should be in scope for this project. It is more user-friendly to disallow accidentally sending invalid forms.

json-editor appears to generate input tags with pattern attributes, so preventing submission of invalid forms could be achieved by adding the required attribute to the input elements. https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern

@PydPiper, this could be done using JavaScript as a workaround.

neoq avatar Apr 10 '24 09:04 neoq

How could the editor prevent a form submit? It has nothing to do with a form, actually you even have to add the hidden text area with the JSON manually.

As @germanbisurgi mentioned you can only check for validation error yourself.

schmunk42 avatar Apr 10 '24 10:04 schmunk42

JSONEditor is use to edit json and it can be used in mutliple scenarios other than form submission. That is why it does not generate submit buttons. The submission part is handled by the developer. For example:

yourForm.on('submit', (event) => {
  event.preventDefault()

  if (editor.getErrors().length === 0) {
      this.submit()
  } else {
      alert("Invalid Form, please double check inputs and resubmit")
  }
})

germanbisurgi avatar Apr 10 '24 12:04 germanbisurgi

I understand, but using the HTML required attribute results in nicer UX than using alert. When inputs have a pattern and are required, and you try to submit the form, the browser scrolls to the first offending element and tells you "Please fill out this field." or "Please match the requested format." This still results in a click-event but prevents form submission automatically.

This is from Firefox: image

It would be great if json-editor could generate input elements with the required attribute if they have a constraint, but I guess that is a slightly different issue. Maybe I get around to submitting a PR of what I mean :)

neoq avatar Apr 15 '24 15:04 neoq

The alert was just an example. It could be done in multiple ways. Add this to your required property if you want to leverage the browser behaviour too:

"options": {
  "inputAttributes": {
    "required": ""
  }
}

germanbisurgi avatar Apr 16 '24 08:04 germanbisurgi