jdorn / json-editor

JSON Schema Based Editor
MIT License
5.81k stars 1.08k forks source link

Required fields #158

Closed bmcbride closed 10 years ago

bmcbride commented 10 years ago

I'm having some issues figuring out how to make a field required. I've tried the following, which adds "required": ["comment"] to the object after defining the properties, but that doesn't seem to work. Any guidance would be most appreciated. Thanks!

jdorn commented 10 years ago

Do you have a full example schema?

bmcbride commented 10 years ago

See the link posted above. Thanks.

jdorn commented 10 years ago

It's working correctly. In JSON schema, required just means the property has to exist. To require non-empty values, set minLength to 1 for that property.

bmcbride commented 10 years ago

Okay, got it. Thanks again.

brian-learningpool commented 10 years ago

I know this issue has been closed, but is there scope for an enhancement here? The validation error "Value must be at least 1 characters long." isn't as user friendly as something like "A value is required here".

Also, a lot of web sites usually have a visual indicator for required fields, e.g. * after the form control, which speeds up form completion for the user. I know the required property is being used for indicating that the property must exist -- maybe mustExist would have been a better name -- but would a new property valueRequired make sense, which would give a more specific error that the control has been left blank?

Great work on the editor, by the way.

bmcbride commented 10 years ago

:+1: for requirement enhancement.

jdorn commented 10 years ago

I agree the property names are confusing, but required and minLength come straight from JSON Schema, so I can't change that. I can definitely change the error message though and look into adding an asterisk.

jdorn commented 10 years ago

I changed the error message for minLength: 1 to Value required.

I want to spend some time thinking about the best way to handle the asterisks before implementing that.

brian-learningpool commented 10 years ago

Awesome -- thank you.

gnom7 commented 6 years ago

Hello, @jdorn

I want to spend some time thinking about the best way to handle the asterisks before implementing that.

Has it been implemented?

schmunk42 commented 6 years ago

@gnom7 https://github.com/jdorn/json-editor#deprecation-notice

https://github.com/json-editor/json-editor/issues/44

gnom7 commented 6 years ago

Hello, @schmunk42, I assume since you referenced this in json-editor/json-editor#44 we can handle the issue here. Also I have one somehow related question - according to the README I can specify my own CSS styles, but I cannot find the way to specify CSS classes for elements in json schema (if I could do it, I'd just use .form-group.required class from bootstrap to handle the first issue)

If you want to specify your own styles with CSS, you can use barebones, which includes almost no classes or inline styles.

schmunk42 commented 6 years ago

I am not too familiar with the code, I'd recommend creating a new issue at https://github.com/json-editor/json-editor/issues

germanbisurgi commented 6 years ago

The documentation says you can use custom styles but does not say you can use custom classes. When you using barebones JSON editor it uses almost no classes that can override your styles.

I looked in the source code looking for a feature that does what you want to do but I did not find anything. A solution could be, to extend the theme class and create a new theme for JSON Editor.

You can look in the original repository wiki:

https://github.com/jdorn/json-editor/wiki#theme-srcthemejs

gnom7 commented 6 years ago

@germanbisurgi, thank you for the suggestion, but it seems that I can't customize form elements this way, only DOM ones (like tables etc.) and it might happen that I'd like to pull one table to the right and another to the left in my form depending on the element which is rendered, or even something more sophisticated, so it would be nice if we had feature to add classes to json form elements, thanks for reply