155f40e5e4
* Add frontmatter to docs files (prep to move) * Move docs into position for website migration * Migrate website from netlify-cms-www Some modifications, including most of the changes in https://github.com/netlify/netlify-cms-www/pull/58 (previously reverted). Also updated the readme and added hugo-bin for quicker onboarding of new docs contributors. * Remove netlify.toml This allows separate build commands for cms-demo and netlifycms.org. * Remove website/netlify.toml May re-add later, but it's not doing anything for now. * Remove unused content file
59 lines
1.8 KiB
Markdown
Executable File
59 lines
1.8 KiB
Markdown
Executable File
---
|
|
title: Validation
|
|
position: 70
|
|
---
|
|
|
|
# Collection Field Validation
|
|
|
|
## Available validations to use on `config.yml`:
|
|
|
|
- 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"] }`
|
|
|
|
|
|
## Advanced Guide (For widget authors)
|
|
|
|
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:
|
|
|
|
```javascript
|
|
isValid = () => {
|
|
// Do internal validation
|
|
return true;
|
|
};
|
|
```
|
|
|
|
Existing error:
|
|
|
|
```javascript
|
|
isValid = () => {
|
|
// Do internal validation
|
|
return false;
|
|
};
|
|
```
|
|
|
|
**Object with `error` (useful for returning custom error messages)**
|
|
Existing error:
|
|
|
|
```javascript
|
|
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.
|
|
|
|
```javascript
|
|
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. |