Comments (3)
@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.
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.
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)
- Initial Values Normalizer produces StdClass instead of Array for collections HOT 1
- `minItems` keyword set in wrong place
- Still in Use? HOT 3
- CollectionType just render first row HOT 16
- Missing transformers
- Override order fields
- any specification about the form JSON schema? HOT 1
- maxLength is not working when Length annotation is used HOT 1
- why isRequire method of AbstractTransfomer is called successfully outside of the class it is in since it is protected? HOT 2
- [Feature] Use 'help' defined on form to render as description HOT 1
- [Feature] Add phpunit as dev requirement in composer HOT 1
- Symfony 5 compatibility HOT 1
- Notice: Undefined index: compound HOT 3
- [Update] symfony/form 4.4 - 5.0 HOT 4
- Any plans for conditional subschemas? HOT 1
- PHP8 HOT 2
- Error on form normalization HOT 1
- Is this project abandoned? HOT 4
- allow Symfony 6 HOT 4
- Call to undefined method Chess\WebBundle\Translation\Translator::transChoice() HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from liform.