mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-28 01:56:00 -05:00
Only require libphonenumber when rendering phone inputs
This commit is contained in:
parent
fd26186d8b
commit
da2ca40ccf
2 changed files with 16 additions and 13 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue