Use less jquery when validating#2252
Conversation
WalkthroughThis pull request updates the Changes
Sequence Diagram(s)sequenceDiagram
participant U as User
participant F as frmFrontFormJS
participant V as validateForm
participant Q as QuerySelector (DOM)
U->>F: Submits form
F->>V: Call validateForm(formObject)
V->>Q: Query 'input,select,textarea' using querySelectorAll
Q-->>V: Return NodeList of fields
loop For Each Field in NodeList
V->>V: Check field properties
alt Field is confirmation
V-->>V: Return early (skip remaining checks)
else
V->>V: Perform validation (e.g., required, number checks)
end
end
V-->>F: Return validation result
F-->>U: Process submission result
Possibly related PRs
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (3)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
js/formidable.js (1)
204-225: Well-structured field validation with clear comments!The validation logic is well-organized with clear comments explaining the confirmation field handling. Consider extracting the field validation logic into a separate function for better modularity.
-fields = vanillaJsObject?.querySelectorAll( 'input,select,textarea' ); -if ( fields.length ) { - fields.forEach( - field => { - if ( '' === field.value ) { - if ( 'number' === field.type ) { - checkValidity( field, errors ); - } - - const isConfirmationField = field.name && 0 === field.name.indexOf( 'item_meta[conf_' ); - if ( ! isConfirmationField ) { - return; - } - } - - validateFieldValue( field, errors, true ); - checkValidity( field, errors ); - } - ); -} +function validateFields( vanillaJsObject, errors ) { + const fields = vanillaJsObject?.querySelectorAll( 'input,select,textarea' ); + if ( ! fields.length ) { + return; + } + + fields.forEach( field => validateSingleField( field, errors ) ); +} + +function validateSingleField( field, errors ) { + if ( '' === field.value ) { + if ( 'number' === field.type ) { + checkValidity( field, errors ); + } + + const isConfirmationField = field.name && 0 === field.name.indexOf( 'item_meta[conf_' ); + if ( ! isConfirmationField ) { + return; + } + } + + validateFieldValue( field, errors, true ); + checkValidity( field, errors ); +}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
js/formidable.js(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
- GitHub Check: PHP 8 tests in WP trunk
- GitHub Check: Cypress
- GitHub Check: PHP 7.4 tests in WP trunk
- GitHub Check: PHP 8 tests in WP trunk
- GitHub Check: PHP 7.4 tests in WP trunk
- GitHub Check: Cypress
🔇 Additional comments (1)
js/formidable.js (1)
176-235: Great improvements in thevalidateFormfunction!The changes improve performance and readability by:
- Using native
querySelectorAllinstead of jQuery selectors- Using modern JavaScript features like optional chaining
- Using
forEachinstead of jQuery's each- Using early returns for better control flow
Related issue https://github.com/Strategy11/formidable-pro/issues/5002
This update removes a use of
.find()and.length.I also dropped a lot of variables. Using
.forEach, we don't need to handle all of the loop variables ourselves.