ro-bo Posted July 11, 2015 Share Posted July 11, 2015 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 More sharing options...
ro-bo Posted July 14, 2015 Author Share Posted July 14, 2015 Someone has an idea? In the meantime i solved it with this code: $out = count($page->fieldname) == 1 ? $page->fieldname->title : ''; Is this the right strategy? Robert Link to comment Share on other sites More sharing options...
ryan Posted July 19, 2015 Share Posted July 19, 2015 @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 More sharing options...
ro-bo Posted August 7, 2015 Author Share Posted August 7, 2015 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 More sharing options...
ro-bo Posted August 11, 2015 Author Share Posted August 11, 2015 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? 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted August 11, 2015 Share Posted August 11, 2015 If you can recreate the issue, could you please post a step-by-step guide to it or upload a site-profile of your installation? Link to comment Share on other sites More sharing options...
ro-bo Posted August 11, 2015 Author Share Posted August 11, 2015 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 3 Link to comment Share on other sites More sharing options...
ro-bo Posted August 18, 2015 Author Share Posted August 18, 2015 Please help me on my way . Can anyone replicate it? Link to comment Share on other sites More sharing options...
LostKobrakai Posted August 18, 2015 Share Posted August 18, 2015 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); } 4 Link to comment Share on other sites More sharing options...
ro-bo Posted August 18, 2015 Author Share Posted August 18, 2015 Hello LostKobrakai!Great, now it works fine. No DB-entries and no value! But then it is a bug in the "FieldtypeOptions" class, isn't it? Link to comment Share on other sites More sharing options...
LostKobrakai Posted August 18, 2015 Share Posted August 18, 2015 Seems like it, but there are few other unresolved ones in that function as well right now. 1 Link to comment Share on other sites More sharing options...
ryan Posted August 19, 2015 Share Posted August 19, 2015 @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); 2 Link to comment Share on other sites More sharing options...
ro-bo Posted August 20, 2015 Author Share Posted August 20, 2015 Wonderful! Thanks Ryan, thank you LostKobrakai! Link to comment Share on other sites More sharing options...
theoretic Posted December 26, 2017 Share Posted December 26, 2017 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 More sharing options...
adrian Posted December 26, 2017 Share Posted December 26, 2017 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 In the Tracy Console panel (again if you are viewing the field in the admin), you can do: 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. 3 Link to comment Share on other sites More sharing options...
theoretic Posted December 26, 2017 Share Posted December 26, 2017 Thanks Adrian! Works like a charm. Maybe this deserves to be added to docs. 1 Link to comment Share on other sites More sharing options...
adrian Posted January 16, 2018 Share Posted January 16, 2018 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 More sharing options...
Robin S Posted January 16, 2018 Share Posted January 16, 2018 1 hour ago, adrian said: I am not convinced this really works Yeah, it's not a solution. There is an open GitHub issue for this: https://github.com/processwire/processwire-issues/issues/349 1 Link to comment Share on other sites More sharing options...
Robin S Posted January 16, 2018 Share Posted January 16, 2018 @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. 1 Link to comment Share on other sites More sharing options...
adrian Posted January 16, 2018 Share Posted January 16, 2018 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; 1 Link to comment Share on other sites More sharing options...
Robin S Posted January 16, 2018 Share Posted January 16, 2018 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; }); 1 Link to comment Share on other sites More sharing options...
adrian Posted January 16, 2018 Share Posted January 16, 2018 1 minute ago, Robin S said: This approach works for the backend too. Yes of course - nice one Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now