-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(app): Add validation to network auth forms #2559
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,34 @@ export default class ConnectForm extends React.Component<Props, State> { | |
}) | ||
} | ||
|
||
getEapFields = (eapMethod: ?WifiEapOption): Array<WifiAuthField> => { | ||
return get(eapMethod, 'options', []).map(field => ({ | ||
...field, | ||
name: `eapConfig.${field.name}`, | ||
})) | ||
} | ||
|
||
getValidationSchema = (values: any) => { | ||
let fields = [] | ||
const errors = {} | ||
if (this.props.securityType === WPA_PSK_SECURITY) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole block is shared with |
||
fields = WIFI_PSK_FIELDS | ||
} else { | ||
const selectedEapMethod = find(this.props.eapOptions, { | ||
name: values.eapConfig.eapType, | ||
}) | ||
fields = this.getEapFields(selectedEapMethod) | ||
} | ||
fields.forEach(f => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick but this could be a reduce: return fields.reduce((errors, field) => {
const {name, displayName, required} = field
const value = get(values, name, '')
if (required && !value) {
set(errors, name, `${displayName} is required`)
} else if (name === 'psk' && value.length < 8) {
set(errors, name, 'Password must be at least 8 characters')
}
return errors
}, {}) |
||
if (f.required && !get(values, f.name)) { | ||
set(errors, f.name, `${f.displayName} is required`) | ||
} else if (f.name === 'psk' && get(values, f.name).length < 8) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we pull |
||
set(errors, `${f.name}`, 'Password must be at least 8 characters') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: unnecessary template string for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shoot, i removed it and then reimplemented it. Will revise |
||
} | ||
}) | ||
return errors | ||
} | ||
|
||
render () { | ||
const {showPassword} = this.state | ||
const {securityType, eapOptions, close} = this.props | ||
|
@@ -70,25 +98,33 @@ export default class ConnectForm extends React.Component<Props, State> { | |
? `${CONNECT_FIELD_ID_PREFIX}${eapMethodField}` | ||
: '' | ||
|
||
// TODO(mc, 2018-10-18): form validation | ||
return ( | ||
<Formik | ||
onSubmit={this.onSubmit} | ||
validate={this.getValidationSchema} | ||
render={formProps => { | ||
const {handleChange, handleSubmit, values, setValues} = formProps | ||
const { | ||
handleChange, | ||
handleSubmit, | ||
values, | ||
setValues, | ||
handleBlur, | ||
errors, | ||
touched, | ||
} = formProps | ||
|
||
// disable submit if form is pristine or errors present | ||
const disabled = isEmpty(touched) || !isEmpty(errors) | ||
|
||
const eapMethod = get(values, eapMethodField) | ||
let fields: Array<WifiAuthField> = [] | ||
|
||
if (securityType === WPA_PSK_SECURITY) { | ||
fields = WIFI_PSK_FIELDS | ||
} else if (securityType === WPA_EAP_SECURITY) { | ||
const selectedEapMethod = find(eapOptions, {name: eapMethod}) | ||
fields = get(selectedEapMethod, 'options', []).map(field => ({ | ||
...field, | ||
name: `eapConfig.${field.name}`, | ||
})) | ||
fields = this.getEapFields(selectedEapMethod) | ||
} | ||
|
||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<FormTable> | ||
|
@@ -121,13 +157,16 @@ export default class ConnectForm extends React.Component<Props, State> { | |
showPassword={!!showPassword[field.name]} | ||
onChange={handleChange} | ||
toggleShowPassword={this.toggleShowPassword} | ||
onBlur={handleBlur} | ||
errors={errors} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than pass the whole |
||
touched={touched} | ||
/> | ||
))} | ||
</FormTable> | ||
<BottomButtonBar | ||
buttons={[ | ||
{children: 'Cancel', onClick: close}, | ||
{children: 'Join', type: 'submit'}, | ||
{children: 'Join', type: 'submit', disabled}, | ||
]} | ||
/> | ||
</form> | ||
|
@@ -137,3 +176,11 @@ export default class ConnectForm extends React.Component<Props, State> { | |
) | ||
} | ||
} | ||
|
||
// Helper function to check for empty objects | ||
function isEmpty (obj) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair enough! |
||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) return false | ||
} | ||
return true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
values
should be typed as{[name: string]: string}
as inonSubmit
on line 45. Should probably consolidate it intotype ConnectFormValues
or something