diff --git a/src/components/forms/phone-input.jsx b/src/components/forms/phone-input.jsx index 5a06d1de5..4684da2bc 100644 --- a/src/components/forms/phone-input.jsx +++ b/src/components/forms/phone-input.jsx @@ -17,6 +17,21 @@ const validationHOCFactory = require('./validations.jsx').validationHOCFactory; require('./row.scss'); require('./phone-input.scss'); +const Formsy = require('formsy-react'); +const libphonenumber = require('google-libphonenumber'); +const phoneNumberUtil = libphonenumber.PhoneNumberUtil.getInstance(); + +Formsy.addValidationRule('isPhone', (values, value) => { + if (typeof value === 'undefined') return true; + if (value && value.national_number === '+') return true; + try { + const parsed = phoneNumberUtil.parse(value.national_number, value.country_code.iso2); + return phoneNumberUtil.isValidNumber(parsed); + } catch (err) { + return false; + } +}); + class PhoneInput extends React.Component { constructor (props) { super(props); diff --git a/src/components/forms/validations.jsx b/src/components/forms/validations.jsx index c882aa40d..b8b86a27f 100644 --- a/src/components/forms/validations.jsx +++ b/src/components/forms/validations.jsx @@ -1,24 +1,12 @@ const defaults = require('lodash.defaultsdeep'); const intl = require('../../lib/intl.jsx'); -const libphonenumber = require('google-libphonenumber'); const omit = require('lodash.omit'); -const phoneNumberUtil = libphonenumber.PhoneNumberUtil.getInstance(); const PropTypes = require('prop-types'); const React = require('react'); module.exports.validations = { notEquals: (values, value, neq) => (value !== neq), - notEqualsField: (values, value, field) => (value !== values[field]), - isPhone: (values, value) => { - if (typeof value === 'undefined') return true; - if (value && value.national_number === '+') return true; - try { - const parsed = phoneNumberUtil.parse(value.national_number, value.country_code.iso2); - return phoneNumberUtil.isValidNumber(parsed); - } catch (err) { - return false; - } - } + notEqualsField: (values, value, field) => (value !== values[field]) }; module.exports.validations.notEqualsUsername = module.exports.validations.notEquals;