Carbon-fields: Disable duplicated entries in complex fields

Created on 19 Sep 2016  路  7Comments  路  Source: htmlburger/carbon-fields

Hello,
I wonder if someone knows a quick solution to this problem. I really think, that disabling the creation of duplicated entries make sometimes huge sense in complex fields.

For instance, my current problem is to create product variants with multiple attributes. If the customer adds a size, he has the possibility to add a new size again or clone the existing one. This should not work...

screenshot 2016-09-19 20 43 17

My opinion is to add a new method called allow_duplicates, which accepts either true or false.

Field::make('complex', 'complex_example')
    ->allow_duplicates(false)
    ->add_fields('complex_1', array(
        //...
    ))
    ->add_fields('complex_2', array(
        //...
    ))

Maybe someone can give me a hint, where I have to modify the code to make this work.

Thank you,
Alexander Barton

feature

Most helpful comment

Why not just make set_max take an Array as second argument so usage would look like:

Field::make( 'complex', 'crb_stuff' )
    ->add_fields( 'foo', [ // Can only occur once (or not at all)
        Field::make( 'text', 'narf' ),
        Field::make( 'text', 'bla' ),
    ] )
    ->add_fields( 'bar', [ // Number only limited by total field limit
        Field::make( 'text', 'whatever' ),
    ] )
    ->set_max( 5, [ // Set total max to 5 and optionally specify max for each field
        'foo' => 1,
    ] )

The total maximum can still be set to -1 if you do not want to set a limit.
This is still not really a clean API for setting min and max values for each field type in a general way, but IMHO the cleanest way to address this issue in a way that fits in with the currently existing API.

Maybe the best way in the long run would be to tell the GroupField itself min and max values of how often it can and/or must occur. Not sure at what point best to pass those in though.

All 7 comments

I tried to implement a solution. Please look at the pull request 98: https://github.com/htmlburger/carbon-fields/pull/98

Nice idea.

It would also be great to allow specific repetitions to each group, rather than an option that forces 1-entry-per-group.

Thank you! What about a method, which can take a second optional argument like this?

Field::make('complex', 'complex_example')
    ->allow_duplicates('complex_1', false)
    ->add_fields('complex_1', array(
        //...
    ))
    ->add_fields('complex_2', array(
        //...
    ))

Another possible approach:

Field::make('complex', 'complex_example')
    ->group_limit( array(
        'complex_1' => 1, // allow only 1 group of this type
        'complex_2' => 2, // allow 2 groups of this type
    ) )
    // ->group_limit( 1 ) // when using no-named groups
    ->add_fields('complex_1', array(
        //...
    ))
    ->add_fields('complex_2', array(
        //...
    ))

What do you think guys?

@m1r0 Yeah, good idea. I would just rename it to limit_groups().

Another thing I want to add: If you are using multiple groups and use the method like this limit_groups(2), all groups will be limited to 2.

Why not just make set_max take an Array as second argument so usage would look like:

Field::make( 'complex', 'crb_stuff' )
    ->add_fields( 'foo', [ // Can only occur once (or not at all)
        Field::make( 'text', 'narf' ),
        Field::make( 'text', 'bla' ),
    ] )
    ->add_fields( 'bar', [ // Number only limited by total field limit
        Field::make( 'text', 'whatever' ),
    ] )
    ->set_max( 5, [ // Set total max to 5 and optionally specify max for each field
        'foo' => 1,
    ] )

The total maximum can still be set to -1 if you do not want to set a limit.
This is still not really a clean API for setting min and max values for each field type in a general way, but IMHO the cleanest way to address this issue in a way that fits in with the currently existing API.

Maybe the best way in the long run would be to tell the GroupField itself min and max values of how often it can and/or must occur. Not sure at what point best to pass those in though.

2.0.0 has been released which adds a ->set_duplicate_groups_allowed() method to complex fields.

If you consider having a numerical limit per group type an important feature feel free to post a separate feature request issue.

Was this page helpful?
0 / 5 - 0 ratings