Pods: Required checkbox on Pods Form not working on user extended

Created on 24 Sep 2019  路  9Comments  路  Source: pods-framework/pods

Describe the bug
I've extended wp users with a yes/no field type (Input type: checkbox) - required field and text field - required too.
Users update their profiles in the frontend within a custom page where pods->form is embedded. Even if a user does not flag the checkbox profile is updated. This contradicts the backend behavior, where the other field is not updated if the checkbox isn't flagged

To Reproduce
Steps to reproduce the behavior:

  1. extend user with a text field and yes/no field - input checkbox - both required.
  2. create a custom template for a page where the following snippet is embedded:
if (is_user_logged_in()) {
    $user_id = get_current_user_id();
    $userpod = pods('user', $user_id);
    $fields = array('my_required_text_field', 'my_required_checkbox');
    echo $userpod->form($fields, 'Update');
} else {
   wp_die("forbidden", 403);
}
  1. write something in the text field but do not flag the checkbox
  2. Message "Form submitted successfully" appears...

4bis. Try the very same steps in the backend (wp-admin/profile.php). The error message "my_required_checkbox is empty" appears.

Expected behavior
the Backend behavior is right. An error message must appear.

Pods Version

2.7.15

WordPress Environment

5.2.3
`

wp-core

version: 5.2.3
site_language: en_US
user_language: en_US
permalink: /%year%/%monthnum%/%day%/%postname%/
https_status: false
user_registration: 0
default_comment_status: open
multisite: false
user_count: 2
dotorg_communication: true

wp-paths-sizes

wordpress_path: C:\wamp64\www\wordpress
wordpress_size: 37.54 MB (39367723 bytes)
uploads_path: C:\wamp64\www\wordpress/wp-content/uploads
uploads_size: 365.25 KB (374011 bytes)
themes_path: C:\wamp64\www\wordpress/wp-content/themes
themes_size: 27.48 MB (28816544 bytes)
plugins_path: C:\wamp64\www\wordpress/wp-content/plugins
plugins_size: 5.13 MB (5379044 bytes)
database_size: 1.09 MB (1141880 bytes)
total_size: 71.60 MB (75079202 bytes)

wp-active-theme

name: Design Italia
version: 0.28
author: Marco Gargano
author_website: http://marcogargano.com/
parent_theme: none
theme_features: widgets, editor-font-sizes, title-tag, automatic-feed-links, post-thumbnails, custom-logo, custom-header, menus, custom-background, align-wide, editor-color-palette, editor-style
theme_path: C:\wamp64\www\wordpress/wp-content/themes/design-italia

wp-themes (7)

Guten: version: 1.1.07, author: Kaira
ItaliaWP: version: 1.5.3, author: Boris Amico
Twenty Nineteen: version: 1.4, author: the WordPress team
Twenty Seventeen: version: 2.2, author: the WordPress team
Twenty Sixteen: version: 2.0, author: the WordPress team
underscore: version: 1.0.0, author: Underscores.me
WP Bootstrap Starter: version: 3.3.1, author: Afterimage Designs

wp-plugins-active (1)

Pods - Custom Content Types and Fields: version: 2.7.15, author: Pods Framework Team

wp-media

image_editor: WP_Image_Editor_GD
imagick_module_version: Not available
imagemagick_version: Not available
gd_version: bundled (2.1.0 compatible)
ghostscript_version: not available

wp-server

server_architecture: Windows NT 10.0 AMD64
httpd_software: Apache/2.4.39 (Win64) PHP/7.3.7
php_version: 7.3.7 64bit
php_sapi: apache2handler
max_input_variables: 2500
time_limit: 240
memory_limit: 1024M
max_input_time: 60
upload_max_size: 120M
php_post_max_size: 8M
curl_version: 7.64.0 OpenSSL/1.1.1c
suhosin: false
imagick_availability: false
htaccess_extra_rules: false

wp-database

extension: mysqli
server_version: 5.7.14-log
client_version: mysqlnd 5.0.12-dev - 20150407 - $Id: 7cc7cc96e675f6d72e5cf0f267f48e167c2abb23 $

wp-constants

WP_HOME: http://wp.localhost
WP_SITEURL: http://wp.localhost
WP_CONTENT_DIR: C:\wamp64\www\wordpress/wp-content
WP_PLUGIN_DIR: C:\wamp64\www\wordpress/wp-content/plugins
WP_MAX_MEMORY_LIMIT: 1024M
WP_DEBUG: true
WP_DEBUG_DISPLAY: true
WP_DEBUG_LOG: true
SCRIPT_DEBUG: false
WP_CACHE: false
CONCATENATE_SCRIPTS: undefined
COMPRESS_SCRIPTS: undefined
COMPRESS_CSS: undefined
WP_LOCAL_DEV: undefined

wp-filesystem

wordpress: writable
wp-content: writable
uploads: writable
plugins: writable
themes: writable

pods

pods-server-software: Apache/2.4.39 (Win64) PHP/7.3.7
pods-user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0
pods-session-save-path: c:/wamp64/tmp
pods-session-save-path-exists: Yes
pods-session-save-path-writable: Yes
pods-session-max-lifetime: 1440
pods-opcode-cache-apc: No
pods-opcode-cache-memcached: No
pods-opcode-cache-opcache: No
pods-opcode-cache-redis: No
pods-object-cache-apc: No
pods-object-cache-apcu: No
pods-object-cache-memcache: No
pods-object-cache-memcached: No
pods-object-cache-redis: No
pods-memory-current-usage: 34.901M
pods-memory-current-usage-real: 30.000M
pods-network-wide: No
pods-install-location: C:\wamp64\www\wordpress\wp-content\plugins\pods/
pods-developer: No
pods-tableless-mode: No
pods-light-mode: No
pods-strict: Yes
pods-allow-deprecated: No
pods-api-cache: Yes

`

