kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
RegistrationForm: validate password mismatch
rodzic
b1017450bc
commit
109046eef8
|
@ -63,6 +63,8 @@ class RegistrationForm extends ImmutablePureComponent {
|
||||||
submissionLoading: false,
|
submissionLoading: false,
|
||||||
params: ImmutableMap(),
|
params: ImmutableMap(),
|
||||||
captchaIdempotencyKey: uuidv4(),
|
captchaIdempotencyKey: uuidv4(),
|
||||||
|
passwordConfirmation: '',
|
||||||
|
passwordMismatch: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
setParams = map => {
|
setParams = map => {
|
||||||
|
@ -77,6 +79,30 @@ class RegistrationForm extends ImmutablePureComponent {
|
||||||
this.setParams({ [e.target.name]: e.target.checked });
|
this.setParams({ [e.target.name]: e.target.checked });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onPasswordChange = e => {
|
||||||
|
const password = e.target.value;
|
||||||
|
const { passwordConfirmation } = this.state;
|
||||||
|
this.onInputChange(e);
|
||||||
|
|
||||||
|
if (password === passwordConfirmation) {
|
||||||
|
this.setState({ passwordMismatch: false });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onPasswordConfirmChange = e => {
|
||||||
|
const password = this.state.params.get('password');
|
||||||
|
const passwordConfirmation = e.target.value;
|
||||||
|
this.setState({ passwordConfirmation });
|
||||||
|
|
||||||
|
if (password === passwordConfirmation) {
|
||||||
|
this.setState({ passwordMismatch: false });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onPasswordConfirmBlur = e => {
|
||||||
|
this.setState({ passwordMismatch: !this.passwordsMatch() });
|
||||||
|
}
|
||||||
|
|
||||||
launchModal = () => {
|
launchModal = () => {
|
||||||
const { dispatch, intl, needsConfirmation, needsApproval } = this.props;
|
const { dispatch, intl, needsConfirmation, needsApproval } = this.props;
|
||||||
|
|
||||||
|
@ -113,9 +139,19 @@ class RegistrationForm extends ImmutablePureComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
passwordsMatch = () => {
|
||||||
|
const { params, passwordConfirmation } = this.state;
|
||||||
|
return params.get('password', '') === passwordConfirmation;
|
||||||
|
}
|
||||||
|
|
||||||
onSubmit = e => {
|
onSubmit = e => {
|
||||||
const { dispatch, inviteToken } = this.props;
|
const { dispatch, inviteToken } = this.props;
|
||||||
|
|
||||||
|
if (!this.passwordsMatch()) {
|
||||||
|
this.setState({ passwordMismatch: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const params = this.state.params.withMutations(params => {
|
const params = this.state.params.withMutations(params => {
|
||||||
// Locale for confirmation email
|
// Locale for confirmation email
|
||||||
params.set('locale', this.props.locale);
|
params.set('locale', this.props.locale);
|
||||||
|
@ -159,7 +195,7 @@ class RegistrationForm extends ImmutablePureComponent {
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { instance, intl, supportsEmailList } = this.props;
|
const { instance, intl, supportsEmailList } = this.props;
|
||||||
const { params } = this.state;
|
const { params, passwordConfirmation, passwordMismatch } = this.state;
|
||||||
const isLoading = this.state.captchaLoading || this.state.submissionLoading;
|
const isLoading = this.state.captchaLoading || this.state.submissionLoading;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -176,6 +212,7 @@ class RegistrationForm extends ImmutablePureComponent {
|
||||||
autoCapitalize='off'
|
autoCapitalize='off'
|
||||||
pattern='^[a-zA-Z\d_-]+'
|
pattern='^[a-zA-Z\d_-]+'
|
||||||
onChange={this.onInputChange}
|
onChange={this.onInputChange}
|
||||||
|
value={params.get('username', '')}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
<SimpleInput
|
<SimpleInput
|
||||||
|
@ -186,8 +223,14 @@ class RegistrationForm extends ImmutablePureComponent {
|
||||||
autoCorrect='off'
|
autoCorrect='off'
|
||||||
autoCapitalize='off'
|
autoCapitalize='off'
|
||||||
onChange={this.onInputChange}
|
onChange={this.onInputChange}
|
||||||
|
value={params.get('email', '')}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
|
{passwordMismatch && (
|
||||||
|
<div className='error'>
|
||||||
|
<FormattedMessage id='registration.password_mismatch' defaultMessage="Passwords don't match." />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<SimpleInput
|
<SimpleInput
|
||||||
placeholder={intl.formatMessage(messages.password)}
|
placeholder={intl.formatMessage(messages.password)}
|
||||||
name='password'
|
name='password'
|
||||||
|
@ -195,17 +238,22 @@ class RegistrationForm extends ImmutablePureComponent {
|
||||||
autoComplete='off'
|
autoComplete='off'
|
||||||
autoCorrect='off'
|
autoCorrect='off'
|
||||||
autoCapitalize='off'
|
autoCapitalize='off'
|
||||||
onChange={this.onInputChange}
|
onChange={this.onPasswordChange}
|
||||||
|
value={params.get('password', '')}
|
||||||
|
error={passwordMismatch === true}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
<SimpleInput
|
<SimpleInput
|
||||||
placeholder={intl.formatMessage(messages.confirm)}
|
placeholder={intl.formatMessage(messages.confirm)}
|
||||||
name='confirm'
|
name='password_confirmation'
|
||||||
type='password'
|
type='password'
|
||||||
autoComplete='off'
|
autoComplete='off'
|
||||||
autoCorrect='off'
|
autoCorrect='off'
|
||||||
autoCapitalize='off'
|
autoCapitalize='off'
|
||||||
onChange={this.onInputChange}
|
onChange={this.onPasswordConfirmChange}
|
||||||
|
onBlur={this.onPasswordConfirmBlur}
|
||||||
|
value={passwordConfirmation}
|
||||||
|
error={passwordMismatch === true}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
{instance.get('approval_required') &&
|
{instance.get('approval_required') &&
|
||||||
|
@ -215,6 +263,7 @@ class RegistrationForm extends ImmutablePureComponent {
|
||||||
name='reason'
|
name='reason'
|
||||||
maxLength={500}
|
maxLength={500}
|
||||||
onChange={this.onInputChange}
|
onChange={this.onInputChange}
|
||||||
|
value={params.get('reason', '')}
|
||||||
required
|
required
|
||||||
/>}
|
/>}
|
||||||
</div>
|
</div>
|
||||||
|
@ -232,12 +281,14 @@ class RegistrationForm extends ImmutablePureComponent {
|
||||||
label={intl.formatMessage(messages.agreement, { tos: <Link to='/about/tos' target='_blank' key={0}>{intl.formatMessage(messages.tos)}</Link> })}
|
label={intl.formatMessage(messages.agreement, { tos: <Link to='/about/tos' target='_blank' key={0}>{intl.formatMessage(messages.tos)}</Link> })}
|
||||||
name='agreement'
|
name='agreement'
|
||||||
onChange={this.onCheckboxChange}
|
onChange={this.onCheckboxChange}
|
||||||
|
checked={params.get('agreement', false)}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
{supportsEmailList && <Checkbox
|
{supportsEmailList && <Checkbox
|
||||||
label={intl.formatMessage(messages.newsletter)}
|
label={intl.formatMessage(messages.newsletter)}
|
||||||
name='accepts_email_list'
|
name='accepts_email_list'
|
||||||
onChange={this.onCheckboxChange}
|
onChange={this.onCheckboxChange}
|
||||||
|
checked={params.get('accepts_email_list', false)}
|
||||||
/>}
|
/>}
|
||||||
</div>
|
</div>
|
||||||
<div className='actions'>
|
<div className='actions'>
|
||||||
|
|
|
@ -18,6 +18,7 @@ export const InputContainer = (props) => {
|
||||||
'with_label': props.label,
|
'with_label': props.label,
|
||||||
'required': props.required,
|
'required': props.required,
|
||||||
'boolean': props.type === 'checkbox',
|
'boolean': props.type === 'checkbox',
|
||||||
|
'field_with_errors': props.error,
|
||||||
}, props.extraClass);
|
}, props.extraClass);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -35,6 +36,7 @@ InputContainer.propTypes = {
|
||||||
type: PropTypes.string,
|
type: PropTypes.string,
|
||||||
children: PropTypes.node,
|
children: PropTypes.node,
|
||||||
extraClass: PropTypes.string,
|
extraClass: PropTypes.string,
|
||||||
|
error: PropTypes.bool,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const LabelInputContainer = ({ label, hint, children, ...props }) => {
|
export const LabelInputContainer = ({ label, hint, children, ...props }) => {
|
||||||
|
@ -87,10 +89,11 @@ export class SimpleInput extends ImmutablePureComponent {
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
label: FormPropTypes.label,
|
label: FormPropTypes.label,
|
||||||
hint: PropTypes.node,
|
hint: PropTypes.node,
|
||||||
|
error: PropTypes.bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { hint, ...props } = this.props;
|
const { hint, error, ...props } = this.props;
|
||||||
const Input = this.props.label ? LabelInput : 'input';
|
const Input = this.props.label ? LabelInput : 'input';
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -164,7 +167,7 @@ FieldsGroup.propTypes = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Checkbox = props => (
|
export const Checkbox = props => (
|
||||||
<SimpleInput type='checkbox' value {...props} />
|
<SimpleInput type='checkbox' {...props} />
|
||||||
);
|
);
|
||||||
|
|
||||||
export class RadioGroup extends ImmutablePureComponent {
|
export class RadioGroup extends ImmutablePureComponent {
|
||||||
|
|
|
@ -371,13 +371,13 @@ code {
|
||||||
select {
|
select {
|
||||||
border-color: lighten($error-red, 12%);
|
border-color: lighten($error-red, 12%);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.error {
|
.error {
|
||||||
display: block;
|
display: block;
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
color: lighten($error-red, 12%);
|
color: lighten($error-red, 12%);
|
||||||
margin-top: 4px;
|
margin-top: 4px;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.input.disabled {
|
.input.disabled {
|
||||||
|
|
Ładowanie…
Reference in New Issue