OpenMage / magento-lts

Official OpenMage LTS codebase | Migrate easily from Magento Community Edition in minutes! Download the source code for free or contribute to OpenMage LTS | Security vulnerability patches, bug fixes, performance improvements and more.
https://www.openmage.org
Open Software License 3.0
864 stars 439 forks source link

Move away from prototype/validation.js and towards native form validation #3054

Open loekvangool opened 1 year ago

loekvangool commented 1 year ago

Description (*)

https://github.com/OpenMage/magento-lts/blob/1.9.4.x/js/prototype/validation.js is handling form validation like entry-required, validate-email. This is wildly outdated since native options exist now: https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation. Native validation removes all responsibility from us, except for adding the <input> attributes like type=email. We no longer need to care about translations, UI, adherence to RFCs, etc.

Since it is native, we can also eject (part of the) translations of form validation errors in every page (new Translate): https://github.com/OpenMage/magento-lts/blob/b6e65db33f2ca7cdc2a4f9e1da9bfd256e68dec8/app/code/core/Mage/Core/Helper/Js.php#L140

Note that security is not affected, both the current and proposed methods are client-side.

Benefits

fballiano commented 1 year ago

I'd need to check the subject a bit more in depth but I wanted to comment that the "Validate forms like it's 2013 instead!" broke me 😂😂😂😂

kiatng commented 1 year ago

A new theme package? And drop prototype.js while at it.

ADDISON74 commented 1 year ago

Just an opinion. It would be wonderful to completely remove the Prototype library from OpenMage and everything that is inherited from Magento and has become obsolete, but it is intensively used both in the Frontend and in the Backend. If this were to succeed, the big problem would be related to the extensions that use it and the custom code added by the developers. I have at least 5 extensions that use Prototype, I should migrate them to jQuery, but I don't really like to do this. If I remember correctly, there are methods in the Prototype that do not exist in jQuery.

loekvangool commented 1 year ago

What I would do is the following:

My opinionated opinion:

empiricompany commented 1 year ago

I can report on my experience attempting to remove validation that relies on the Prototype library. I created a simple observer attached to the core_block_abstract_to_html_after event that adds the required attribute to all inputs with the class required-entry.

The problem is in the "Account > Change Password" section, where the new password fields are hidden and, with the required attribute, form validation occurs on submission. Additional code is required to add/remove the required attribute depending on whether the user has selected the option to change their password or not, otherwise the new password inputs will still be considered in the form validation even if they are hidden.

Another litte thing to consider for a potential new ultra-light default theme is to modify the qty inputs by adding pattern="[0-9]*" and inputmode="numeric". (I prefer not to change the input type from text to number because it add the graphical arrows.)

Removing Prototype is a bigger challenge because all classes for configuring product options, the search completation, etc. would need to be rewritten. I think that creating a new super-lightweight theme would bring a lot of value to OpenMage, but there is a lot of work to be done and it would be necessary to create a dedicated mega pull request.

Personally, I have removed many additional Prototype libraries such as Scriptaculous and have been able to achieve Lighthouse scores above 90.