Don’t try to validate an optional field if it is empty (#1237)
This commit is contained in:
parent
63df2e8baa
commit
a5d4fd41a4
@ -176,7 +176,9 @@
|
||||
"name": "Damien Duhamel",
|
||||
"avatar_url": "https://avatars3.githubusercontent.com/u/3051737?v=4",
|
||||
"profile": "http://www.damienduhamel.fr",
|
||||
"contributions": []
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "juliankrispel",
|
||||
|
@ -63,7 +63,7 @@ These wonderful folks are responsible for developing and maintaining Netlify CMS
|
||||
| [<img src="https://avatars2.githubusercontent.com/u/33676?v=4" width="100px;"/><br /><sub><b>Cássio Souza</b></sub>](https://twitter.com/cassiozen)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=cassiozen "Code") | [<img src="https://avatars3.githubusercontent.com/u/2112202?v=4" width="100px;"/><br /><sub><b>Shawn Erquhart</b></sub>](https://erquh.art)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=erquhart "Code") | [<img src="https://avatars3.githubusercontent.com/u/11071?v=4" width="100px;"/><br /><sub><b>Andrey Okonetchnikov</b></sub>](http://okonet.ru)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=okonet "Code") | [<img src="https://avatars0.githubusercontent.com/u/6515?v=4" width="100px;"/><br /><sub><b>Mathias Biilmann</b></sub>](https://www.netlify.com)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=biilmann "Code") | [<img src="https://avatars1.githubusercontent.com/u/1425133?v=4" width="100px;"/><br /><sub><b>Benaiah Mischenko</b></sub>](http://benaiah.me)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=Benaiah "Code") | [<img src="https://avatars0.githubusercontent.com/u/2281080?v=4" width="100px;"/><br /><sub><b>Rafael Conde</b></sub>](http://rafaelconde.net)<br />[🎨](#design-rafaelconde "Design") [💻](https://github.com/netlify/netlify-cms/commits?author=rafaelconde "Code") | [<img src="https://avatars0.githubusercontent.com/u/2513147?v=4" width="100px;"/><br /><sub><b>Joseph Earl</b></sub>](http://josephearl.co.uk)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=josephearl "Code") |
|
||||
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|
||||
| [<img src="https://avatars2.githubusercontent.com/u/5713670?v=4" width="100px;"/><br /><sub><b>Brian Douglas</b></sub>](https://briandouglas.me/)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=bdougie "Code") | [<img src="https://avatars3.githubusercontent.com/u/44568?v=4" width="100px;"/><br /><sub><b>Frances Berriman</b></sub>](https://fberriman.com)<br />[🎨](#design-phae "Design") | [<img src="https://avatars0.githubusercontent.com/u/1050?v=4" width="100px;"/><br /><sub><b>David Calavera</b></sub>](https://github.com/calavera)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=calavera "Code") | [<img src="https://avatars0.githubusercontent.com/u/8883545?v=4" width="100px;"/><br /><sub><b>Luis Correia</b></sub>](https://luisc.pt)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=lmcorreia "Code") | [<img src="https://avatars1.githubusercontent.com/u/20345941?v=4" width="100px;"/><br /><sub><b>Caleb</b></sub>](https://github.com/tech4him1)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=tech4him1 "Code") | [<img src="https://avatars0.githubusercontent.com/u/3292160?v=4" width="100px;"/><br /><sub><b>Jerry Holmes</b></sub>](http://www.pagedragon.com)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=jholmes033169 "Code") | [<img src="https://avatars0.githubusercontent.com/u/8377185?v=4" width="100px;"/><br /><sub><b>abumalick</b></sub>](https://github.com/abumalick)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=abumalick "Code") |
|
||||
| [<img src="https://avatars1.githubusercontent.com/u/4513526?v=4" width="100px;"/><br /><sub><b>Eamonn Bell</b></sub>](http://www.eamonnbell.com)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=eamonnbell "Code") | [<img src="https://avatars2.githubusercontent.com/u/6111186?v=4" width="100px;"/><br /><sub><b>Jessica Parsons</b></sub>](https://github.com/verythorough)<br />[📖](https://github.com/netlify/netlify-cms/commits?author=verythorough "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/71047?v=4" width="100px;"/><br /><sub><b>Kyle Mathews</b></sub>](https://www.bricolage.io)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=KyleAMathews "Code") | [<img src="https://avatars1.githubusercontent.com/u/16258690?v=4" width="100px;"/><br /><sub><b>americool</b></sub>](https://github.com/americool)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=americool "Code") | [<img src="https://avatars3.githubusercontent.com/u/3051737?v=4" width="100px;"/><br /><sub><b>Damien Duhamel</b></sub>](http://www.damienduhamel.fr)<br /> | [<img src="https://avatars1.githubusercontent.com/u/1188186?v=4" width="100px;"/><br /><sub><b>Julian Krispel-Samsel</b></sub>](http://reactrocket.com)<br /> | [<img src="https://avatars0.githubusercontent.com/u/6445815?v=4" width="100px;"/><br /><sub><b>Kevin McCormack</b></sub>](http://harlemsquirrel.github.io/)<br /> |
|
||||
| [<img src="https://avatars1.githubusercontent.com/u/4513526?v=4" width="100px;"/><br /><sub><b>Eamonn Bell</b></sub>](http://www.eamonnbell.com)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=eamonnbell "Code") | [<img src="https://avatars2.githubusercontent.com/u/6111186?v=4" width="100px;"/><br /><sub><b>Jessica Parsons</b></sub>](https://github.com/verythorough)<br />[📖](https://github.com/netlify/netlify-cms/commits?author=verythorough "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/71047?v=4" width="100px;"/><br /><sub><b>Kyle Mathews</b></sub>](https://www.bricolage.io)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=KyleAMathews "Code") | [<img src="https://avatars1.githubusercontent.com/u/16258690?v=4" width="100px;"/><br /><sub><b>americool</b></sub>](https://github.com/americool)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=americool "Code") | [<img src="https://avatars3.githubusercontent.com/u/3051737?v=4" width="100px;"/><br /><sub><b>Damien Duhamel</b></sub>](http://www.damienduhamel.fr)<br />[💻](https://github.com/netlify/netlify-cms/commits?author=Dammmien "Code") | [<img src="https://avatars1.githubusercontent.com/u/1188186?v=4" width="100px;"/><br /><sub><b>Julian Krispel-Samsel</b></sub>](http://reactrocket.com)<br /> | [<img src="https://avatars0.githubusercontent.com/u/6445815?v=4" width="100px;"/><br /><sub><b>Kevin McCormack</b></sub>](http://harlemsquirrel.github.io/)<br /> |
|
||||
| [<img src="https://avatars3.githubusercontent.com/u/1923476?v=4" width="100px;"/><br /><sub><b>Kalin Chernev</b></sub>](https://github.com/kalinchernev)<br /> | [<img src="https://avatars2.githubusercontent.com/u/5018268?v=4" width="100px;"/><br /><sub><b>tortilaman</b></sub>](https://github.com/tortilaman)<br /> | [<img src="https://avatars0.githubusercontent.com/u/1184334?v=4" width="100px;"/><br /><sub><b>Václav Klecanda</b></sub>](http://www.vxk.cz)<br /> | [<img src="https://avatars0.githubusercontent.com/u/2983665?v=4" width="100px;"/><br /><sub><b>DrianHillman</b></sub>](http://drianhillman.me)<br /> | [<img src="https://avatars1.githubusercontent.com/u/1198032?v=4" width="100px;"/><br /><sub><b>Sean Crawford</b></sub>](https://github.com/seantcoyote)<br /> | [<img src="https://avatars1.githubusercontent.com/u/111932?v=4" width="100px;"/><br /><sub><b>Robert Riemann</b></sub>](http://blog.riemann.cc)<br /> | [<img src="https://avatars1.githubusercontent.com/u/4165105?v=4" width="100px;"/><br /><sub><b>Ben Berman</b></sub>](http://jygabyte.com)<br /> |
|
||||
| [<img src="https://avatars3.githubusercontent.com/u/4349324?v=4" width="100px;"/><br /><sub><b>Benjamin Kniffler</b></sub>](https://github.com/bkniffler)<br /> | [<img src="https://avatars1.githubusercontent.com/u/845983?v=4" width="100px;"/><br /><sub><b>Mike Wickett</b></sub>](http://www.wickett.ca)<br /> | [<img src="https://avatars1.githubusercontent.com/u/2751799?v=4" width="100px;"/><br /><sub><b>Rory Claasen</b></sub>](http://roryclaasen.me)<br /> | [<img src="https://avatars3.githubusercontent.com/u/724844?v=4" width="100px;"/><br /><sub><b>Frederic Brodbeck</b></sub>](http://www.freder.io/)<br /> | [<img src="https://avatars2.githubusercontent.com/u/1245746?v=4" width="100px;"/><br /><sub><b>Stuart Dum</b></sub>](https://github.com/simplystuart)<br /> | [<img src="https://avatars0.githubusercontent.com/u/8184251?v=4" width="100px;"/><br /><sub><b>Ryan Watters</b></sub>](https://github.com/rdwatters)<br /> | [<img src="https://avatars3.githubusercontent.com/u/4315?v=4" width="100px;"/><br /><sub><b>Helder S Ribeiro</b></sub>](https://twitter.com/hsribei)<br /> |
|
||||
| [<img src="https://avatars1.githubusercontent.com/u/979966?v=4" width="100px;"/><br /><sub><b>Artem Govorov</b></sub>](http://dm.gl)<br /> | [<img src="https://avatars3.githubusercontent.com/u/2683300?v=4" width="100px;"/><br /><sub><b>Cédric Delpoux</b></sub>](http://xuopled.netlify.com/)<br /> | [<img src="https://avatars3.githubusercontent.com/u/83225?v=4" width="100px;"/><br /><sub><b>imorente</b></sub>](https://github.com/imorente)<br /> | [<img src="https://avatars3.githubusercontent.com/u/5230460?v=4" width="100px;"/><br /><sub><b>David Francoeur</b></sub>](http://davidfrancoeur.com)<br /> | [<img src="https://avatars3.githubusercontent.com/u/1954977?v=4" width="100px;"/><br /><sub><b>Rusta</b></sub>](https://github.com/Rusta)<br /> | [<img src="https://avatars1.githubusercontent.com/u/1299786?v=4" width="100px;"/><br /><sub><b>Henrik Lau Eriksson</b></sub>](http://henrik.laueriksson.com)<br /> | [<img src="https://avatars0.githubusercontent.com/u/1294877?v=4" width="100px;"/><br /><sub><b>Kraig Walker</b></sub>](https://www.kraigwalker.com)<br /> |
|
||||
|
@ -6,6 +6,13 @@ import ValidationErrorTypes from 'Constants/validationErrorTypes';
|
||||
|
||||
const truthy = () => ({ error: false });
|
||||
|
||||
const isEmpty = value => (
|
||||
value === null ||
|
||||
value === undefined ||
|
||||
(value.hasOwnProperty('length') && value.length === 0) ||
|
||||
(value.constructor === Object && Object.keys(value).length === 0)
|
||||
);
|
||||
|
||||
export default class Widget extends Component {
|
||||
static propTypes = {
|
||||
controlComponent: PropTypes.func.isRequired,
|
||||
@ -84,14 +91,9 @@ export default class Widget extends Component {
|
||||
this.props.onValidate(errors);
|
||||
};
|
||||
|
||||
validatePresence(field, value) {
|
||||
validatePresence = (field, value) => {
|
||||
const isRequired = field.get('required', true);
|
||||
if (isRequired && (
|
||||
value === null ||
|
||||
value === undefined ||
|
||||
(value.hasOwnProperty('length') && value.length === 0) ||
|
||||
(value.constructor === Object && Object.keys(value).length === 0)
|
||||
)) {
|
||||
if (isRequired && isEmpty(value)) {
|
||||
const error = {
|
||||
type: ValidationErrorTypes.PRESENCE,
|
||||
message: `${ field.get('label', field.get('name')) } is required.`,
|
||||
@ -100,10 +102,15 @@ export default class Widget extends Component {
|
||||
return { error };
|
||||
}
|
||||
return { error: false };
|
||||
}
|
||||
};
|
||||
|
||||
validatePattern(field, value) {
|
||||
validatePattern = (field, value) => {
|
||||
const pattern = field.get('pattern', false);
|
||||
|
||||
if (isEmpty(value)) {
|
||||
return { error: false };
|
||||
}
|
||||
|
||||
if (pattern && !RegExp(pattern.first()).test(value)) {
|
||||
const error = {
|
||||
type: ValidationErrorTypes.PATTERN,
|
||||
@ -114,7 +121,7 @@ export default class Widget extends Component {
|
||||
}
|
||||
|
||||
return { error: false };
|
||||
}
|
||||
};
|
||||
|
||||
validateWrappedControl = (field) => {
|
||||
const response = this.wrappedControlValid();
|
||||
@ -126,7 +133,7 @@ export default class Widget extends Component {
|
||||
} else if (response instanceof Promise) {
|
||||
response.then(
|
||||
() => { this.validate({ error: false }); },
|
||||
(err) => {
|
||||
(err) => {
|
||||
const error = {
|
||||
type: ValidationErrorTypes.CUSTOM,
|
||||
message: `${ field.get('label', field.get('name')) } - ${ err }.`,
|
||||
|
Loading…
x
Reference in New Issue
Block a user