diff --git a/projects/packages/forms/changelog/update-store-rating-type b/projects/packages/forms/changelog/update-store-rating-type new file mode 100644 index 0000000000000..952dfb9fae118 --- /dev/null +++ b/projects/packages/forms/changelog/update-store-rating-type @@ -0,0 +1,4 @@ +Significance: patch +Type: added + +Forms: Improve how we store and display ratings field values diff --git a/projects/packages/forms/src/contact-form/class-contact-form-field.php b/projects/packages/forms/src/contact-form/class-contact-form-field.php index d136d236cc2a9..acdb51e130820 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-field.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-field.php @@ -472,6 +472,26 @@ function ( $option ) { /* translators: %s is the name of a form field */ $this->add_error( sprintf( __( '%s requires a time', 'jetpack-forms' ), $field_label ) ); } + break; + case 'rating': + $max_rating = $this->get_attribute( 'max' ) ? (int) $this->get_attribute( 'max' ) : 5; + if ( str_ends_with( $field_value, '/' . $max_rating ) ) { + $field_value = explode( '/', $field_value )[0]; + } + + if ( ! is_numeric( $field_value ) ) { + /* translators: %s is the name of a form field - For example "Rate our website rating must be a number." where "Rate our website" is the name. */ + $this->add_error( sprintf( __( '%s rating must be a number.', 'jetpack-forms' ), $field_label ) ); + break; + } + + $min_value = $this->get_attribute( 'required' ) ? 1 : 0; + + if ( $max_rating < $field_value || $field_value < $min_value ) { + /* translators: %s is the name of a form field - For example "Rate our website rating must be between 1 and 5." where "Rate our website" is the name. */ + $this->add_error( sprintf( __( '%1$s rating must be between %2$d and %3$d.', 'jetpack-forms' ), $field_label, $min_value, $max_rating ) ); + } + break; case 'file': // Make sure the file field is not empty @@ -2700,7 +2720,7 @@ private function render_rating_field( $id, $label, $value, $class, $required, $r id="%1$s" type="radio" name="%2$s" - value="%3$s/%4$s" + value="%3$s" data-wp-on--change="actions.onFieldChange" class="jetpack-field-rating__input visually-hidden" %5$s diff --git a/projects/packages/forms/src/contact-form/class-contact-form.php b/projects/packages/forms/src/contact-form/class-contact-form.php index e19d131df40a8..d71e2a0cde786 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form.php +++ b/projects/packages/forms/src/contact-form/class-contact-form.php @@ -990,7 +990,7 @@ private static function render_ajax_success_wrapper( $form, $submission_success $html .= '