-
Notifications
You must be signed in to change notification settings - Fork 39
Catches JS errors in calculations on the front end and alerts users. #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
93f8a22 to
c6f2e7e
Compare
c6f2e7e to
ab895ae
Compare
|
Hi Laura! It looks like the approach for this is a bit dependent on what the team decides. Once that is decided, I can better review this pull request. At the moment, I'm seeing a couple of minor code styling items that could be adjusted:
|
|
@jwahlin This is ready for your review so we can start a discussion. I'd especially like your input on what to put in error messages. |
stephywells
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking great! I have a couple suggestions here. Great work!
js/formidable.js
Outdated
| } | ||
|
|
||
| if ( typeof total === 'undefined' || isNaN(total) ) { | ||
| if (typeof total === 'undefined' || isNaN(total)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind putting the spacing back in this line?
js/formidable.js
Outdated
|
|
||
| // Set decimal points | ||
| if ( isNumeric( dec ) ) { | ||
| if (isNumeric(dec)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get the spacing back in this line too?
js/formidable_admin.js
Outdated
| * @returns {RegExp} | ||
| */ | ||
| function getNonNumericShortcodes() { | ||
| return /\[(date|time|email|ip\])/; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our age calculation actually uses the time shortcode. Since both date and time can use formats that make them numeric, it may be better to allow them. Moving the ] after the ) in this regex should do the trick.
js/formidable.js
Outdated
| alertMessage += 'Oops! There\'s an error in the calculation in field ' + field_key + ':\n\n'; | ||
| alertMessage += thisFullCalc + '\n\n'; | ||
| if (err.message) { | ||
| alertMessage += 'The error message is: ' + err.message + '\n\n'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can remove 'The error message is:' part? That will simplify the localization if there's only one string or two that we need to add.
js/formidable.js
Outdated
| if (err.message) { | ||
| alertMessage += 'The error message is: ' + err.message + '\n\n'; | ||
| } | ||
| alertMessage += 'No worries! These are generally easy to fix.'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can remove this line too?
| } | ||
| } | ||
| } | ||
| if ( current_user_can( 'activate_plugins' ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make sure they also have permission to edit forms with current_user_can( 'frm_edit_forms' )
js/formidable.js
Outdated
| return; | ||
| } | ||
|
|
||
| alertMessage += 'Oops! There\'s an error in the calculation in field ' + field_key + ':\n\n'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to move any text strings to the localization. This is included in FrmAppHelper::localize_script()
js/formidable_admin.js
Outdated
| if (stack.length > 0 || unmatchedClosing.length > 0) { | ||
| msg = '***This calculation has at least one unmatched ( ) { } [ ].***\n\n'; | ||
| /* | ||
| //a more specific message option that has opening and closing grouped, with only the first unmatched |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this commented section need to stick around?
js/formidable_admin.js
Outdated
| * @returns {RegExp} | ||
| */ | ||
| function getNonFormShortcodes() { | ||
| return /\[(key|id\]|created|updated)/; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regex needs a little modification to make sure it only applies to the shortcodes we are checking for. Right now, it flags [keyoffield]. Should we use created-at, created-by, updated-at, updated-by?
|
Thank you for your comments, which were very helpful, as always! I'm glad to know about storing message strings in wp_localize_script so they can be translated. I've added more shortcodes to the regex checking for non-form shortcodes. As it's written, most don't require a closing bracket to match. I'd appreciate your review of this (and everything else, of course) to make sure the net I'm throwing isn't too wide. |
stephywells
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like there is one more change needed on this. Thanks for your patience!
|
|
||
| var alertMessage = ''; | ||
|
|
||
| if ( ( !jQuery( 'form' ).hasClass( 'frm-admin-viewing' ) ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line looks like it would check for the class on the first form on the page. If the first form is a login form, this will always return false. Right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So smart of you to think of this! Login forms didn't cross my mind. The jQuery docs say that hasClass will return true if any of the matched elements have the specified class. So this happens to work properly as it is, I believe.
| var warningMessage = checkMatchingParens( calculation ); | ||
| warningMessage += checkShortcodes( calculation, this ); | ||
|
|
||
| if ( warningMessage != '' ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have JS hint enabled in PHP storm? JS hint prefers !== instead of != whenever possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great hint. (pun!) Thanks.
|
Thank you very much for doing this review! I need to figure out a better system for following GitHub updates. This somehow slipped past my radar, and I didn't see it until today. The JS has been split since I wrote this. I'm sure there's a cool GitHub way to incorporate everything, but I just created a new branch and added the changes by hand. I'll submit for a pull request shortly. |
@jwahlin @stephywells First draft of catching JS calculation errors on the front end and alert owners/users.
To be safe, I hide the submit/next button when there's an error. I also put a red line around the form. I'm happy to handle this another way, if you guys have a different preference.
This is phase 1. Phase 2 will involve checking calculations in the admin for unmatched parens (and other errors, if we think of them) and alerting users.