Skip to content

Can't set a 0 (empty) value, reverts to default #135

Closed

Description

@tylerdigital

Whenever a user submits a value that is empty() (ie. 0, "", etc) then the field's default value is used instead.

Specifically, I'm trying to get the user to enter a number into a field. my default is 1, but 0 is a valid entry. If the user submits 0, then CMB2 sees this as an empty() value and resets it to the default (1).

Is there a way around this?

The code that seems to be affecting this is
CMB2_Field->escaped_value():

        if ( false === $cb || $this->escaping_exception() ) {
            // If requesting NO escaping, return meta value
            return ! empty( $meta_value ) ? $meta_value : $this->args( 'default' );
        }

        // escaping function passed in?
        $func       = $func ? $func : 'esc_attr';
        $meta_value = ! empty( $meta_value ) ? $meta_value : $this->args( 'default' );

Activity

  1. jtsternberg commented on Jan 6, 2015

    @jtsternberg
    Member

    There are ways around it, but I've updated the library to accept non-null values. An empty field ("") is still going to be considered null and will then trigger the default, but 0 should now work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions