Skip to content

Commit 2eac76d

Browse files
committed
Render input on frontend
1 parent aa47a31 commit 2eac76d

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

projects/packages/forms/src/contact-form/class-contact-form-field.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ public function validate() {
249249
$this->add_error( sprintf( __( '%s requires at least one selection', 'jetpack-forms' ), $field_label ) );
250250
}
251251
break;
252+
case 'number':
253+
// Make sure the number address is valid
254+
if ( ! is_numeric( $field_value ) ) {
255+
/* translators: %s is the name of a form field */
256+
$this->add_error( sprintf( __( '%s requires a number', 'jetpack-forms' ), $field_label ) );
257+
}
258+
break;
252259
default:
253260
// Just check for presence of any text
254261
if ( ! is_string( $field_value ) || ! strlen( trim( $field_value ) ) ) {
@@ -910,6 +917,25 @@ public function render_date_field( $id, $label, $value, $class, $required, $requ
910917
return $field;
911918
}
912919

920+
/**
921+
* Return the HTML for the number field.
922+
*
923+
* @param int $id - the ID.
924+
* @param string $label - the label.
925+
* @param string $value - the value of the field.
926+
* @param string $class - the field class.
927+
* @param bool $required - if the field is marked as required.
928+
* @param string $required_field_text - the text in the required text field.
929+
* @param string $placeholder - the field placeholder content.
930+
*
931+
* @return string HTML
932+
*/
933+
public function render_number_field( $id, $label, $value, $class, $required, $required_field_text, $placeholder ) {
934+
$field = $this->render_label( 'number', $id, $label, $required, $required_field_text );
935+
$field .= $this->render_input_field( 'number', $id, $value, $class, $placeholder, $required );
936+
return $field;
937+
}
938+
913939
/**
914940
* Return the HTML for the default field.
915941
*
@@ -1102,6 +1128,9 @@ public function render_field( $type, $id, $label, $value, $class, $placeholder,
11021128
case 'consent':
11031129
$field .= $this->render_consent_field( $id, $field_class );
11041130
break;
1131+
case 'number':
1132+
$field .= $this->render_number_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder );
1133+
break;
11051134
default: // text field
11061135
$field .= $this->render_default_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder, $type );
11071136
break;

projects/packages/forms/src/contact-form/class-contact-form-plugin.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,19 @@ public static function gutenblock_render_field_file( $atts, $content ) {
520520
return Contact_Form::parse_contact_field( $atts, $content );
521521
}
522522

523+
/**
524+
* Render the number field.
525+
*
526+
* @param array $atts - the block attributes.
527+
* @param string $content - html content.
528+
*
529+
* @return string HTML for the file upload field.
530+
*/
531+
public static function gutenblock_render_field_number( $atts, $content ) {
532+
$atts = self::block_attributes_to_shortcode_attributes( $atts, 'number' );
533+
return Contact_Form::parse_contact_field( $atts, $content );
534+
}
535+
523536
/**
524537
* Add the 'Form Responses' menu item as a submenu of Feedback.
525538
*/

projects/packages/forms/src/contact-form/class-contact-form.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,9 @@ public static function get_default_label_from_type( $type ) {
868868
case 'name':
869869
$str = __( 'Name', 'jetpack-forms' );
870870
break;
871+
case 'number':
872+
$str = __( 'Number', 'jetpack-forms' );
873+
break;
871874
case 'email':
872875
$str = __( 'Email', 'jetpack-forms' );
873876
break;
@@ -977,6 +980,7 @@ public function get_field_ids() {
977980
switch ( $type ) {
978981
case 'email':
979982
case 'name':
983+
case 'number':
980984
case 'url':
981985
case 'subject':
982986
case 'textarea':

0 commit comments

Comments
 (0)