Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SearchKit - Improve editable UX by not allowing required fields to be left blank #22358

Merged
merged 1 commit into from
Jan 28, 2022

Conversation

colemanw
Copy link
Member

@colemanw colemanw commented Jan 3, 2022

Overview

Fixes dev/report#92 by enforcing required fields during in-place and bulk editing in SearchKit.

Before

SearchKit would allow you to save required fields with blank values.

After

Saving a blank value for a custom field marked "required" is prevented in the inline-edit UI:
image

It's also prevented in the bulk-update action:
image

Works similarly for select, date, and numeric inputs.

Technical Details

Unlike the 'required' field property, which only determines if the API requires a value to Create, the new 'nullable' property tells a UI whether a field is allowed to be set to NULL in Create OR Update. This value is now documented in APIv4 getFields and is returned consistently for both core and custom fields.

SearchKit uses this property during in-place edit and bulk edit operations to determine whether a field can be left blank.

@civibot
Copy link

civibot bot commented Jan 3, 2022

(Standard links)

@demeritcowboy
Copy link
Contributor

Not sure if I'm understanding correctly with respect to non-custom fields: Would the next step be to update all the xml schema files to add a not_null setting? I'm confused because api4 getfields seems to ignore the existing <required> setting in the xml (both before and after patch), so required is always false (and so at the moment not_null is always false which is wrong).

And if that's correct, then this is going to be confusing terminology because GenCode etc uses <required> to determine whether the SQL should have NOT NULL in the SQL column definition, but there'd also be a <not_null> right beside it that it doesn't use.

Relatedly this line, while correct I think, is confusing: $ctrl.field.not_null ? null : ts('None') since it reads like "if not null, then null, otherwise none" where "none" means allow null.

Maybe "nullable" would be a better keyword?

@eileenmcnaughton
Copy link
Contributor

Just noting that I tested this change and made the custom field marital status required but I can still remove it using edit in place with this patch (I logged a couple of issues in testing).

image

I'm with @demeritcowboy in struggling to get my head around this one - I think it needs to be documented as a confusing new 'getfields' value - I guess we have 3 things

  1. required at the db level. is_deceased is a required field but it defaults to a reliable FALSE
  2. required at the api level - I can't think of anything required at the api level but not at the db level at the moment but I can't think of things required at the db level and not required at the api level (eg is_deceased) and others that are required at both (financial_type_id)
  3. quasi required - this applies to custom fields - they are required but ONLY if they are present on the form or another value in the same custom group is.

I think isnullable gets as as close as we are likely to get to something that makes sense

@colemanw colemanw changed the title SearchKit - Improve editable UX by not allowing required fields to be de-selected SearchKit - Improve editable UX by not allowing required fields to be left blank Jan 25, 2022
…ble UX

Unlike the 'required' field property, which only determines if the API requires a value to Create,
the 'nullable' property tells a UI whether a field is allowed to be set to NULL in Create OR Update.

SearchKit uses this property during in-place edit and bulk edit operations to determine whether a
field can be left blank.
@colemanw
Copy link
Member Author

@demeritcowboy I'm finally happy with this. Ready for a second round of review. See updated description.

@@ -58,7 +59,8 @@ public static function arrayToField(array $data, $entity) {
$field = new FieldSpec($name, $entity, $dataTypeName);
$field->setType('Field');
$field->setColumnName($name);
$field->setRequired(!empty($data['required']));
$field->setNullable(empty($data['required']));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an "approximation" for now? For example I don't think activity date or status are really "nullable".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of those fields are nullable in our schema. I think you could make a very good argument that they shouldn't be. Fixing it on the schema level can be a pain though because you have to deal with any existing data with null values. A quicker fix would be to set nullable=false just at the APIv4 layer for those fields (via the ActivitySpecProvider). I can do that as a followup.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. My only other comment then was the one below about custom date fields. Otherwise looked good.

@demeritcowboy
Copy link
Contributor

It doesn't seem to work for non-required custom date fields. I can't blank it out.

@colemanw
Copy link
Member Author

It doesn't seem to work for non-required custom date fields. I can't blank it out.

@demeritcowboy I tracked that down to something pretty obscure. The API was returning a full date-time value for custom fields which only contain a date, and it was making the Angular field validation confused. Fixed in #22649.

@demeritcowboy
Copy link
Contributor

Thanks @colemanw I tested both together and looks good. Will put merge-ready here.

@demeritcowboy demeritcowboy added the merge ready PR will be merged after a few days if there are no objections label Jan 28, 2022
@colemanw colemanw merged commit 6005455 into civicrm:master Jan 28, 2022
@colemanw colemanw deleted the searchKitNotNull branch January 28, 2022 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has-test merge ready PR will be merged after a few days if there are no objections
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants