feat(js): register constraints and transformers in a Svaroh namespace - #19
Merged
Merged
Conversation
Every constraint and every view transformer exported its class name as a separate property of "window", about thirty unprefixed globals for a library that only needs one name. They are now registered in a single "Svaroh" namespace object, and parseConstraints() and parseTransformers() resolve the class name the PHP side serialises there instead of on "window". The old global names are kept as deprecated aliases, and a class an application defines as a global is still resolved from the global scope, so custom constraints, custom transformers and Callback handlers written against them keep working. Fixes formapro#129
66Ton99
force-pushed
the
feat/issue-129-js-namespace
branch
from
August 24, 2026 19:38
dca52d2 to
4e956d8
Compare
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.
Fixes formapro#129 — "Add namespace in JS lib": Validation Constraints should not be in global scope.
What was broken
Every constraint and every view transformer ended with a line like
which is about thirty unprefixed names on
windowfor a library that needs one.Root cause
Those globals are not decorative.
JsFormValidatorFactoryserialises the PHP classname of each constraint (
getConstraintsData()) and of each transformer(
parseTransformers()) into the form model, and the browser side instantiated theclass by that name read straight off
window:So the global scope was the class registry. Moving the classes without moving the
lookup would break validation at runtime.
What changed
src/Resources/public/js/namespace.jscreates a single namespace object withconstraintsandtransformersregistries. It is merged into an existingwindow.Svarohrather than overwriting it.constraints/index.jsandtransformers/index.jsnow import the default exportsand register them in that namespace under the name the PHP side serialises. The
pre-2.7
False,NullandTrueconstraint names are registered as aliases ofIsFalse,IsNullandIsTrue, matching what the modules already put onwindow.SvarohJsFormValidator.resolveClass()is the new single lookup point, used by bothparseConstraints()andparseTransformers(): the namespace first, the globalscope as a fallback.
doc/3_7.mdanddoc/3_8.mdnow register custom constraints and transformersin the namespace, and the README gained a "JavaScript Namespace" section.
Why
SvarohIt mirrors the
Svaroh\JsFormValidatorBundlePHP namespace — the JS side alreadycarries that rebrand in
SvarohJsFormValidator,SvarohJsFormErrorandSvarohJsFormElement, so a vendor-scopedSvarohobject reads as the same library.It is one short name for user code to type, and it is created by merging into
whatever
window.Svarohalready holds, so an application that owns aSvarohobject of its own is not clobbered.
Compatibility
This is deliberately not a breaking change. The extension story of this library is
global functions, so:
still assign
window.<GlobalName>, untouched by this PR;Callbackhandler an application defines as a globalis still found, because
resolveClass()falls back to the global scope.One behaviour does change: because the namespace is consulted first, assigning a
global with the name of a bundled class no longer overrides it. Replace a bundled
class through
Svaroh.constraints/Svaroh.transformersinstead. This is noted indoc/3_7.md.Follow-up (not this PR): drop the
window.<GlobalName>lines from the constraintand transformer modules, and drop the global-scope fallback in
resolveClass(), in arelease that can carry a BC break. The library's own core objects
(
SvarohJsFormValidator,SvarohJsBaseConstraint,SvarohJsFormError,SvarohJsFormElement) are still globals and could move under the namespace at thesame time.
Tests
jest: 34 suites / 347 tests → 35 suites / 400 tests, all green.composer test: 43 tests / 151 assertions.phpstan: clean.constraints/globals.test.jsandtransformers/globals.test.jsdefine the contractfor this surface, so they keep their name lists and now assert both halves of it:
each name is registered in the namespace, and the deprecated global points at the
very same constructor. Nothing was removed or weakened.
namespace.test.jscovers the lookup: a bundled constraint and a nestedtransformer chain are still instantiated after their globals are deleted (these fail
without the
resolveClass()change), an application class defined only as a global isstill instantiated, one registered only in the namespace is instantiated, and an
unknown name is still skipped.
Merge order
This branch is written to coexist with the bug-fix branches in flight: it does not
touch the individual constraint or transformer modules at all, and in
SvarohJsFormValidator.jsit only adds an import and rewrites the two lookup lines,none of which is near the hunks of formapro#59, formapro#61, formapro#67, formapro#72, formapro#81, formapro#87, formapro#90, formapro#120, formapro#126, formapro#135
or formapro#148.
git merge-treereports no conflict against any of them, and the mergedtree of this branch with formapro#148, formapro#90, formapro#59, formapro#72, formapro#81 and formapro#135 runs 467 Jest tests green.
Merging it last is still the safest order, but it should not require conflict
resolution.