Skip to content

Commit 770d0c9

Browse files
ClearlyClaireimrasalghul
authored andcommitted
Add HTML form validation for the registration form (mastodon#14560)
* Add HTML-level validation of username in sign-up form * Make required fields with incorrect values more visible * Enable HTML form validation for the registration form * Mark agreement checkbox as required client-side * Add minimum length to password * Add client-side password confirmation validation
1 parent 4bccb2e commit 770d0c9

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

app/javascript/packs/public.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ function main() {
116116
new Rellax('.parallax', { speed: -1 });
117117
}
118118

119+
delegate(document, '#registration_user_password_confirmation,#registration_user_password', 'input', () => {
120+
const password = document.getElementById('registration_user_password');
121+
const confirmation = document.getElementById('registration_user_password_confirmation');
122+
if (password.value && password.value !== confirmation.value) {
123+
confirmation.setCustomValidity((new IntlMessageFormat(messages['password_confirmation.mismatching'] || 'Password confirmation does not match', locale)).format());
124+
} else {
125+
confirmation.setCustomValidity('');
126+
}
127+
});
128+
119129
delegate(document, '.custom-emoji', 'mouseover', getEmojiAnimationHandler('data-original'));
120130
delegate(document, '.custom-emoji', 'mouseout', getEmojiAnimationHandler('data-static'));
121131

app/javascript/styles/mastodon/forms.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ code {
364364
box-shadow: none;
365365
}
366366

367-
&:focus:invalid:not(:placeholder-shown) {
367+
&:focus:invalid:not(:placeholder-shown),
368+
&:required:invalid:not(:placeholder-shown) {
368369
border-color: lighten($error-red, 12%);
369370
}
370371

app/views/about/_registration.html.haml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
.simple_form__overlay-area{ class: (closed_registrations? && @instance_presenter.closed_registrations_message.present?) ? 'simple_form__overlay-area__blurred' : '' }
2-
= simple_form_for(new_user, url: user_registration_path, namespace: 'registration') do |f|
2+
= simple_form_for(new_user, url: user_registration_path, namespace: 'registration', html: { novalidate: false }) do |f|
33
%p.lead= t('about.federation_hint_html', instance: content_tag(:strong, site_hostname))
44

55
.fields-group
66
= f.simple_fields_for :account do |account_fields|
7-
= account_fields.input :username, wrapper: :with_label, label: false, required: true, input_html: { 'aria-label' => t('simple_form.labels.defaults.username'), :autocomplete => 'off', placeholder: t('simple_form.labels.defaults.username') }, append: "@#{site_hostname}", hint: false, disabled: closed_registrations?
7+
= account_fields.input :username, wrapper: :with_label, label: false, required: true, input_html: { 'aria-label' => t('simple_form.labels.defaults.username'), :autocomplete => 'off', placeholder: t('simple_form.labels.defaults.username'), pattern: Account::USERNAME_RE.source }, append: "@#{site_hostname}", hint: false, disabled: closed_registrations?
88

99
= f.input :email, placeholder: t('simple_form.labels.defaults.email'), required: true, input_html: { 'aria-label' => t('simple_form.labels.defaults.email'), :autocomplete => 'off' }, hint: false, disabled: closed_registrations?
10-
= f.input :password, placeholder: t('simple_form.labels.defaults.password'), required: true, input_html: { 'aria-label' => t('simple_form.labels.defaults.password'), :autocomplete => 'off' }, hint: false, disabled: closed_registrations?
10+
= f.input :password, placeholder: t('simple_form.labels.defaults.password'), required: true, input_html: { 'aria-label' => t('simple_form.labels.defaults.password'), :autocomplete => 'off', :minlength => User.password_length.first, :maxlength => User.password_length.last }, hint: false, disabled: closed_registrations?
1111
= f.input :password_confirmation, placeholder: t('simple_form.labels.defaults.confirm_password'), required: true, input_html: { 'aria-label' => t('simple_form.labels.defaults.confirm_password'), :autocomplete => 'off' }, hint: false, disabled: closed_registrations?
1212

1313
- if approved_registrations?
@@ -16,7 +16,7 @@
1616
= invite_request_fields.input :text, as: :text, wrapper: :with_block_label, required: false
1717

1818
.fields-group
19-
= f.input :agreement, as: :boolean, wrapper: :with_label, label: t('auth.checkbox_agreement_html', rules_path: about_more_path, terms_path: terms_path), disabled: closed_registrations?
19+
= f.input :agreement, as: :boolean, wrapper: :with_label, label: t('auth.checkbox_agreement_html', rules_path: about_more_path, terms_path: terms_path), required: true, disabled: closed_registrations?
2020

2121
.actions
2222
= f.button :button, sign_up_message, type: :submit, class: 'button button-primary', disabled: closed_registrations?

0 commit comments

Comments
 (0)