Jump to content

Select Options Fieldtype (FieldtypeOptions)


ro-bo
 Share

Recommended Posts

Hello,

I started a new project, where I have some Options Fields to select some different values.

I'm using multi-language support and the input type is Select (Single Value).

Processwire v 2.6.1.

When I'm editing the page and choose some values everything is all right! But when I set the value back to blank, suddenly strange things happens:

If I'm checking the selected value via API I always get the ID of the first and the last option-item separate by a pipe:

eg. 1|4 if there are 4 options or 1|2 if there are 2 options available/selectable

So I take a look in the DB and realized that there are always 2 entries for the same page_id with the first and the last data-ID of the selectable options.

Is it a bug or a feature?

For my client sometimes it's necessary to choose a blank option, for me it's important to check this by API.

So how can I realize this with the Select Options Fieldtype?

Does anyone have an idea to get this working?

Thanks for your help,

Robert

Link to comment
Share on other sites

@ro-bo the Options fieldtype behaves like a Page fieldtype. For more information see the documentation page here: https://processwire.com/api/modules/select-options-fieldtype/

In terms of having a blank option selectable, all that you need to do is just make sure that you don't have the "required" box checked (on the Input tab). You don't need to add your own blank option. 

I do think it's unusual that you are getting a "1|2" or "1|4" after choosing "Blank". I've tried to duplicate that here (with select-single value) but can't. If I output my value after choosing blank, then I get just blank. My test is just "echo $page->option_field;" Can you post some code of how you are checking the value? I would also be curious about your settings for "which options are pre-selected" and how your options are defined in the "what are the selectable options" textarea.

Link to comment
Share on other sites

  • 3 weeks later...

Hello Ryan,

thanks for your answer and I'm sorry for this late reply. It's strange... I don't have the "required" box chekced and I also don't have added a blank option. I don't understand why I get "1|2" aso. after choosing "Blank" and why there are 2 entries for the same page_id in DB. I think it's not the code how I'm checking the value (I'm  familiar with PHP and MySQL), and my code is a little bit to extensive to post it here (it's splitted in many files with functions -> yes, I'm using wire('pages') instead of $pages...) ... sorry. There are a lot of option-fields in my project and I always get the same issue...

Here is an example of my settings in the "what are the selectable options" textarea (3 different Languages):

DE:

1=EZ

2=DZ

3=DZ/Familie

4=SUITE

EN:

1=single

2=double

3=double/family

4=suite/family

FR:

1=individuel

2=double

3=double/familiale

4=suite/familiale

The "required" box isn't checked, and I'm using it as select-single value.

I have to admit that I have installed and uninstalled a lot of Modules in the meantime - perhaps it's the reason why I have these problems...?

Many thanks for PW!

Link to comment
Share on other sites

Hello again!

Now I made a new/blank installation without "multi language" or any other modules except the "Select Options Field". I didn't make any new fields or templates beside an Option-Field called "testfield".

Unfortunately with this new installation I can reproduce this issue too...

In the attachment you can find some screenshots from database, PW-backend and frontend-output with <?php echo $page->testfield; ?>.

I have already mentioned that this issue appears, when I firstly choose one option and save the page -> then I set it back to blank and save it again.

Does anyone have any idea?

post-3208-0-57890500-1439293029_thumb.jp

post-3208-0-47854200-1439293058_thumb.jp

post-3208-0-64759900-1439293066_thumb.jp

post-3208-0-33290300-1439293071_thumb.jp

  • Like 1
Link to comment
Share on other sites

Hi LostKobrakai. Thanks for your answer.

Here is a step-by-step guide:

1. Download Processwire 2.6.1 Master from http://processwire.com/download/

2. Upload PW with site-blank profile to Server

3. Install PW

4. Login to PW-Backen

5. Modules -> Core -> Select Options -> Install

6. Setup -> Fields -> Add New

7. Basics Tab -> Name: test -> Type: Options -> Label: testfield

8. Details Tab -> Select (Single Value) -> Selectable Options: test1 (new Line) test2 (new Line) test3

9. Actions Tab -> Add Field to Template "Home"

10. Save (Add Field after title)

11. Pages Tab -> Home -> edit

