Closed nebulousGirl closed 5 years ago
@nebulousGirl I'm hoping to do the same thing. I thought I had a workaround, but it ended up falling flat.
What I was planning to do is use the setAttribute method to add an attribute to the repeater of 'data-limit'. Then I'd add a new click handler to the "Add New" button that would fire before the standard TypeRocket handler that adds a new record in the repeater. It would grab that "data-limit" attribute, check the number of rows, and if it was at the limit, would pop up an alert and then call stopImmediatePropagation(). Not pretty, but should work.
Problem is, setAttribute doesn't do anything on a repeater field! It doesn't add the actual attribute on any of the wrapper elements that create the control. That seems like a bit of an oversight, but somewhat understandable given that there's not really an actual input anywhere for this particular field (which would normally be assigned the attribute).
The only other option I can think of to make this work is to create a new class that extends Repeater and replace the getString method to include the appropriate limit. Don't love that idea, though, as I'd hate to override the parent getString method if I don't have to.
I have a pretty good Validation "plugin" of sorts in an internal project at work, just have to figure out how to share it. Could possibly incorporate it into TR itself. I also have a Conditional Logic API that I'm planning on submitting via PR soon-ish.
(The Validation implementation we are working with includes the ability to set min/max instances of a module within a Page Builder, as well as min/max on repeaters.)
Hey @zackphilipps
I have updated the Validator class recently but I have not released it because it is not finished. https://github.com/TypeRocket/core/blob/a694793beafe21c2e222f957176f43f4b3683b1c/src/Utility/Validator.php#L144-L158
I'd be interested in seeing your conditional logic API as well. The sooner you submit the sooner I can look and give feedback.
Thanks, Kevin
@zackphilipps Could you share the Validator class? I will need those features when returning from the holidays and was planning on coding it from scratch, but would hate to reinvent the wheel.
@zackphilipps Ditto! Would love to see what you have.
@kevindees Can anyone submit code to review? I've created a Relationship Field class that I think would fit really well into TypeRocket. I'm embarrassingly uninitiated to git, so I'm not sure how to submit my code.
@eric-michel @nebulousGirl here is a small part of our validation implementation (the part I think you are most concerned with): https://github.com/TypeRocket/core/pull/24/files
This will only work on custom resources IF you have your own validation implementation on your controllers.
Example validation rule array:
$validation = [
'name' => 'required',
// min, max, or size, then number, then sanitized component name
'builder' => 'callback:tr_valid_compcount:max,1,component-name',
// repeater within a builder
'builder.*.component-name.repeater-name' => 'callback:tr_valid_repcount:min,2|callback:tr_valid_repcount:max,4',
];
@kevindees I know it's against your philosophy to perform validation on post meta:
https://typerocket.com/docs/v3/validator/#section-about-validation
Validation is best done for custom resources. Post types fields should not be required but should be expected to be empty just as all other post type fields are in WordPress.
However, when building a CMS for a client we've found it prudent to enforce some content management rules, such as:
So that is the itch we've scratched. Wanted to know your thoughts on possibly incorporating post meta validation into TR core. It would require adding some JS to prevent saving until all fields are valid.
@zackphilipps Thanks Zack, and happy new year! I look forward to trying it out!
@eric-michel @nebulousGirl @zackphilipps
This is now in v4
https://typerocket.com/docs/v4/field-types/#section-repeaters-limit-items
I need to limit the number of items added to a repeater. I haven't seen anything in the docs to get this done. Is there a way to do this?