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...

<?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)) {


public function save($data)
    {
        parent::save($data);
        $data['units'] = intval($data['units']);
        $data['language'] = intval($data['language']);
    }

 
hutman replied on at Permalink Best Answer Reply
hutman
In your options you have something like this

<option value="metric" <?php echo($this->controller->displayTag == "metric" ? "selected" : "")?>><?=t('Metric')?></option>

With the same variable name in all of the options, instead it should be
<option value="metric" <?php echo($units == "metric" ? "selected" : "")?>><?=t('Metric')?></option>

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...
O3JVIND replied on at Permalink Reply
Thanks a lot – works perfectly.

Now I'll see if I can understand the Form Helper stuff 🙃
O3JVIND replied on at Permalink Reply
I get your point :-)
Rewrote the form getting the benefits of Form Helper.

<?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)) {
hutman replied on at Permalink Reply
hutman
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

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
<div class="form-group">
   <label class="control-label">Units</label>
   <?php echo $form->select('units', $unitOptions, $units, array('style' => 'width: 400px;')); ?>
</div>

Just some suggestions, what you have will certainly work, these things will just make your code a little cleaner.
O3JVIND replied on at Permalink Reply
Thank you very much for your kind advices 😊
They help me a lot and I hope others will benefit from them too.