• Resolved sanitar

    (@sanitar)


    hi!it’s some going wrong with “adverts_field_select” field
    documentation say that in “options” i must type like this

        array(
            array( "value" => 1, "text" => "One", "depth" => 1 ),
            array( "value" => 5, "text" => "Five", "depth" => 1 ),
            // more options here ...
        );

    but i have dinamical array

    foreach ($countries_array as $val)
     {
      $prepeared_countries[]= array( "value" => "1", "text" => "1");
     }

    ,for example let it be like this
    $a=array(array( "value" => "1", "text" => "One"));

    when i place a variable into the option

    $form["field"][] = array(
                "name" => "country",
                "type" => "adverts_field_select",
                "order" => 4,
                "label" => __("Category", "adverts"),
                "max_choices" => 1,
                "options" =>     $a,
                "validator" => array(
                    array( 
                        "name" => "max_choices",
                        "params" => array( "max_choices" => 1 )
                    )
                )
            );

    it’s give me the error

    Fatal error: You need to specify options source for field [country]. in /home/****/*******.com/www/wp-content/plugins/wpadverts/includes/functions.php on line 1365

    if i doing like this

    $form["field"][] = array(
                "name" => "country",
                "type" => "adverts_field_select",
                "order" => 4,
                "label" => __("Category", "adverts"),
                "max_choices" => 1,
                "options" =>     array($a),
                "validator" => array(
                    array( 
                        "name" => "max_choices",
                        "params" => array( "max_choices" => 1 )
                    )
                )
            );

    i’ll get just empty select field,without options
    but if i type just like this

    $form["field"][] = array(
                "name" => "country",
                "type" => "adverts_field_select",
                "order" => 4,
                "label" => __("Category", "adverts"),
                "max_choices" => 1,
                "options" =>     array(    array( "value" => 1, "text" => "One", "depth" => 1 ),
        array( "value" => 5, "text" => "Five", "depth" => 1 ),),
                "validator" => array(
                    array( 
                        "name" => "max_choices",
                        "params" => array( "max_choices" => 1 )
                    )
                )
            );

    it give me the two options in select field.what is going wrong with array in variable?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    hmm i am not sure, i created a code snippet from your code and used it on my dev site, the code adds a Country field with two options so the variable $a seems fine.

    The snippet is

    
    add_filter( "adverts_form_load", function( $form ) {
        if( $form["name"] != "advert" ) {
            return $form;
        }
    
        $a = array( 
            array( "value" => "1", "text" => "One"),
            array( "value" => "2", "text" => "Two") 
        );
    
        $form["field"][] = array(
            "name" => "country",
            "type" => "adverts_field_select",
            "order" => 4,
            "label" => __("Category", "adverts"),
            "max_choices" => 1,
            "options" =>     $a,
            "validator" => array(
                array( 
                    "name" => "max_choices",
                    "params" => array( "max_choices" => 1 )
                )
            )
        );
        return $form;
    } );
    

    The error you are seeing is triggered only if the field does not have the “options” or “options_callback” params are either not set or are set to null.

    Can you maybe paste your whole filter and function it will be easier to tell then?

    Thread Starter sanitar

    (@sanitar)

    omg,i saw what i doing wrong:the problem was just in declaration out of the function.when i took it inside-it’s works fine.i just lost focus about scope of variables.thanks,now it’s all fine.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘options array for select field’ is closed to new replies.