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

Fix #289: if no options (data) are provided generate an empty optiong… #298

Closed

Conversation

HenryVolkmer
Copy link

…roup.

Refer to https://w3c.github.io/html-reference/select.html

Scope

This pull request includes a

  • Bug fix
  • New feature
  • Translation

Changes

  • remove isset(): the propertys are setted by class-definition. We want to check if !null.
  • force a empty string as optiongroup if no data was set.

Related Issues

If this is related to an existing ticket, include a link to it as well.

@kadyrleev
Copy link

Hi, there!
Thanks for your hard work.
Any traction on this one? It's supposed to fix a bug that lives ther since Yii 2.0.15 (now we have 2.0.29)

@kadyrleev
Copy link

The fix does not help, unfortunately:
yiisoft/yii2#17147 (comment)

kartik-v added a commit that referenced this pull request Nov 1, 2019
@kartik-v
Copy link
Owner

kartik-v commented Nov 1, 2019

This has been fixed with this commit

kartik-v added a commit that referenced this pull request Nov 8, 2019
kartik-v added a commit that referenced this pull request Nov 8, 2019
kartik-v added a commit that referenced this pull request Nov 8, 2019
kartik-v added a commit that referenced this pull request Nov 8, 2019
kartik-v added a commit that referenced this pull request Nov 8, 2019
@Androphin
Copy link

Androphin commented Nov 21, 2019

Hello,

seems only partly fixed.
I load data with AJAX and if I don't enter something and the required form-field gets submitted no invalid error is shown.
It has always the 'is-valid' CSS class applied, even if the option-tag has no value/is empty.
I think it has to do with what's described here #289 (comment)
Because there is always an empty option applied to the select-form. The supplied workaround there did not work out.

Yii version: 2.0.30
yii2-widget-select2 version: 2.1.7
Using bootstrap 4 and therefore param['bsVersion'] = '4.x'.

My code:

$formatJs = <<< 'JS'
                    var formatZipItem = function(item){
                        if(item.loading){
                            return item.text;
                        }
                        $("#city-input").val(item.city);
                        var markup = '<b>'+item.zipcode+'</b>';
                        return '<div style="overflow:hidden;">'+markup+'</div>';
                    };
                    var formatZipSelection = function(item){
                        if( item.city ){
                            $("#city-input").val(item.city);
                        }
                        return item.zipcode || item.text;
                    }
                    JS;

                    $this->registerJs($formatJs, \yii\web\View::POS_HEAD);

                    $resultsJs = <<< JS
                    function (data) {
                        return { results: data.items };
                    }
                    JS;


                    echo $form->field($model, 'zip' )->widget(Select2::class, [
                        'options' => ['placeholder' => 'Zipcode'],
                        'size' => Select2::SMALL,
                        'addon' => [
                                'prepend' => ['content' => '<i class="fas fa-map-marker"></i>'],
                        ],
                        'pluginOptions' => [
                            'allowClear' => true,
                            'selectOnClose' => true,
                            'minimumInputLength' => 3,
                            'language' => [
                                'errorLoading' => new JsExpression("function () { return 'Missing...'; }"),
                            ],
                            'ajax' => [
                                'delay' => 250,
                                'url' => '/autocomplete/json/zip-city-list',
                                'dataType' => 'json',
                                'data' => new JsExpression('function(params) { return {zipcode:params.term}; }'),
                                'processResults' => new JsExpression( $resultsJs ),
                                'cache' => true
                            ],
                            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                            'templateResult' => new JsExpression('formatZipItem'),
                            'templateSelection' => new JsExpression('formatZipSelection')
                        ],
                    ])->label(false);

I tried with set empty 'data' for the widget.
I tried with an empty 'initValueText'.
I tried with no placeholder, because if I add a placeholder to the config like in above code, it gets added as an option like "Zipcode"

Any suggestions?

@tmendesilva
Copy link

Same problem when options are loading by AJAX and field is required

@kartik-v
Copy link
Owner

It seems the issue is as mentioned by @Axhind in this comment.

This is due to an update to the yii2 form validation script which behaves in a totally different way now and skips select dropdowns with no options OR just one empty option.

The solution workaround is that you need to have at least ONE EXTRA option in your SELECT dropdown beyond the empty value (which is set via placeholder).

@kadyrleev
Copy link

Hope this will be fixed as the issue is in yii.activeForm indeed:
yiisoft/yii2#17147 (comment)

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

Successfully merging this pull request may close these issues.

5 participants