question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Material theme input fields are broken with custom format validation

See original GitHub issue

General information

  • json-editor version: 1.4.0-beta.0
  • materialize/1.0.0/css/materialize.min.css
  • materialize/1.0.0/js/materialize.min.js

Expected behavior

Input fields (string) are rendered as without format or built-in validation (e.g. minLength)

Actual behavior

Input fields are broken, Select generated from enums seems ok

Steps to reproduce the behavior

Attach custom validator with JS and use Material theme

Direct LINK

{
  "type": "object",
  "title": "Car",
  "properties": {
    "make": {
      "type": "string",
      "enum": [
        "Toyota",
        "BMW"
      ],
      "format": "connectionString"
    },
    "model_custom": {
      "type": "string",
      "format": "connectionString"
    },
    "model": {
      "type": "string",
      "minLength": 1
    }
  }
}
JSONEditor.defaults.custom_validators.push(function (schema, value, path) {
  var errors = [];
  if (schema.format === "connectionString") {
    if (!/^([^=;]+=[^=;]+)(;[^=;]+=[^=]+)*$/.test(value)) {
      errors.push({
        path: path,
        property: 'format',
        message: 'Connection string must be in the format "Server={value};Database={value};Trusted_Connection=yes;"'
      });
    }
  }
  return errors;
});

var editor = new JSONEditor(element, {
  [...]
  "theme": "materialize",
  [...]
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
pmk65commented, Aug 5, 2019

@lukzas Did the update I committed, solve your problem?

1reaction
pmk65commented, Aug 4, 2019

I think I have spotted the problem. In src/editors.string.js, the type is defined like this:

    // Specific format
    if(this.format) {
      // Text Area
      if(this.format === 'textarea') {
        this.input_type = 'textarea';
        this.input = this.theme.getTextareaInput();
      }
      // Range Input
      else if(this.format === 'range') {
        this.input_type = 'range';
        var min = this.schema.minimum || 0;
        var max = this.schema.maximum || Math.max(100,min+1);
        var step = 1;
        if(this.schema.multipleOf) {
          if(min%this.schema.multipleOf) min = Math.ceil(min/this.schema.multipleOf)*this.schema.multipleOf;
          if(max%this.schema.multipleOf) max = Math.floor(max/this.schema.multipleOf)*this.schema.multipleOf;
          step = this.schema.multipleOf;
        }

        this.input = this.theme.getRangeInput(min,max,step);
      }
      // HTML5 Input type
      else {
        this.input_type = this.format;
        this.input = this.theme.getFormInputField(this.input_type);
      }
    }
    // Normal text input
    else {
      this.input_type = 'text';
      this.input = this.theme.getFormInputField(this.input_type);
    }

And the “HTML5 Input type” sets the type to the format. There should probably be a check for valid HTML5 input types here and a fallback to “text” type. I have committed an updated src/editors/string.js to the 2.x branch. Now it checks for valid HTML5 input types and fallback to text type

Read more comments on GitHub >

github_iconTop Results From Across the Web

Errors - Patterns - Material Design
Errors occur when an app fails to complete an expected action. Some examples of errors include: When user input is not understood; An...
Read more >
Form Validation in React Material UI - YouTube
Form Validation in React Material UI.In this video, we discuss how to implement validation in React Material UI Form and how to insert...
Read more >
Client-side form validation - Learn web development | MDN
Built-in form validation has better performance than JavaScript, but it is not as customizable as JavaScript validation.
Read more >
Text Inputs - Materialize
The validate class leverages HTML5 validation and will add a valid and invalid class accordingly. If you don't want the Green and Red...
Read more >
Bootstrap Input fields - examples & tutorial
Both elements are wrapped in .form-outline class which provides a material design look. Example label.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found