Skip to content

Commit

Permalink
Fix non rendered fields still validating and displaying response subm…
Browse files Browse the repository at this point in the history
…ission (#41979)

* Avoid validating non renderable fields

* Prevent non-renderable fields from displaying in form submission response

* changelog

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/13535853444

Upstream-Ref: Automattic/jetpack@75236ed
  • Loading branch information
talldan authored and matticbot committed Feb 26, 2025
1 parent cb595a7 commit b247392
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
This is an alpha version! The changes listed here are not final.

### Fixed
- Forms: Ensure fields that skip rendering (like empty options fields) do not trigger validation or show value in form submission response.
- Forms: Fix 404 error when a user submits an invalid form with JavaScript disabled.

## [0.39.0] - 2025-02-24
Expand Down
5 changes: 3 additions & 2 deletions src/contact-form/class-contact-form-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,14 @@ public function is_error() {
* Validates the form input
*/
public function validate() {
$field_type = $this->maybe_override_type();

// If it's not required, there's nothing to validate
if ( ! $this->get_attribute( 'required' ) ) {
if ( ! $this->get_attribute( 'required' ) || ! $this->is_field_renderable( $field_type ) ) {
return;
}

$field_id = $this->get_attribute( 'id' );
$field_type = $this->maybe_override_type();
$field_label = $this->get_attribute( 'label' );

if ( isset( $_POST[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
Expand Down
10 changes: 10 additions & 0 deletions src/contact-form/class-contact-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,11 @@ public function process_submission() {
// For all fields, grab label and value
foreach ( $field_ids['all'] as $field_id ) {
$field = $this->fields[ $field_id ];

if ( ! $field->is_field_renderable( $field->get_attribute( 'type' ) ) ) {
continue;
}

$label = $i . '_' . $field->get_attribute( 'label' );
$value = $field->value;

Expand All @@ -1172,6 +1177,11 @@ public function process_submission() {
// Extra fields have their prefix starting from count( $all_values ) + 1
foreach ( $field_ids['extra'] as $field_id ) {
$field = $this->fields[ $field_id ];

if ( ! $field->is_field_renderable( $field->get_attribute( 'type' ) ) ) {
continue;
}

$label = $i . '_' . $field->get_attribute( 'label' );
$value = $field->value;

Expand Down

0 comments on commit b247392

Please sign in to comment.