Getting previously selected values in form.php
Permalink
In my quest on developing a weather block I've run into a question I can't find an answer for in the documentation (I probably use the wrong search terms).
When the user wants to edit a block I want the previous set values of dropdown menus to be read from the database.
In the code below (form.php and snippet from controller.php) the first items in the dropdown menus are selected when opening a form to edit a previously added block.
Question: How do I read information from the db and make this the selected item in a dropdown?
Complete block is here:
https://github.com/o3jvind/cool_weather...
When the user wants to edit a block I want the previous set values of dropdown menus to be read from the database.
In the code below (form.php and snippet from controller.php) the first items in the dropdown menus are selected when opening a form to edit a previously added block.
Question: How do I read information from the db and make this the selected item in a dropdown?
Complete block is here:
https://github.com/o3jvind/cool_weather...
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); if (!isset($api)) { $api = ''; } if (!isset($id)) { $id = '2624652'; } if (!isset($units)) { $units = 'metric'; } if (!isset($language)) { $language = 'en'; } if (!isset($header)) {
Viewing 15 lines of 110 lines. View entire code block.
Thanks a lot – works perfectly.
Now I'll see if I can understand the Form Helper stuff 🙃
Now I'll see if I can understand the Form Helper stuff 🙃
I get your point :-)
Rewrote the form getting the benefits of Form Helper.
Rewrote the form getting the benefits of Form Helper.
That's great that you got it working. Couple other things that you could do, you can add 'required' => 'required' to your form fields to make them required using HTML5, that way you know that you will have the values on the edit page and you don't have to worry about setting defaults. So then you can add an add function to your block controller and set your default values there like this
And if you wanted to clean up your select elements you could add another function in your controller called something like getSelectOptions() and then call that function from your add and edit functions, like this
And then in your block form.php you would just have
Just some suggestions, what you have will certainly work, these things will just make your code a little cleaner.
public function add() { $this->set('units', 'metric'); }
And if you wanted to clean up your select elements you could add another function in your controller called something like getSelectOptions() and then call that function from your add and edit functions, like this
public function add() { $this->getSelectOptions(); } public function edit() { $this->getSelectOptions(); } public function getSelectOptions() { $this->set('unitOptions', array('imperial' => t('Imperial'), 'metric' => t('Metric'))); }
And then in your block form.php you would just have
Just some suggestions, what you have will certainly work, these things will just make your code a little cleaner.
Thank you very much for your kind advices 😊
They help me a lot and I hope others will benefit from them too.
They help me a lot and I hope others will benefit from them too.
With the same variable name in all of the options, instead it should be
I would also strongly suggest that you look at the Form Helper, it makes a lot of this much easier than what you're doing: https://documentation.concrete5.org/tutorials/how-to-use-the-form-wi...