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

Select Options Fieldtype : default behaviour when field is required #349

Closed
er314 opened this issue Aug 17, 2017 · 6 comments
Closed

Select Options Fieldtype : default behaviour when field is required #349

er314 opened this issue Aug 17, 2017 · 6 comments

Comments

@er314
Copy link

er314 commented Aug 17, 2017

I guess the observed behaviour is intended, but it's very surprising to me :

When a Select Options field is "required", I would expect (I would want) that the user MUST position a value for this field, otherwise PW MUST refuse to save the page.

But the observed behaviour is that PW does position value id 1 by default.
And I can't seem to find a way to prevent this : in field settings, tried pre-selecting option blank to no avail ; tried setting default value = 0 to no avail.

Of course the user MAY change this default value. But this is misleading, and it's not "my intended behaviour".
In other words, when a Text field is "required", the user MUST position a value for this field ; PW does not position a default value "hello world" without my consent. Right ? :-)

Edit : Disregarding our opinions about what the default behaviour should be, I guess we can agree that, when "What option do you want pre-selected (if any)" is set to blank, then there should be no option pre-selected, also when the field is required ; so in this case it's a bug ?

PW 3.0.71
cheers

@Toutouwai
Copy link

I just encountered this problem. To restate the issue...

If "Select (Single value)" is chosen as the input type for a Select Options field, and the field is set to "required", the first option in the select input becomes pre-selected in Page Edit even if the "What options do you want pre-selected? (if any)" setting is none.

So despite the description of that setting being "This field also serves as a preview of your selected input type (InputfieldSelect) and options" it is in fact not an accurate preview.

Setting:
2017-12-07_151903

Page Edit:
2017-12-07_151939

If "Radios (Single value)" is chosen as the input type then no option is pre-selected (i.e. this input type works as expected).

@Toutouwai
Copy link

Toutouwai commented Jan 16, 2018

As @adrianbj pointed out in the forums, this is actually an issue with InputfieldSelect rather than FieldtypeOptions.

I think this line could be amended to:

if($allowBlank && !$this->attr('multiple') && !$hasBlankOption) {

No need that I can see to disallow the blank option if the inputfield is required - there is already the allowBlank argument for whenever there is a need to disallow a blank option.

@adrianbj
Copy link

Not a solution for the PW admin, but in case someone is mostly concerned about getting this to work on the frontend via the form API (which is my current need), this hack works:

$f->options = array('' => 'Choose One') + $f->options;

With this you can set required to true and get JS validation to work and still not have an initial valid option selected.

That said, I think the solution suggested by @Toutouwai sounds great!

@er314
Copy link
Author

er314 commented Feb 27, 2018

As per latest blog post : Tested in v3.0.93 -> this is not fixed.
@Toutouwai has explained very well the issue above, + his proposed solution seems to work for me.

ryancramerdesign added a commit to processwire/processwire that referenced this issue Feb 27, 2018
…nputfieldSelect required field not having blank option
@ryancramerdesign
Copy link
Member

While this isn't specifically a bug, I agree that the behavior suggested here seems better, so have updated it to have that behavior when InputfieldSelect is used with an Options field. But since the InputfieldSelect behavior was intended, I'm going to leave the behavior as-is outside of use with an options field. After the next master, I'll switch the dev branch to update the behavior for other uses of InputfieldSelect, as I think that would need more testing just to make sure there aren't other side effects. Thanks.

@er314
Copy link
Author

er314 commented Feb 27, 2018

Yes, sorry, "this is not fixed" was not an accurate statement.
Thank you for the update.

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

No branches or pull requests

4 participants