Skip to content

Migrate demoextrafield extra-field validation to Symfony constraints#250

Merged
jolelievre merged 2 commits into
PrestaShop:masterfrom
jolelievre:demoextrafield-constraint-validation
Jun 24, 2026
Merged

Migrate demoextrafield extra-field validation to Symfony constraints#250
jolelievre merged 2 commits into
PrestaShop:masterfrom
jolelievre:demoextrafield-constraint-validation

Conversation

@jolelievre

Copy link
Copy Markdown
Contributor

Description

Migrates the demoextrafield example module from the legacy single validator: 'isXxx' string to Symfony Validator constraints (constraints: [...]), matching the core change in PrestaShop/PrestaShop#41806.

Each registerExtraProperty(...) definition now passes real Symfony\Component\Validator\Constraint objects:

  • video_link (LANG) is the showcase for both multilang validation styles on one field:
    • new DefaultLanguage(allowNull: true, fieldName: 'Video link') — whole-array: if any language is filled, the default language must be too;
    • new Assert\All([new Assert\Url()]) — per-language: each language value must be a valid URL.
  • custom_date (SHOP) → new Assert\Date().
  • is_dangerous (COMMON, SwitchType) → no constraints — demonstrates that validation is opt-in (a switch always yields a valid 0/1).
  • date_last_seen (COMMON) stays read-only, auto-updated by the FO hook.

hookDisplayFooterProduct is updated to load the product in a single language (new Product($productId, false, $languageId) — note langId is Product's 3rd ctor argument) and update date_last_seen (COMMON) plus, only when it's non-empty, the per-language video_link via ObjectModel::update() — exercising constraint validation in the front-office legacy container.

Depends on

How to test

  1. Check out the core branch from Replace legacy extra-property validation with Symfony Validator constraints PrestaShop#41806 on a 9.2 dev shop.
  2. Install demoextrafield; open a product form.
  3. Video link: invalid URL in any language → per-language URL error; fill a non-default language but leave the default empty → DefaultLanguage error; empty everywhere → passes; valid → persists.
  4. View the product on the front office → footer hook updates date_last_seen and (when set) the current language's video_link.

Replace the legacy validator: 'isXxx' strings with constraints: [...] of real
Symfony Constraint objects, matching the core constraint-based validation.

- video_link (LANG): DefaultLanguage (whole-array) + All([Url]) (per-language)
- custom_date (SHOP): Assert\Date
- is_dangerous (COMMON, SwitchType): no constraints (validation is opt-in)
- hookDisplayFooterProduct loads the product in one language
  (langId is Product's 3rd ctor arg) and updates date_last_seen plus the
  per-language video_link only when it is non-empty
@jolelievre jolelievre force-pushed the demoextrafield-constraint-validation branch from d1017bc to 7aa5505 Compare June 23, 2026 12:20
- Rename the formRequired constructor argument to required, matching the renamed
  ExtraPropertyDefinition flag (now shared by the BO form and the Admin API).
- Drop allowNull on video_link's DefaultLanguage so the whole-array validation is
  actually exercised: an all-empty submit no longer passes by default.
@jolelievre jolelievre marked this pull request as ready for review June 24, 2026 09:36
@jolelievre jolelievre merged commit 69ba40a into PrestaShop:master Jun 24, 2026
@jolelievre jolelievre deleted the demoextrafield-constraint-validation branch June 24, 2026 09:36
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