-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(setErrorOnSchema): set error as object of errors
- Loading branch information
Mariusz Przodała
committed
Mar 20, 2020
1 parent
7fa8186
commit 6e286a0
Showing
4 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React, { Component } from 'react'; | ||
import Schema from 'form-schema-validation'; | ||
import isEmpty from 'lodash/isEmpty'; | ||
import PropTypes from 'prop-types'; | ||
import { FieldConnect } from '../../components/FieldConnect'; | ||
|
||
const getInternalSchema = () => new Schema({ | ||
name: { | ||
type: String, | ||
validators: [ | ||
{ | ||
validator: (value) => { | ||
if (value.length === 2) { | ||
return 'FieldValidatorError'; | ||
} | ||
return true; | ||
}, | ||
}, | ||
], | ||
}, | ||
surname: { | ||
type: String, | ||
}, | ||
age: { | ||
type: String, | ||
}, | ||
}); | ||
|
||
class CustomField extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = {}; | ||
} | ||
|
||
componentDidMount() { | ||
this.context.setFieldValidator(this.setAuthorsValidator); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.context.removeFieldValidator(this.setAuthorsValidator); | ||
} | ||
|
||
|
||
setAuthorsValidator = (_model, value) => { | ||
const schema = getInternalSchema(); | ||
const allErrors = []; | ||
value.forEach((author) => { | ||
const errors = schema.validate(author); | ||
allErrors.push(!isEmpty(errors) ? errors : undefined); | ||
}); | ||
return allErrors.length ? allErrors : true; | ||
}; | ||
|
||
render() { | ||
return ( | ||
<div /> | ||
); | ||
} | ||
} | ||
|
||
CustomField.contextTypes = { | ||
setFieldValidator: PropTypes.func, | ||
removeFieldValidator: PropTypes.func, | ||
}; | ||
|
||
CustomField.propTypes = {}; | ||
|
||
CustomField.defaultProps = {}; | ||
|
||
export default FieldConnect(CustomField); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters