Skip to content

Commit

Permalink
Improve logic to validate required fields #207
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Sep 11, 2022
1 parent 60ac7ef commit 8670367
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 33 deletions.
31 changes: 6 additions & 25 deletions shortcodes/oik-contact-field.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

/**
* @copyright (C) Copyright Bobbing Wide 2022
* @package oik
*
*/


/**
* Implements bw_contact_field shortcode.
*
Expand Down Expand Up @@ -51,7 +49,7 @@ function bw_contact_field( $atts, $content, $tag ) {
/**
* Sets the required flag in #extras if the Label contains a '*' or the
* required attribute is set.
* If required sets the requiredindicator to '*' by default.
* If required sets the `requiredindicator` to '*' by default.
*/
$required = ( false !== strpos( $label, '*' ) ) ? 'y' : 'n';
$required = bw_array_get( $atts, 'required', $required);
Expand All @@ -65,33 +63,25 @@ function bw_contact_field( $atts, $content, $tag ) {
}
}

// The prefix could be set in the bw_contact_form shortcode or the first instance of the bw_contact_field shortcode.
//$prefix = bw_contact_form_prefix( 'oiku_');

$full_name = bw_contact_field_full_name( $name );
bw_register_field( $full_name, $type, $label, $args );
global $bw_contact_fields;
$bw_contact_fields[$full_name] = $full_name;
//global $bw_fields;
//bw_format_field( $bw_fields[ $name ] );
//bw_theme_field( $name, $value, $bw_fields[ $name ] );
// The next line of code is used for testing.
//bw_form_field( $full_name, $type, $label, $value, $bw_fields[ $full_name ]);


// The contact field is not expected to produce output itself. It's used in the contact form.
$html = bw_ret();
return $html;
}

/**
* Returns a field name given the label.
*
* @param string $label eg 'Name *'
* @return string lower cased sanitized version of the label
*/
function bw_contact_field_name_from_label( $label ) {
$name = $label;
$name = strtolower( $name );
$name = sanitize_key( $name);
//$name = 'oiku_' . $name;
return $name;
}

Expand All @@ -105,7 +95,7 @@ function bw_contact_field_type_from_name( $name ) {
$types = [ 'name' => 'text'
, 'email' => 'email'
, 'subject' => 'text'
, 'telephone' => 'tel' // Not yet supported by bw_form_field()
, 'telephone' => 'tel' // @TODO Not yet supported by bw_form_field()
, 'message' => 'textarea'
, 'text' => 'textarea'
];
Expand All @@ -121,15 +111,6 @@ function bw_contact_field_type_from_name( $name ) {
* @return string
*/
function bw_contact_field_full_name( $name ) {
//$prefix = bw_contact_form_prefix();
$prefix = bw_contact_form_id();
return $prefix . '_' . $name;
}

function bw_contact_form_prefix( $prefix=null ) {
static $form_prefix;
if ( $prefix !== null ) {
$form_prefix = $prefix;
}
return $form_prefix;
}
}
60 changes: 52 additions & 8 deletions shortcodes/oik-contact-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ function bw_get_contact_field_value( $field_name ) {
return( $message );
}


/**
* Gets the contact fields for the email.
*
Expand All @@ -202,6 +201,46 @@ function bw_get_contact_fields() {
return $field_values;
}

function bw_validate_contact_fields() {
global $bw_contact_fields;
$valid = true;
foreach ( $bw_contact_fields as $field ) {
$field_value = bw_get_contact_field_value( $field);
$valid = bw_validate_required_field( $field, $field_value );
if ( !$valid ) {
$label = bw_query_field_label( $field );
$text = sprintf( __( "Required field not set: %s", 'oik' ), $label );
bw_contact_issue_message( $field, "bw_field_required", $text);
break;
}
}
return $valid;
}

/**
* Validates a required field to be non-empty.
*
* @param $field_name
* @param $field_value
* @return bool - false if required field isn't set.
*/
function bw_validate_required_field( $field_name, $field_value ) {
$field_value = trim( $field_value );
global $bw_fields;
$valid = false;
$field = bw_array_get( $bw_fields, $field_name, null );
if ( null === $field ) {
return $valid;
}
// A checkbox value of '0' means it's not checked.
if ( 'checkbox' === $field['#field_type']) {
$field_value = str_replace( '0', '', $field_value );
}
$required = bw_array_get( $field['#args'], 'required', null );
$valid = ( 'y' === $required ) ? strlen( $field_value) > 0 : true;
return $valid;
}

/**
* Perform an Akismet check on the message, if it's activated
*
Expand Down Expand Up @@ -321,8 +360,7 @@ function bw_thankyou_message( $fields, $send, $sent ) {
* Process a contact form submission
*
* Handle the contact form submission
* 1. Check fields
* If message is blank then display an error message.
* 1. Check for required fields
* 2. Perform spam checking
* 3. Send email, copying user if required
* 4. Display "thank you" message
Expand All @@ -331,7 +369,8 @@ function bw_thankyou_message( $fields, $send, $sent ) {
function _bw_process_contact_form_oik() {
$email_to = bw_array_get( $_REQUEST, "oiku_email_to", null );
$message = bw_get_message();
if ( $email_to && $message ) {
$valid = bw_validate_contact_fields();
if ( $email_to && $valid ) {
oik_require( "includes/oik-contact-form-email.php" );
$fields = array();
$subject = bw_get_subject();
Expand All @@ -356,11 +395,9 @@ function _bw_process_contact_form_oik() {
bw_thankyou_message( $fields, $send, $sent );
} else {
$sent = false;
if ( !function_exists( "bw_issue_message" ) ) {
oik_require( "includes/bw_messages.php" );
}

$text = __( "Invalid. Please correct and retry.", "oik" );
bw_issue_message( null, "bw_field_required", $text );
bw_contact_issue_message( null, "bw_field_required", $text );
$displayed = bw_display_messages();
if ( !$displayed ) {
p_( $text );
Expand All @@ -369,6 +406,13 @@ function _bw_process_contact_form_oik() {
return( $sent );
}

function bw_contact_issue_message( $field, $code, $text, $type='error' ) {
if ( !function_exists( "bw_issue_message" ) ) {
oik_require( "includes/bw_messages.php" );
}
bw_issue_message( $field, $code, $text, $type );
}

/**
* Implement help hook for bw_contact_form
*/
Expand Down

0 comments on commit 8670367

Please sign in to comment.