Skip to content

fix: keep a control's typed or picked value a String in event data - #103

Merged
AdamMusa merged 10 commits into
mainfrom
fix/event-data-keeps-strings
Nov 20, 2026
Merged

AdamMusa merged 10 commits into
mainfrom
fix/event-data-keeps-strings

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Fixes #96

What changed

Ruflet::Event#parse_data ran JSON.parse on every String payload. The client sends what the user typed or picked as a plain String, right after it syncs the same String into the control with update_control (textfield.dart, code_editor.dart, material_dropdown.dart, radio_group.dart). So when the user typed 123, 2.50, null or [1], the handler got 123, 2.5, nil or [1], and the change event wrote that value over props["value"].

Now, in packages/ruflet_core/lib/ruflet_ui/ruflet/event.rb:

  • A String payload that equals the control's synced props["value"] stays a String.
  • One exception keeps today's decoded value. It applies on a Dropdown or a RadioGroup, when the payload decodes to a non-String that one of the control's own choices declares. The renderer reads choices with get<String>, so an Integer key 2 comes back as "2" and the handler still gets 2.
    • A choice is read the way the renderers read it: an option's key, else its text, else its wire id, and a radio's value.
    • A RadioGroup's choices are all the radios below it, including radios inside a Form, a container, a field's suffix or a label. radio_group.dart wraps its content in a RadioGroup<String>, and each of those radios joins it. Only the radios of a nested RadioGroup and the options of a nested Dropdown are left out.
  • SearchBar change/submit and Dropdown text_change always carry the typed text. material_search_bar.dart syncs the text with its capitalization applied but sends it as typed.
  • Every other String payload decodes as before.

In form_control.rb, a Form now relays its field's already-decoded event through the new public Event#relayed, instead of decoding raw_data a second time.

The new code uses only String, Hash and Array methods, so it runs the same on the embedded VM.

Changes since the last review:

  • Integer radios inside a Form, a TextField suffix or a Checkbox label within a RadioGroup reach the handler as Integers again. The last head left out every form field and Form, so radio_group(content: form(controls: [radio(value: 1), radio(value: 2)])) gave "2", where main gives 2.
  • I did not skip a nested Dropdown completely, which was the suggested fix. material_dropdown.dart builds its label and leading_icon inside the group, so a radio there joins the group too. Only its options are skipped.
  • New tests:
    • a RadioGroup containing a Form, a container, a TextField suffix, a Checkbox label and a Dropdown label;
    • the Form case with real MessagePack frames, on both receivers;
    • the Form case in the VM harness.
  • The Form relay test now checks the relayed event's name, control and target.
  • The test comments now say that only a DropdownM2 reports an option by its wire id.
  • I merged main up to 27e2ffa8.

How verified

The final head is 7fd8d07c, with main f034e99b merged in. The PR's diff against main is unchanged. The suites, the H1 goldens and the embedded VM below ran on 7fd8d07c. The "Against main's code", seed and mutation results come from 7b2f07c1, whose event.rb, form_control.rb and four test files are identical to the final head's.

Regression tests. Every file passes on the head and fails against origin/main's event.rb and form_control.rb:

Test Head Against main's code
packages/ruflet_core/test/event_client_value_test.rb 17 runs, 0 failures 11 failures, 1 error
packages/ruflet_server/test/event_client_value_wire_test.rb (real MessagePack frames) 8 runs, 0 failures 6 failures
packages/ruflet_rails/test/erb_form_typed_text_test.rb passes posts {"zip"=>12345, "price"=>19.9, "note"=>"", "tags"=>[1], "qty"=>2, "plan"=>20}
tools/embedded_vm_harness/tests/event_client_value_test.rb (CRuby) 18 checks pass 10 fail
  • With the last head's event.rb (0917444f), the core file has 1 failure, the server file 2 and the harness 1, all on radios inside a Form.

  • The core file passes on seeds 1 to 5. The core and server files and the harness also pass on Ruby 3.4.5.

  • Mutations of event.rb and form_control.rb. Each of these fails at least one of the new files:

    • the suggested fix (skipping a nested Dropdown completely);
    • not skipping a nested RadioGroup; keeping, or skipping all props of, a nested Dropdown;
    • not walking a nested control's children, or its props;
    • not setting the relayed event's control or target; reverting the Form relay;
    • dropping RadioGroup, or Dropdown, from the owners; dropping the declared-choice path, the text-event check, either text-event entry, the Option entry, Option's text or _i fallback, or the owner check;
    • putting back the "2"-over-2 guard.

    Two mutations survive, and both change nothing. Not renaming the relayed event: a Form relays change as change and submit as submit. Dropping text for DropdownOption: DropdownOptionControl already copies text into key.

Existing suites, one process per file, Ruby 4.0.5:

Suite Files Runs Failures, errors
packages/ruflet_core 205 2081 0, 0 (1 skip, in dsl_presented_controls_test.rb, which this PR does not touch)
packages/ruflet_server 15 64 0, 0
packages/ruflet_rails (bundle exec) 18 297 0, 0
packages/ruflet 22 707 0, 0

H1 goldens:

  • cd packages/ruflet && ruby test/compat/compat.rb --jobs 4 checked 314 goldens and exited 0.
  • ruby -Ilib -Itest test/compat_goldens_test.rb gave 322 runs with 0 failures.