12. Choose test1 -> Save

13. Choose "blank" (no Value/Option) -> Save

14. Now I check the Database and there are 2 Entries for the same page_id

15. open home.php with an editor -> write "echo $page->test;" under "include("./basic-page.php");" -> Save and upload it to the server

16. Open the Home Page (Frontend) with browser -> the echo command outputs "1|3" means 1 for the first Entry and 3 for the last entry. If I add an Option (see step 8) e.g. test4 the echo command outputs "1|4" aso.

I have not done anything else!

Meantime I tested it on two different Servers (My Local Server and EDIS-Provider where some PW-Pages run successfully) and the phenomenon continues to exist.

I think (and Ryan confirmed it in the poste further above) that it's unusual -> the echo command should output no value (blank) instead of "1|2" or "1|3" ...

Best Regards

Robert

  • Like 3
Link to comment
Share on other sites

I'm not really sure how this is selecting 1|3 here, but there's certainly a lack in checking for empty strings in the FieldtypeOptions::sanitizeValue() function.

To fix this add this right at the start of the function:

if($value === ""){
    return $this->getBlankValue($page, $field);
}
  • Like 4
Link to comment
Share on other sites

@ro-bo thanks for the detailed instructions. I was able to duplicate it after following the steps in those instructions. And @LostKobrakai was right that there was a missing getBlankValue in the sanitizeValue method. This week's PW dev version will include this fix at the top of sanitizeValue:

if(empty($value)) return $this->getBlankValue($page, $field);
  • Like 2
Link to comment
Share on other sites

  • 2 years later...

Hi there! And thanks for Processwire.

Got an interesting question: is it possible to programmatically get the default value of SelectOptions field? Getting all possible values is not a problem, but docs can say nothing about getting the default value. Thanks in advance!

Link to comment
Share on other sites

There are other ways, but check out the Request Info panel in Tracy when viewing the field settings in the admin. See that there is an "initValue" setting.

This indicates that you can do:

$fields->get("map_width")->initValue

image.png.b988eb55d390d7ab9f9016fc3de9593d.png

 

In the Tracy Console panel (again if you are viewing the field in the admin), you can do:

image.png.c11bb36a46f37bf0d7797ad63cff4956.png

This is because the Console makes available $field, $template, and $module when you're in the admin and viewing an individual field, template, or module.

  • Like 3
Link to comment
Share on other sites

  • 3 weeks later...
On 7/19/2015 at 2:27 AM, ryan said:

In terms of having a blank option selectable, all that you need to do is just make sure that you don't have the "required" box checked (on the Input tab). You don't need to add your own blank option. 

Ryan,

I am not convinced this really works. The problem is that I want the field to be required, but I don't want anything preselected. Unless I am missing something there doesn't seem to be a way to do this. I actually think this is an issue not just with this fieldtype, but with the select type inputfields in general.

Link to comment
Share on other sites

@adrian, you are right that the issue is with InputfieldSelect. I added a note to that effect at GitHub.

Until it is fixed this seems to be an okay workaround:

$wire->addHookBefore('InputfieldSelect::render', function (HookEvent $event) {
    $inputfield = $event->object;
    $field = $inputfield->hasField;
    if(!$field || $field->type != 'FieldtypeOptions') return;
    if($field->required) $inputfield->required = false;
});

The inputfield is still treated as required in Page Edit.

  • Like 1
Link to comment
Share on other sites

16 minutes ago, Robin S said:

if($field->required) $inputfield->required = false;

Thanks Robin - looks good for backend, but if you are using the form API on the frontend, and relying on the HTML5 "required" attribute to trigger your JS validation then this will break it.

This is my frontend hack:

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

 

  • Like 1
Link to comment
Share on other sites

22 minutes ago, adrian said:

This is my frontend hack:


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

This approach works for the backend too. :-)

$wire->addHookBefore('InputfieldSelect::render', function (HookEvent $event) {
    $inputfield = $event->object;
    $field = $inputfield->hasField;
    if(!$field || $field->type != 'FieldtypeOptions') return;
    if($field->required) $inputfield->options = array('' => 'Choose One') + $inputfield->options;
});

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...