Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

Select Option Type

Open ankitaraikundalia opened this issue 11 years ago • 2 comments

Hi, Two questions related to the select option type:

  1. What does 'no-validate' parameter do?
  2. How to populate the choices array dynamically?

Example: I have the following code in the settings file:

<?php if ( ! defined( 'FW' ) ) {
    die( 'Forbidden' );
}

$options = array(
    'appearance' => array(
        'title'   => __( 'Appearance Settings', 'fw' ),
        'type'    => 'tab',
        'options' => array(
                        'color-scheme' => array(
                            'type'  => 'select',
                            'value' => 'blue',
                            'attr'  => array( 'class' => 'custom-class', 'data-foo' => 'bar' ),
                            'label' => __('Color Scheme', 'fw'),
                            'desc'  => __('Description', 'fw'),
                            'help'  => __('Help tip', 'fw'),
                            'choices' => array(
                                'blue' => __('Blue', 'fw'),
                                'green' => array(
                                    'text' => __('Green', 'fw'),
                                    'attr' => array('data-foo' => 'bar'),
                                )
                            ),
                            /**
                             * Allow not existing choices
                             * Used when select is used in another option types and populated dynamically
                             */
                            'no-validate' => false,
                        )
                    )
                )
);

I want to populate the choices 'blue' and 'green' from the names of the css files I have in my theme-name/styles/ folder, like at present I have blue.css and green.css in the folder. If I add red.css file in the folder, it should be reflected in the select options.

ankitaraikundalia avatar Dec 23 '14 13:12 ankitaraikundalia

What does 'no-validate' parameter do?

By default, on form submit, the selected choice is checked if it exists in the 'choices' array, if not, the default 'value' parameter is used and saved to the database. So you can't change the select with "Inspect element" and submit a not existing choice.

But if you want to disable that verification, you can set the 'no-validate' parameter. You may need that when you use the select option-type somewhere and populate it with dynamic <option> from javascript.

How to populate the choices array dynamically? I want to populate the choices 'blue' and 'green' from the names of the css files I have in my theme-name/styles/ folder, like at present I have blue.css and green.css in the folder. If I add red.css file in the folder, it should be reflected in the select options.

Write a function that searches for the .css files and returns the choices array. For e.g.

// file: inc/helpers.php

function fw_theme_get_styles_choices() {
    $dir = get_template_directory() .'/styles';
    $choices = array();

    foreach (glob($dir .'/*.css') as $path) {
        $style = preg_replace('/\.css$/', '', basename($path));

        $choices[ $style ] = fw_id_to_title($style);
    }

    return $choices;
}

then use it in the options array

$options = array(
    'appearance' => array(
        'title'   => __( 'Appearance Settings', 'fw' ),
        'type'    => 'tab',
        'options' => array(
            'color-scheme' => array(
                'type'  => 'select',
                'value' => 'blue',
                'label' => __('Color Scheme', 'fw'),
                'choices' => fw_theme_get_styles_choices(),
            )
        )
    )
);

ghost avatar Dec 23 '14 14:12 ghost

@moldcraft Thank you.. This works perfect...

ankitaraikundalia avatar Dec 24 '14 06:12 ankitaraikundalia