Frontend Forms Bug

All 9 comments

Issue-Label Bot is automatically applying the label Type: Bug to this issue, with a confidence of 0.98. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

Frontend forms are not very stable at the moment, while they work, lots of features don't work 100%.
For frontend handling we currently advice to use plugins like Gravity Forms which are much better suitable for this and have a Pods addon available.

Ok, I found the culprit, at least in the FE:

in jquery.pods.js line 191 val = 0 is wrong: the right statement is val = null.

In fact, by inspecting the profile.php POST request I've noticed that when the checkbox isn't flagged the pods field is not even in the body field. Conversely, in the adjax-admin POST reques originating from pods->form the field is present and = 0.

I'm not an expert of pods internals, but I suspect there is a bug also in the data validation performed by the BE.

Let me explain: what are the allowable values for yes/no field - input checkbox?

1 (for checkbox flagged) and null (for checkbox not flagged)?
Or everything except null (for checkbox flagged) and null (for checkbox not flagged)?

In the former case, the BE does not perform the correct validation, since in the current version of the code 0 is considered an acceptable value. In the latter, when 0 was transmitted the usermeta value should be converted to 1 and do not remain 0.

Hello @bunglegrind
As you can see here: https://github.com/pods-framework/pods/blob/2.x/classes/fields/boolean.php#L189

Everything except a false value (false, 0, null) will be set to flagged (true), with some checks beforehand for the user created input values.
While your PR might prevent your issue it doesn't look like it's actually fixing the problem.

Hi,
I have the same issue and fixed it adding the following code in https://github.com/pods-framework/pods/blob/2.x/classes/fields/boolean.php


public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
    $errors = array();
    $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );

    if ( 1 === (int) pods_v( 'required', $options ) && $check === 0 ) {
        $errors[] = __( 'This field is required.', 'pods' );
    }

    if ( ! empty( $errors ) ) {
        return $errors;
    }
    return true;
}

@Turkal
That looks good, could you create/open a pull request for us to review and merge? If not let me know and I'll create one myself!
Thanks!

Edit:
One note on codestyle. Please change $check === 0 to 0 === $check and align the = chars.

Hi @JoryHogeveen,

I've created the pull request and included the style changes.

Best,

Checked and approved, thanks!

thanks!

Was this page helpful?
0 / 5 - 0 ratings