Possible to extract common fields?
Just wondering if i'm doing it right, or if there is a way to optimize it somehow. I feel it's a lot of repeating. any suggestions are welcome.
i have a form where i put each day in a week in a tab. so the tabs are named 'Monday', 'Tuesday' and so on.
Under each of these tabs, i'm adding data for that day.
This code is the same for each day, except the tab name, badge, repeater make() and relationship query.
Would it be possible to extract the common parts of this somehow? hope you understand what i'm asking for.
Solution:Jump to solution
In my case, I created a Trait to share between a few resources, and in the trait are static methods like
getAuthorNameField()
or getMetaSection()
to easily call those methods when defining my form schema in multiple resources. This keeps things consistent, and I can re-order or keep/exclude things in various resources according to other requirements, without making it hard to maintain.
Thus my form(Form $form)
method simply defines the Grid and Sections schema, and then inside the Sections I call the various getFoo()
methods from my trait. In some cases I call complete sections via a getFooSection()
method in my trait, where I always use the same form elements when using that section.
I suppose you could pass methods to those functions too, to may them slightly dynamic if needed. In my case they all relate to the same Model, so don't require anything dynamic at the per-field level.
...2 Replies
Solution
In my case, I created a Trait to share between a few resources, and in the trait are static methods like
getAuthorNameField()
or getMetaSection()
to easily call those methods when defining my form schema in multiple resources. This keeps things consistent, and I can re-order or keep/exclude things in various resources according to other requirements, without making it hard to maintain.
Thus my form(Form $form)
method simply defines the Grid and Sections schema, and then inside the Sections I call the various getFoo()
methods from my trait. In some cases I call complete sections via a getFooSection()
method in my trait, where I always use the same form elements when using that section.
I suppose you could pass methods to those functions too, to may them slightly dynamic if needed. In my case they all relate to the same Model, so don't require anything dynamic at the per-field level.
I suppose you could do similar as I did with Sections if you had common things per "Day".
And I suppose you could have some of the functions in the trait call other functions to render a group of things based on various criteria.
And maybe a Trait is too rigid, and another kind of class would be fine for your needs.
There are lots of options you could explore.Thanks! I'll try that out! 🙂
Just wanted to give you a feedback. Using trait was exactly what I needed. This opens a new world and ideas for me. Thank you!