Crud: Allows_null should be overridable on all select fields

Created on 13 Nov 2020  ·  5Comments  ·  Source: Laravel-Backpack/CRUD

Feature Request

What's the feature you think Backpack should have?

The SELECT field type doesn't allows_null to be specified. It only relies on DB column being nullable.
But on fake fields, it is always "true" (regarding comment in #1856 ).
I think we should get the allows_null attribute the priority over database setting : if the dev has set it, it means that he has an idea and needs some special behavior ;)

Anyways, in a "repeatable" context, the select2 field might not be a DB column of a table...

Have you already implemented a prototype solution, for your own project?

Not yet, but should be easy to do !

Do you see this as a core feature or an add-on?

Core

Minor Bug

All 5 comments

@LemarinelNet In my opinion selects should always have the null option, and be required or not. This way you would force the end users to open the select and view all options, some times end users may assume the selected option is correct without checking others.

Anyway, I agree with you, developers have the final word, so if they want to force "allows_null" to false, even on a nullable column, is up to them 🤷‍♂️

Lets hear the opinion of the relationship fields master @pxpm

Yes that's also true, and this leads me to another problem : on repeatable fields, I didn't found a reliable way to set "required" on a field !
Any tips ?

Totally agree with the idea. Nice feature, brings consistency and predictability to select fields.

@LemarinelNet required on repeatable can be done from the FormRequest, but it's a bit more complicated. There's an example for making some fields required inside the repeatable field in our demo. Check out https://github.com/Laravel-Backpack/demo/blob/master/app/Http/Requests/DummyRequest.php#L26-L64 for the exact implementation. It's not _perfect_. As far as I remember too all repeatable subfields show up red upon error. I'd love to fix that in a future version. But it works... It doesn't takes the user back to properly fill in the form.

That being said, we should probably make a distinction here:

  • allows_null is NOT validation; it's just a toggle used inside the blade file, to allow the developer to decide whether or not to show an empty <option> in selects; the actual validation should be done in the FormRequest or CrudController;
  • required is validation;

Just making sure 😬

Hey guys.

You are right, during the last months there have been some improvements on some fields that did not make it to all fields. Relationship fields already have that option and I 100% agree that it's developer option in the end to make the field nullable or non-nullable, we in backpack just want to do the best from our side.

I'v created PR #3334 to add the null overriding ability and database checks to the fields that use them.

Best,
Pedro

Great! Thanks a lot @pxpm . The PR looks great, so let's move the conversation to PR https://github.com/Laravel-Backpack/CRUD/pull/3334 to make steps towards a merge.

Cheers! Thanks for bringing this up @LemarinelNet .

Was this page helpful?
0 / 5 - 0 ratings