Skip to content

Commit c5fbb4b

Browse files
committed
form select: loose equality testing for option values
the form component now considers numbers and their string representation as equal when comparing the `value` parameter and the values from the `options` parameter in dropdowns see #1002
1 parent 2b8c515 commit c5fbb4b

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
```
2323
- When a sql file is saved with the wrong character encoding (not UTF8), SQLPage now displays a helpful error messages that points to exactly where in the file the problem is.
2424
- More visual error messages: errors that occured before (such as file access issues) used to generate plain text messages that looked scary to non-technical users. All errors are now displayed nicely in the browser.
25+
- The form component now considers numbers and their string representation as equal when comparing the `value` parameter and the values from the `options` parameter in dropdowns. This makes it easier to use variables (which are always strings) in the value parameter in order to preserve a dropdown field value across page reloads. The following is now valid:
26+
- ```sql
27+
select 'form' as component;
28+
select
29+
'select' as type,
30+
true as create_new,
31+
true as dropdown,
32+
'2' as value, -- passed as text even if the option values are passed as integers
33+
'[{"label": "A", "value": 1}, {"label": "B", "value": 2}]' as options;
34+
```
2535

2636
## v0.36.1
2737
- Fix regression introduced in v0.36.0: PostgreSQL money values showed as 0.0

sqlpage/templates/form.handlebars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
>
105105
{{#if empty_option}}<option value="">{{empty_option}}</option>{{/if}}
106106
{{#each (parse_json options)}}
107-
<option value="{{value}}" {{#if (or (eq ../value value) selected)}}selected{{/if}}>{{label}}</option>
107+
<option value="{{value}}" {{#if (or (loose_eq ../value value) selected)}}selected{{/if}}>{{label}}</option>
108108
{{/each}}
109109
</select>
110110
{{else}}

src/template_helpers.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub fn register_all_helpers(h: &mut Handlebars<'_>, config: &AppConfig) {
3737
register_helper(h, "plus", plus_helper as HH);
3838
register_helper(h, "minus", minus_helper as HH);
3939
h.register_helper("sum", Box::new(sum_helper));
40+
register_helper(h, "loose_eq", loose_eq_helper as HH);
4041
register_helper(h, "starts_with", starts_with_helper as HH);
4142

4243
// to_array: convert a value to a single-element array. If the value is already an array, return it as-is.
@@ -472,6 +473,16 @@ fn sum_helper<'reg, 'rc>(
472473
Ok(())
473474
}
474475

476+
/// Compare two values loosely, i.e. treat all values as strings. (42 == "42")
477+
fn loose_eq_helper(a: &JsonValue, b: &JsonValue) -> JsonValue {
478+
match (a, b) {
479+
(JsonValue::String(a), JsonValue::String(b)) => a == b,
480+
(JsonValue::String(a), non_str) => a == &non_str.to_string(),
481+
(non_str, JsonValue::String(b)) => &non_str.to_string() == b,
482+
(a, b) => a == b,
483+
}
484+
.into()
485+
}
475486
/// Helper that returns the first argument with the given truthiness, or the last argument if none have it.
476487
/// Equivalent to a && b && c && ... if the truthiness is false,
477488
/// or a || b || c || ... if the truthiness is true.

0 commit comments

Comments
 (0)