Repository navigation
[6.x] Forms 2: Move fake and preview values into fieldtypes - #15840
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Each fieldtype now provides its own fake value and its own email preview value. Two new hooks on the base
Fieldtype:previewValue(): mixedreturns 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 returnsnull.fakeValue(\Faker\Generator $faker): mixedreturns a realistic random sample value, such as for fake submissions. It's only called locally. The base returnspreviewValue(), so an addon fieldtype that implementspreviewValue()gets both. OverridingfakeValue()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 throughpreProcess().These are public API on a class every addon fieldtype extends, so the signatures need to be final before release.
Changes
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 implementspreviewValue(), which returns the field's default, ornull. Revealer implements neither, because it never stores a value.FakeSubmissionGeneratorandPreviewSubmissionnow just loop over the fields and call the hooks.Statamic\Fields\SampleValueclass: fixed lorem ipsum text, option key normalisation, and picking a random set of option keys (which replacesStatamic\Forms\FieldOptions). Fieldtypes don't depend onStatamic\Formsclasses for this.Groupbuilds its nested fields throughFormField, 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.\Faker\Generatortype hint is safe while Faker is inrequire-dev. PHP doesn't load a parameter's type until the method is called, andfakeValue()is only called locally.Behaviour changes
fakeValue(). The exception is a hidden field, whose default is its real submitted value, so both fakes and the preview use it.Jane Doe,555-0100, lorem ipsum) instead of the field's label in brackets ([Name]).input_type, then the HTMLautocompletevalue (given-name,postal-code,address-level2…), then whole words in the handle and label instead of substrings.hotel,statementandpage_titleno longer match phone, state and job title. Labels with spaces like "First Name" matchfirst_name, camelCase handles are split into words, and the rules are checked from most to least specific, socompany_emailis an email andcompany_nameis a company.multipleenabled now get an array of 1 tomax_itemsoption 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.