openlibrary
openlibrary copied to clipboard
Replace all jQuery `validate` instances with built-in HTML or JS validation
Problem
As part of an overall reduction of the OL code size to improve site performance, one of the most obvious and easiest targets is jQuery validate, a form validation plugin that can be almost completely duplicated with simpler, faster built-in HTML checks.
Current behavior:
Currently, the plugin is used like so to ensure required inputs are filled, emails are correctly formatted, and the publish date is not more than a year in the future: https://github.com/internetarchive/openlibrary/blob/b010f39ad5bbb5a476db816bf5aae9f9b0be2971/openlibrary/plugins/openlibrary/js/validate.js#L7-L16
Not only is the current implementation inefficient, but it has bizarre, non-user-friendly side-effects. For instance:
- On the
/contact
page, therequired
attribute on the "Your Question" field, instead of displaying the desired error message if the form is submitted with an empty question, actually hides the whole question field, making the form impossible to submit:
-
If a
type="email"
is added to an email input in a form using jQueryvalidate
, the plugin adds non-user-friendly and visually awkward on-key-up validation as the user types -
The error messages, such as this one in
/add
, are located only in the JavaScript, and cannot be translated - which you can verify by searchingmessages.pot
for "Are you sure that" -
All of these validation checks, including very important ones like "required" will not run if the user has JavaScript disabled
Proposal & Constraints
Since in practice jQuery validate is only used for 3 different checks, removing it should be very simple, and involve just:
- Replacing each instance of
class="required"
in a jQuery validate form with the HTMLrequired
attribute - Replacing each instance of
class="email"
in a jQuery validate form with the HTMLtype="email"
attribute - For the
publish-date
custom validation, which is only used inadd.html
: a. Move the existing publish date validation function into theadd-book.js
file which handles other JS validation for the form b. Add the error message to the HTML so it can be correctly internationalized c. Ensure the function fires on the correct input on blur - Clean up: Remove the
validate
class from each relevant form, remove the invocation fromindex.js
, deletevalidate.js
, and uninstall the plugin
Subtasks
- [x] #9607
- [x] #9608
- [x] #9610
- [ ] #9816
Related files
validate.js
support.html
/admin/people/index.html
/admin/people/view.html
/books/add.html
/books/edit.html
/tag/add.html
/tag/edit.html
/author/edit.html
Stakeholders
@cdrini
Instructions for Contributors
- Please run these commands to ensure your repository is up to date before creating a new branch to work on this issue and each time after pushing code to Github, because the pre-commit bot may add commits to your PRs upstream.