Git Product home page Git Product logo

Comments (3)

jrbarnard avatar jrbarnard commented on September 15, 2024 2

@domhaas The code I've got is in a private repo but here is a rough rewritten version:

In reality I have a ConstraintTransformer that handles the job of identifying the type of Constraint and schema and what transformation to do to keep it cleaner and easier to test.

class FormConstraintExtension implements \Limenius\Liform\Transformer\ExtensionInterface
{
    public function apply(\Symfony\Component\Form\FormInterface $form, array $schema)
    {
        // Get the constraints set directly against the form / field
        $constraints = $form->getConfig()->getOption('constraints', []);

        // Loop the form constraints
        foreach ($constraints as $constraint) {
            // Apply the schema transformation
            // Single example of a count constraint on an array
            // Add more as needed
            if ($constraint instanceof \Symfony\Component\Validator\Constraints\Count && $schema['type'] === 'array') {
                $schema['minItems'] = $constraint->min;
                $schema['maxItems'] = $constraint->max;
            }
        }

        return $schema;
    }
}

This extension would need to be registered as in the docs (for symfony framework: Symfony\Component\Validator\Constraints\Count)

I hope that helps

from liform.

jrbarnard avatar jrbarnard commented on September 15, 2024

I got around this by having a ConstraintExtension that will loop over the set constraints and run them through custom constraint -> schema transformers that I've written.
It looks like the underlying ValidationGuesser stuff doesn't work with Form Type constraints.

I'd like to see a system in core that would support this with default transformers.

For instance:

  • Define Constraint Transformers as you do with extensions (tagging services) so they get loaded
  • As part of the Abstract Transformer (before ->applyExtensions()) call applyConstraints() which will loop the set constraints on the form and locate & apply the individual Transformer classes for each Constraint updating the Schema.

We'd need a bunch of default transformers, e.g LengthTransformer min / max apply to string types, minLength and maxLength keys.

I'd be happy to do a PR with a few initial transformers which could then be added to in the future.

from liform.

domhaas avatar domhaas commented on September 15, 2024

I got around this by having a ConstraintExtension that will loop over the set constraints and run them through custom constraint -> schema transformers that I've written.
It looks like the underlying ValidationGuesser stuff doesn't work with Form Type constraints.

I'd like to see a system in core that would support this with default transformers.

For instance:

* Define Constraint Transformers as you do with extensions (tagging services) so they get loaded

* As part of the Abstract Transformer (before ->applyExtensions()) call applyConstraints() which will loop the set constraints on the form and locate & apply the individual Transformer classes for each Constraint updating the Schema.

We'd need a bunch of default transformers, e.g LengthTransformer min / max apply to string types, minLength and maxLength keys.

I'd be happy to do a PR with a few initial transformers which could then be added to in the future.

@jrbarnard I'm currently stuck on the the same point. I'm not using annotation-constraints cause of trying to keep the entities generic.
Do you got some code to share for your constraint-extension? Thank you in advance.

Btw, this extension is really helpful. I'm using it together with the react-jsonschema-form, but needed to make some customizations. Hoping to find time to share my changes.

from liform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.