Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Symfony Json Form Laravel Package

precision-soft/symfony-json-form

A Symfony package for working with JSON-driven forms, helping you define, validate, and process form data from JSON schemas within Symfony applications. It integrates with Symfony’s Form component and offers a lightweight way to handle JSON form configurations.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install the package via Composer (PHP) and include the React assets if you need the frontend half.
  2. Start with .example/ – a runnable product editor that demonstrates every element type, three form services, a value object built through context hooks, and React rendering with both formV1 and formV2. Read .example/README.md first.
  3. Run the example validation with .dev/validate/all.sh --example to see the full flow.
  4. Your first use case: create an AbstractFormService subclass for a simple form (e.g., a product with name and price), define a DTO, and wire handleRequest() to populate it from a request and render() to output JSON for the React components.

Implementation Patterns

  • Form services – Extend AbstractFormService. Override hasJsonBody() to control JSON detection (defaults to inspecting the request content type) and constructDto() to customise DTO instantiation.
  • Request handling – handleRequest() decodes JSON bodies and denormalises them with the correct format, so JavaScript clients sending whole numbers (e.g. 150 for a float property) work out of the box. GET and form-encoded requests are still denormalised without a format.
  • Rendering – render() produces the JSON consumed by the React components. It accepts a DTO instance or a subclass of the expected DTO (since v1.3.0 the guard uses instanceof). If the DTO cannot be constructed without arguments, it throws the package’s Exception with formName and dtoClass context, wrapping the original ArgumentCountError.
  • Context hooks – Use them to build value objects (as shown in .example/) when your form data needs richer types than scalars.
  • React frontend – Import types with import type because assets/react/tsconfig.json sets verbatimModuleSyntax. The example renders the same JSON with both formV1 and formV2, deriving initial values from it — copy that pattern for your own forms.
  • Testing – The package’s own suite uses precision-soft/symfony-phpunit (AbstractTestCase, MockDto) and a Mockery double of the Symfony Serializer. If you write similar tests, mirror that setup; phpstan/phpstan-mockery is available as a dev dependency.

Gotchas and Tips

  • JSON body detection – If your client sends a different Content-Type (e.g. application/vnd.api+json), override hasJsonBody(). When JSON is detected, the serializer receives the json format; otherwise it gets none — this matters for numeric type coercion.
  • DTO subclasses – handleRequest() and render() now accept subclasses via instanceof. A DTO of an unrelated class still raises the original exception with the same context. Pass a subclass only when you intentionally want to populate in place.
  • Unbuildable DTOs – render() wraps constructor errors in the package Exception. Catch it to surface a clear message (formName, dtoClass) instead of a raw ArgumentCountError.
  • React type imports – With verbatimModuleSyntax enabled, a type-only import written without import type will fail the typecheck. Also ensure your bundler elides unused imports if you compile the sources yourself.
  • Example is export-ignored – It won’t be present in a production install. Use it from the repository as a reference; don’t rely on it at runtime.
  • Dev dependencies – The test suite relies on precision-soft/symfony-phpunit v3.7.0 and phpstan/phpstan-mockery. These are only needed for development; they are not runtime requirements.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
ably/pubsub-server
mstfkhazaal/defender
mfonte/base62x
karnoweb/metapilot
tobya/bcsapiwrapper
swalker2/cpanel
vish4395/laravel-translation-manager
morningtrain/economic
teksite/systeminfo-laravel
atomcms/voting
run6/weather
siokas/laravelembeddirectives
adamlien/pkuuids
gilsonsouza/lara-doc-generator
manelgavalda/enrollment_mobile
unabomber/timezone
delatbabel/contacts
astritzeqiri/g-recaptcha
kevinongko/laravel-genode
developeruz/laravel-analytics