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

Validation #216

Merged
merged 10 commits into from Jan 13, 2017
Merged

Validation #216

merged 10 commits into from Jan 13, 2017

Conversation

cassiozen
Copy link

@cassiozen cassiozen commented Jan 12, 2017

- Summary

Adds validation to widgets. Available validations:

  • Presence: By default all widgets are required, unless specified in the config. Example:
    - {label: "Subtitle", name: "subtitle", widget: "string", required: false}

  • Pattern: Field configuration can specify a regex pattern with the appropriate error message. Example:
    - {label: "Title", name: "title", widget: "string", pattern: [".{10,}", "Should have more than 10 characters"] }

The widget control can optionally implement an isValid method to perform custom validations, in addition to presence and pattern. The isValid method will be automatically called, and it can return either a boolean value, an object with an error message or a promise. Examples:

Boolean
No errors:

  isValid = () => {
    // Do internal validation
    return true;
  };

Existing error:

  isValid = () => {
    // Do internal validation
    return false;
  };

Object with error (Useful for returning custom error messages)
Existing error:

  isValid = () => {
    // Do internal validation
    return { error: 'Your error message.' };
  };

Promise
You can also return a promise from isValid. While the promise is pending, the widget will be marked as in error. When the promise resolves, the error is automatically cleared.

  isValid = () => {
    return this.existingPromise;
  };

Note: Do not create a promise inside isValid - isValid is called right before trying to persist. This means that even if a previous promise was already resolved, when the user hits 'save', isValid will be called again - if it returns a new Promise it will be immediately marked as in error until the new promise resolves.

- Test plan

- Description for the changelog

Added support for validations.

- A picture of a cute animal (not mandatory but encouraged)

……(\__/)
……(=’.’=)
…☆(”)_(”)☆

@calavera
Copy link
Contributor

Looking good so far

@cassiozen cassiozen changed the title Validation [WIP] Validation Jan 13, 2017
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

Successfully merging this pull request may close these issues.

None yet

2 participants