Skip to content

Commit bbad652

Browse files
authored
fix(checkout): add form_has_auto_generate_password() and strip JS password rules (#742)
Add protected form_has_auto_generate_password() method to the Checkout class that iterates form settings to detect password fields with auto_generate_password enabled. Use this method in get_js_validation_rules() to unset the password, password_conf, and valid_password rules when auto-generate is active, preventing the JS validator from blocking submission on a field that is never rendered. Closes #741
1 parent 6ded15a commit bbad652

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

inc/checkout/class-checkout.php

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,33 @@ public function get_checkout_variables() {
21232123
return apply_filters('wu_get_checkout_variables', $variables, $this);
21242124
}
21252125

2126+
/**
2127+
* Returns true when the current checkout form has a password field
2128+
* configured with auto_generate_password enabled.
2129+
*
2130+
* Used to suppress client-side password validation rules when no
2131+
* password input is rendered.
2132+
*
2133+
* @since 2.0.20
2134+
* @return bool
2135+
*/
2136+
protected function form_has_auto_generate_password(): bool {
2137+
2138+
if ( ! $this->checkout_form) {
2139+
return false;
2140+
}
2141+
2142+
foreach ($this->checkout_form->get_settings() as $step) {
2143+
foreach (wu_get_isset($step, 'fields', []) as $field) {
2144+
if ('password' === wu_get_isset($field, 'type') && ! empty($field['auto_generate_password'])) {
2145+
return true;
2146+
}
2147+
}
2148+
}
2149+
2150+
return false;
2151+
}
2152+
21262153
/**
21272154
* Converts the PHP validation rules into a JS-friendly structure.
21282155
*
@@ -2140,39 +2167,15 @@ public function get_checkout_variables() {
21402167
*/
21412168
public function get_js_validation_rules(): array {
21422169

2143-
/*
2144-
* When the checkout form has a password field with auto_generate_password
2145-
* enabled, the hidden flag is submitted with the form but is not present
2146-
* in the GET request at render time. We detect this from the form settings
2147-
* so the JS validator knows to skip password rules without needing the
2148-
* flag to be in the request.
2149-
*/
2150-
$form_auto_generates_password = false;
2151-
2152-
if ($this->checkout_form) {
2153-
$password_fields = $this->checkout_form->get_all_fields_by_type('password');
2154-
2155-
foreach ($password_fields as $field) {
2156-
if (! empty($field['auto_generate_password'])) {
2157-
$form_auto_generates_password = true;
2158-
break;
2159-
}
2160-
}
2161-
}
2162-
21632170
$raw_rules = $this->validation_rules();
21642171

21652172
/*
2166-
* When the form auto-generates the password, clear the password rules
2167-
* so the JS validator does not require a password field that is not
2168-
* rendered. The server-side validation_rules() already handles this via
2169-
* request_or_session(), but at render time the flag is not in the request
2170-
* so we must detect it from the form settings instead.
2173+
* When the checkout form uses auto-generated passwords, strip the
2174+
* password-related rules from the JS ruleset so the client-side
2175+
* validator does not block submission on a field that is never shown.
21712176
*/
2172-
if ($form_auto_generates_password) {
2173-
$raw_rules['password'] = '';
2174-
$raw_rules['password_conf'] = '';
2175-
$raw_rules['valid_password'] = '';
2177+
if ($this->form_has_auto_generate_password()) {
2178+
unset($raw_rules['password'], $raw_rules['password_conf'], $raw_rules['valid_password']);
21762179
}
21772180

21782181
/*

0 commit comments

Comments
 (0)