nova-flexible-content compatibility and more #9
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.
Hello, in one of my projects I use whitecube/nova-flexible-content which saves the repeatable fields in a json. The problem is that nova-items-field requires an already-parsed json and since this is not the case with flexible-content it does not work.
To solve this, I've added a check on the type of the field value and if it's a string it parses it.
With nova-flexible-content there is also another problem: you don't know exactly the attribute name since it's prefixed by a group key generated internally by the library (since the field is repeatable the prefix is used to identify exactly the instance of the field).
So, I've changed the definition of the rules to allow to skip the attribute name.
For example
is equal to
This is handled during the validation, so with nova-flexible-content it handles correctly the attribute name with the randomly generated prefix.
I've also updated the readme with this example.
And, as you can see in the example, I've also managed to allow validation on the array to check if there are at least or at most N items etc.
There was also another problem with the names of the attributes containing dots (e.g.,
flexible_input.0.attributes.x_conditions
), because the validator expected an array due to the dots in the name while instead in the input the attribute name contained the dots in the key, so I've simply swapped.
with->
during validation and then reverted the replacement.As a bonus, I've also changed how the details view is presented to show the items as a list since, in my opinion, makes more sense
![image](https://user-images.githubusercontent.com/28984898/220978968-e23c71a2-f974-4267-86b1-3905a3c40a0e.png)
In general, these changes are not interfering with a normal usage of the library without nova-flexible-content, with the only exception that now it works even if you don't apply the cast
'attribute' => 'array'
on the model, but I think that's not a bad thing, and now you can avoid to repeat the attribute name on the rules (without breaking the previous behaviour), which I think is also a good thing.Probably, these changes allow also to use this component with other libraries that manipulates the fields in a json-format, like translatable etc, but I haven't tested it.