Skip to content

[6.x] Forms 2: Move fake and preview values into fieldtypes - #15840

Merged
jasonvarga merged 7 commits into
forms-2from
forms-2-fieldtype-fake-values
Nov 10, 2026
Merged

jasonvarga merged 7 commits into
forms-2from
forms-2-fieldtype-fake-values

Conversation

@jasonvarga

@jasonvarga jasonvarga commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Each fieldtype now provides its own fake value and its own email preview value. Two new hooks on the base Fieldtype:

  • previewValue(): mixed returns a fixed sample value for previews that don't have a real value, such as the email connection preview. It may run in production, so it must not use Faker. The base returns null.
  • fakeValue(\Faker\Generator $faker): mixed returns a realistic random sample value, such as for fake submissions. It's only called locally. The base returns previewValue(), so an addon fieldtype that implements previewValue() gets both. Overriding fakeValue() is optional.

Both hooks are written for general use, not just these two features. For example, they could later fill the form builder's fields with sample values (previewValue() in production, fakeValue() locally). Both return the processed (stored) shape, so a CP consumer would pass the value through preProcess().

These are public API on a class every addon fieldtype extends, so the signatures need to be final before release.

Changes

  • These core fieldtypes implement both hooks: Text, Slug, Textarea, Markdown, Lists, Integer, Range, Floatval, OpinionScale, StarRating, Toggle, Date, Time, Link, Checkboxes, ImageChoice, Ranking, Dictionary and Group. Select, Radio, ButtonGroup and YesNo get them through HasSelectOptions. Text matches the field's handle and label (email, phone, name…) to pick both a fixed example value for previews and a Faker value for fakes. Hidden only implements previewValue(), which returns the field's default, or null. Revealer implements neither, because it never stores a value.
  • FakeSubmissionGenerator and PreviewSubmission now just loop over the fields and call the hooks.
  • Shared helpers live in a new Statamic\Fields\SampleValue class: fixed lorem ipsum text, option key normalisation, and picking a random set of option keys (which replaces Statamic\Forms\FieldOptions). Fieldtypes don't depend on Statamic\Forms classes for this.
  • Group builds its nested fields through FormField, so both form-typed and core-typed children work. This is the one remaining dependency on Forms. It's marked with a TODO and can switch to $this->fields() once the form Group fieldtype converts its nested fields to core fieldtypes.
  • The \Faker\Generator type hint is safe while Faker is in require-dev. PHP doesn't load a parameter's type until the method is called, and fakeValue() is only called locally.

Behaviour changes

  • Fakes no longer use field defaults. Previously a field with a default (other than a list of option-type fields) always got its default in fakes. Now every field's fake comes from fakeValue(). The exception is a hidden field, whose default is its real submitted value, so both fakes and the preview use it.
  • Previews show example values like a real submission would (Jane Doe, 555-0100, lorem ipsum) instead of the field's label in brackets ([Name]).
  • Text matching is stricter and more accurate, for both previews and fakes. It checks input_type, then the HTML autocomplete value (given-name, postal-code, address-level2…), then whole words in the handle and label instead of substrings. hotel, statement and page_title no longer match phone, state and job title. Labels with spaces like "First Name" match first_name, camelCase handles are split into words, and the rules are checked from most to least specific, so company_email is an email and company_name is a company.
  • Selects with multiple enabled now get an array of 1 to max_items option keys in fakes (up to 3 when unset), instead of a single key. Checkboxes, image choice and dictionary use the same helper, so checkboxes and multiple image choice now pick up to 3 options instead of 2.
  • Revealer fields no longer get text. They had no effect anyway, because Revealer never stores a value.
  • Otherwise generated values are the same as before. The other known data-validity issues in the generator (number bounds, numeric option keys) are tracked separately.

jasonvarga and others added 7 commits November 10, 2026 11:53
Adds previewValue() and fakeValue(\Faker\Generator $faker) hooks to the base Fieldtype. previewValue() is a deterministic, Faker-free placeholder for the email preview. fakeValue() defaults to previewValue(), so addon fieldtypes get both by implementing one. FakeSubmissionGenerator and PreviewSubmission now just loop over fields and call the hooks; the generator keeps the default-value handling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Moves the placeholder, option key and default-value helpers into Statamic\Fields\SampleValue so fieldtypes no longer depend on PreviewSubmission, FieldOptions or FakeSubmissionGenerator. Group still uses FormField until the form Group fieldtype converts its nested fields to core fieldtypes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fakes now always come from fakeValue(). The only field whose default is its real value is a hidden field, so Hidden's previewValue() returns its default, and its fakeValue() falls back to that. Revealer never stores a value, so it no longer implements either hook.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Previews now show values like a real submission would (Jane Doe, 555-0100, lorem ipsum) rather than the field's label in brackets. Text picks its example value with the same handle/label matching as fakes, which now treats spaces in labels as underscores so labels like "First Name" match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Text now picks its sample kind from input_type, then the autocomplete attribute, then whole words in the handle and label, instead of substrings. Fields like hotel, statement and page_title no longer match phone, state and job title. Rules are checked from most to least specific, so company_email is an email and company_name is a company.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Selects with multiple enabled now get between one and max_items keys (or up to three) instead of a single key. Checkboxes, image choice and dictionary share the same random-subset helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jasonvarga
jasonvarga merged commit 4e61286 into forms-2 Nov 10, 2026
63 checks passed
@jasonvarga
jasonvarga deleted the forms-2-fieldtype-fake-values branch November 10, 2026 22:02
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.

1 participant