Skip to content

Commit 1468478

Browse files
authored
Merge pull request #130 from mailchimp/fix/100
Ensure that form data persists when validation fails.
2 parents 3e87b99 + 20beac2 commit 1468478

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

assets/js/mailchimp.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,44 @@
22
(function ($) {
33
function mc_beforeForm() {
44
// Disable the submit button
5-
$('#mc_signup_submit').attr('disabled', 'disabled');
5+
$('.mc_signup_submit_button').attr('disabled', 'disabled');
66
}
77

8-
function mc_success(data) {
8+
function mc_success(data, status, xhr, submittedForm) {
9+
let form = $(submittedForm);
10+
if (!submittedForm || !form) {
11+
form = $('.mc_signup_form');
12+
}
13+
914
// Re-enable the submit button
10-
$('#mc_signup_submit').removeAttr('disabled');
15+
$('.mc_signup_submit_button').removeAttr('disabled');
1116

1217
// Put the response in the message div
13-
$('#mc_message').html(data);
18+
form.find('.mc_message_wrapper').html(data);
1419

1520
// See if we're successful, if so, wipe the fields
16-
const reg = /class="|'mc_success_msg"|'/i;
21+
const reg = /class=(["'])mc_success_msg\1/i;
1722

1823
if (reg.test(data)) {
19-
$('#mc_signup_form').each(function () {
24+
$(form).each(function () {
2025
this.reset();
2126
});
22-
$('#mc_submit_type').val('js');
27+
28+
$('.mc_submit_type').val('js');
2329
}
30+
2431
window.scrollTo({
25-
top: document.getElementById('mc_signup').offsetTop - 28,
32+
top: parseInt(form.offset().top, 10) - 28,
2633
behavior: 'smooth',
2734
});
2835
}
2936

3037
$(function ($) {
3138
// Change our submit type from HTML (default) to JS
32-
$('#mc_submit_type').val('js');
39+
$('.mc_submit_type').val('js');
3340

3441
// Attach our form submitter action
35-
$('#mc_signup_form').ajaxForm({
42+
$('.mc_signup_form').ajaxForm({
3643
url: window.mailchimpSF.ajax_url,
3744
type: 'POST',
3845
dataType: 'text',

mailchimp_widget.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ function mailchimp_sf_signup_form( $args = array() ) {
167167
?>
168168

169169
<div id="mc_signup">
170-
<form method="post" action="#mc_signup" id="mc_signup_form">
171-
<input type="hidden" id="mc_submit_type" name="mc_submit_type" value="html" />
170+
<form method="post" action="#mc_signup" id="mc_signup_form" class="mc_signup_form">
171+
<input type="hidden" id="mc_submit_type" class="mc_submit_type" name="mc_submit_type" value="html" />
172172
<input type="hidden" name="mcsf_action" value="mc_submit_signup_form" />
173173
<?php wp_nonce_field( 'mc_submit_signup_form', '_mc_submit_signup_form_nonce', false ); ?>
174174

@@ -184,7 +184,7 @@ function mailchimp_sf_signup_form( $args = array() ) {
184184

185185
<div class="mc_form_inside">
186186

187-
<div class="updated" id="mc_message">
187+
<div class="update mc_message_wrapper" id="mc_message">
188188
<?php echo wp_kses_post( mailchimp_sf_global_msg() ); ?>
189189
</div><!-- /mc_message -->
190190

@@ -267,7 +267,7 @@ function mailchimp_sf_signup_form( $args = array() ) {
267267
?>
268268

269269
<div class="mc_signup_submit">
270-
<input type="submit" name="mc_signup_submit" id="mc_signup_submit" value="<?php echo esc_attr( $submit_text ); ?>" class="button" />
270+
<input type="submit" name="mc_signup_submit" class="mc_signup_submit_button" id="mc_signup_submit" value="<?php echo esc_attr( $submit_text ); ?>" class="button" />
271271
</div><!-- /mc_signup_submit -->
272272

273273
<?php

tests/cypress/e2e/mailchimp-block.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,22 @@ describe('Block Tests', () => {
246246
});
247247
});
248248

249+
it('Form data should persist if validation fails', () => {
250+
// Verify
251+
const firstName = 'John';
252+
const lastName = 'Doe';
253+
cy.visit(`/?p=${postId}`);
254+
cy.get('#mc_mv_EMAIL').should('exist');
255+
cy.get('#mc_mv_FNAME').clear().type(firstName);
256+
cy.get('#mc_mv_LNAME').clear().type(lastName);
257+
cy.get('#mc_signup_submit').should('exist');
258+
cy.get('#mc_signup_submit').click();
259+
cy.get('.mc_error_msg').should('exist');
260+
cy.get('.mc_error_msg').contains('Email Address: This value should not be blank.');
261+
cy.get('#mc_mv_FNAME').should('have.value', firstName);
262+
cy.get('#mc_mv_LNAME').should('have.value', lastName);
263+
});
264+
249265
// TODO: Add tests for the Double Opt-in and Update existing subscribers settings.
250266
// TODO: Add tests for the block styles settings.
251267
// TODO: Add tests for the form submission.

tests/cypress/e2e/settings.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,22 @@ describe('Admin can update plugin settings', () => {
153153
cy.get('#mc_unsub_link').should('not.exist');
154154
});
155155
});
156+
157+
it('Form data should persist if validation fails', () => {
158+
// Verify
159+
[shortcodePostURL, blockPostPostURL].forEach((url) => {
160+
const firstName = 'John';
161+
const lastName = 'Doe';
162+
cy.visit(url);
163+
cy.get('#mc_mv_EMAIL').should('exist');
164+
cy.get('#mc_mv_FNAME').clear().type(firstName);
165+
cy.get('#mc_mv_LNAME').clear().type(lastName);
166+
cy.get('#mc_signup_submit').should('exist');
167+
cy.get('#mc_signup_submit').click();
168+
cy.get('.mc_error_msg').should('exist');
169+
cy.get('.mc_error_msg').contains('Email Address: This value should not be blank.');
170+
cy.get('#mc_mv_FNAME').should('have.value', firstName);
171+
cy.get('#mc_mv_LNAME').should('have.value', lastName);
172+
});
173+
});
156174
});

0 commit comments

Comments
 (0)