Skip to content

Conversation

@LauraSteiner
Copy link
Contributor

@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.

@jwahlin
Copy link
Contributor

jwahlin commented Sep 1, 2017

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:

  1. In our JavaScript, we use camel case. That is the norm for JavaScript according to the research I've done on it.
  2. There is some inconsistent spacing in the PR. That could be cleaned up. We include spaces within function calls in PHP, but not JavaScript. Each language has its own code styling. A couple examples of places where I'm seeing inconsistent spacing:
  • function create_calculations_error_message(err, field_key, thisFullCalc ){ (should not have space after thisFullCalc)
  • if (action ==="frm_forms_preview"){ (should be space after ===)

@LauraSteiner
Copy link
Contributor Author

@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.

Copy link
Contributor

@stephywells stephywells left a 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)) {
Copy link
Contributor

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)) {
Copy link
Contributor

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?

* @returns {RegExp}
*/
function getNonNumericShortcodes() {
return /\[(date|time|email|ip\])/;
Copy link
Contributor

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';
Copy link
Contributor

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.';
Copy link
Contributor

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' ) ) {
Copy link
Contributor

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';
Copy link
Contributor

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()

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
Copy link
Contributor

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?

* @returns {RegExp}
*/
function getNonFormShortcodes() {
return /\[(key|id\]|created|updated)/;
Copy link
Contributor

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?

@LauraSteiner
Copy link
Contributor Author

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.

@LauraSteiner LauraSteiner removed the request for review from jwahlin January 31, 2018 03:51
Copy link
Contributor

@stephywells stephywells left a 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' ) ) ) {
Copy link
Contributor

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?

Copy link
Contributor Author

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 != '' ) {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great hint. (pun!) Thanks.

@LauraSteiner
Copy link
Contributor Author

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.

@stephywells stephywells deleted the issue/1524 branch February 11, 2019 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants