Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple attr for select input type #1312

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

RubenMartins
Copy link
Contributor

@RubenMartins RubenMartins commented Oct 30, 2019

Props to @rubengc #995

Didn't found this in a PR format so created this one.

Please advise.

Description

Add support for attribute multiple, showing correctly the multiple elements saved on database

Motivation and Context

Motivation to provide CMB2 with the multiple feature out of box easily.
Fixes #995

Risk Level

admin-only

Testing procedure

You can test by adding the 'multiple' attribute to a select field type
$cmb->add_field( array( 'name' => 'Test Select', 'id' => 'wiki_test_select', 'type' => 'select', 'options' => array( 'standard' => __( 'Option One', 'cmb2' ), 'custom' => __( 'Option Two', 'cmb2' ), 'none' => __( 'Option Three', 'cmb2' ), ), 'attributes' => array( 'multiple' => true ) ) );

Types of changes

Bug fix (non-breaking change which fixes an issue)

Checklist:

Props to @rubengc CMB2#995 

Didn't found this in a PR format so created this one.

Please advise.
Copy link
Member

@jtsternberg jtsternberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. There are some code-formatting issues.
  2. CMB2_Utils::normalize_if_numeric( $opt_value ) should be used where you're using just $opt_value.
  3. in_array should have the strict mode set, by setting 3rd param to true
  4. Last else-if is a repeat of the first if?
  5. Provide testing instructions please.

This can be tested against a select field with attr "multiple":
$cmb->add_field( array(
	'name'             => 'Test Select',
	'id'               => 'wiki_test_select',
	'type'             => 'select',
	'options'          => array(
		'standard' => __( 'Option One', 'cmb2' ),
		'custom'   => __( 'Option Two', 'cmb2' ),
		'none'     => __( 'Option Three', 'cmb2' ),
	),
	'attributes' => array(
		'multiple' => true
	),
) );
@RubenMartins
Copy link
Contributor Author

RubenMartins commented Jan 4, 2021

@jtsternberg This can be tested against a select field with the attribute "multiple":
$cmb->add_field( array( 'name' => 'Test Select', 'id' => 'wiki_test_select', 'type' => 'select', 'options' => array( 'standard' => __( 'Option One', 'cmb2' ), 'custom' => __( 'Option Two', 'cmb2' ), 'none' => __( 'Option Three', 'cmb2' ), ), 'attributes' => array( 'multiple' => true ) ) );

By using the attribute multiple this will submit multiple values in the same meta. This code recognizes that the value is an array and acts accordingly to save the data correctly.

This is what I remember I did to fix the issue that I faced with a select with multiple values.

I've fixed the code following your comments 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

concat_items() should check if value is array to allow multiple select
2 participants