Embedded VM. I built an arm64 embedded_mruby from the final head 7fd8d07c (mruby 449627d, ruby_runtime/vm/build_config/desktop_macos.rb). Its framework contains this PR's choice_values and main's newest page.rb code. The harness tests client_state_readers, icon_deferral, scoped_helper, ruflet_gem and ruflet_record_gem also pass on it.

  • All 18 checks pass.
  • With the last head's event.rb, 1 check fails (the radio inside a Form). With origin/main's event.rb, 11 fail. One example: the VM's JSON turns "007" into 7.

Behavior changes

This is a bug fix (D6). No app code needs to change.

  • Typed text reaches the handler as the String the user typed, even when it looks like JSON: "123", "2.50", " 42", "1e3", "true", "null", "[1]", "{...}", "\"q\"".
    • This covers change on TextField, CupertinoTextField, AutoComplete and CodeEditor, TextField submit, SearchBar change and submit (with or without capitalization), and Dropdown text_change.
    • Before, these arrived as a number, boolean, nil, Array, Hash or unquoted String, while "007" or "12a" stayed Strings. Code that did arithmetic on them already raised on the first letter and on an emptied field ("").
    • The CodeEditor docs already describe this data as "the new value".
  • A picked choice declared as a String stays that String. This covers String option keys, String text-only options and String radio values: "2" used to arrive as 2, and "null" as nil.
    • Symbol keys come back as their String, also when they look like a number. :"2" now gives "2" instead of 2, the same way :small already gave "small".
  • Where the String shows up: e.data, e.value, Form on_change/on_submit data, and props["value"] after a change or select. So form.values, validators and a later render see the String too.
  • ERB-to-native forms (ruflet_rails) post typed text and the value picked in <select> and <radio-group> as Strings, the way a browser posts them. They now send {"qty"=>"2", "plan"=>"20"}, where before they sent {"qty"=>2, "plan"=>20}.
  • Not changed:
    • Integer, Float and boolean option keys and radio values, wherever the radio sits inside its RadioGroup. This includes Ruflet.option keys, Integer text-only options, and options that declare both 2 and "2": the client sends "2" for either, and the handler gets 2.
    • A DropdownM2 option with neither key nor text, which material_dropdown_m2.dart reports by its wire id. (Ruflet.dropdown_m2 goes on the wire as a Dropdown, whose renderer leaves such an option out.)
    • Every payload that is not the control's own value: a PopupMenuButton item id, JSON the control did not sync, maps, booleans and numbers.

Docs follow-up (ruflet_site):

  • app/content/docs/events_and_state.md:34 says "JSON strings become Ruby arrays or hashes". It needs this exception added.
  • The next script/sync_ruflet_api run will add Event#relayed to config/runtime_catalog.json.
  • Docs CI pins ruflet 51bea9b1 (.github/workflows/ci.yml:93), so merging this PR does not break the docs build.

Found, not fixed here: the block forms Ruflet.dropdown { dropdown_option(key: 1) } and Ruflet.radio_group { column { radio(value: 1) } } put no options or radios on the wire, so there is nothing to pick. That is a separate bug.

Event#parse_data ran JSON.parse on every String payload. The client sends
a TextField's text, a Dropdown's option key and a RadioGroup's value as
plain Strings, right after syncing the same String into the control
(update_control). When the text looked like JSON, handlers got another
value of another type ("123" became 123, "2.50" became 2.5, "null" became
nil, "[1]" became [1]), and change events wrote it over props["value"].

A String payload that equals the control's own synced value or text now
stays a String. The decoded value is kept only when it matches a
non-String option key or radio value the app declared: the renderer
sends Integer key 2 back as "2", and handlers keep getting 2. Payloads
that are not the control's value decode as before. A Form relays its
field's decoded event instead of decoding raw_data again.

Fixes #96
The compiled framework does not load JSON by itself, so Ruflet::Event
only decodes String payloads on the VM once something has required
"json". Require it so the check exercises the decoding path.
…t a String

Review follow-ups for the event value fix:

- Only a Dropdown or a RadioGroup reports one of its choices as its
  value. A TextField holding a Radio or a Dropdown in a prop now reports
  the typed text like any other TextField.
- When choices declare both 2 and "2", the client sends "2" for either,
  so the handler gets 2, as before this branch.
- An option with neither key nor text is read by its wire id, as
  material_dropdown_m2.dart does, so that payload decodes as before.
- SearchBar change and submit, and Dropdown text_change, always carry
  the typed text: the search bar syncs the capitalized text but sends it
  as typed, and text typed into a dropdown can equal the picked key.
- The props["text"] match is gone; the Dropdown text_change entry covers
  the only control that synced its text before an event.

Tests pin Option keys and text, Integer text-only options, the wire id
fallback, the 2/"2" case, nested radios, CodeEditor, Symbol keys, the
capitalized SearchBar, and ERB select and radio-group params.
radio_group.dart wraps its content in a RadioGroup<String> that every Radio
below it joins, also one inside a Form, a field's suffix or a Checkbox's or
Dropdown's label. choice_values skipped those, so their Integer values
reached the handler as Strings. Only a nested RadioGroup owns its radios and
a nested Dropdown its options.

Tests also pin that a Form relays its field's event with itself as control
and target.
@AdamMusa
AdamMusa merged commit 9f2ca79 into main Nov 20, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

String event data is JSON-decoded: typed text, dropdown keys and radio values change type ("123" becomes 123, "null" becomes nil)

1 participant