jsonform / jsonform

Build forms from JSON Schema. Easily template-able. Compatible with Bootstrap 3 out of the box.
https://jsonform.github.io/jsonform/playground/index.html
MIT License
2.72k stars 553 forks source link

Empty required textarea #402

Closed alcabon closed 1 year ago

alcabon commented 2 years ago

An empty textarea has always instance.getType() === 'string' in jsv.js in my case (?) and the required option doesn't work because it is not "undefined" like an empty input string.

Did you notice the same thing?

I am building a tool based on jsonform.js and that works well excepted for the empty required textarea and the validator of jsv.js that I used.

          myitem: {
              type: "textarea",
              title: "Item {{idx}}",
              required: true,
              default:""
            },

https://github.com/jsonform/jsonform/blob/master/deps/opt/jsv.js

My quick fix for empty required textareas in json forms for the validator of jsv.js:

"required" : { "type" : "boolean", "default" : false,

        "parser" : function (instance, self) {
            return !!instance.getValue();
        },

        "validator" : function (instance, schema, self, report, parent, parentSchema, name) {

            if ( ( instance.getType() === "undefined" 
            || instance.getType() === "string" && instance.getValue().trim() === ''
             )
            && schema.getAttribute("required")) {
                report.addError(instance, schema, "required", "Property is required", true);
            }
        }
    },
tchapi commented 2 years ago

Hi @alcabon So this is more a bug with jsv ... unfortunately it is not maintained anymore so I guess your fix is the right solution

alcabon commented 2 years ago

Hi @tchapi

https://github.com/garycourt/JSV/blob/master/lib/json-schema-draft-03.js seems the last version indeed (10 years ago).

You are using your own fork based on this draft version and I have created my own fork too.

Thanks for your help

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.