DataForm: mark fields as required or optional automatically#74430
DataForm: mark fields as required or optional automatically#74430oandregal merged 16 commits intoWordPress:trunkfrom
Conversation
|
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @czarflix! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
@czarflix thanks for your contribution! A couple of notes: If you join Make WordPress as a contributor, and then link your account to your GitHub account, you'll be marked as a contributor in the WordPress 7.0 release. You also get a profile like this https://profiles.wordpress.org/oandregal/ This is a step in the good direction, we need to do an extra step: the downstream consumers need a way to pass this setting to the Edit control. This is a form-level configuration (either fields are highlighted as required or optional, it doesn't make sense mixing both). So this is what I'm thinking:
|
|
@oandregal Thanks for the feedback and the contributor tip — I’ll get that set up. Your form-level approach makes more sense. Here’s how I’m planning to implement it:
|
|
I've updated the PR with the form-level approach. Changes
|
c353d5b to
1a7fdee
Compare
CI Lint Failures - Pre-existing IssuesThe CI lint failures are not caused by this PR:
Could a maintainer confirm if these pre-existing lint issues should block this PR, or be addressed separately? |
|
@oandregal @gigitux @ntsekouras Fixed the lint issues from the previous run. Ready for review when you have time! |
| () => ( { | ||
| layout: DEFAULT_LAYOUT, | ||
| fields: !! field.children ? field.children : [], | ||
| labelMode: 'showRequired', |
There was a problem hiding this comment.
I'm a bit confused why in all cases here we pass showRequired mode, whereas below we pass markWhenOptional which was computed based on the label mode.
There was a problem hiding this comment.
This was a bug. All 6 nested layout files were hardcoding labelMode: 'showRequired', which broke the parent's computed value.
Fixed by:
- Removed
labelModefrom all nested form objects - Added
markWhenOptionaltoDataFormContext - All layouts now read from context, ensuring proper inheritance
The context approach means nested forms at any depth correctly inherit the auto-computed value.
|
Thanks for the PR! I might be missing context, but here are my thoughts after checking the changes and reading the comments above. I'm not sure we should have any configurable prop for this and I believe we should be opinionated with the one referred to as This way we don't have to add any new API since the UX wise I believe it's better to have this |
|
I can't comment on the code but I can confirm that the pattern we'd like to enable is one where the minimal number of fields are marked. IE if 2 of 10 fields are required only the required ones should be marked, and vice versa. When all the fields in the form are required or optional that should be stated in the DataForm I'm planning a PR to document this. |
- Remove configurable form.labelMode prop - Add markWhenOptional to DataFormContext (auto-computed) - Fix nested forms to inherit labeling via Context - Mark minority of fields (required OR optional) to reduce noise Addresses feedback from @ntsekouras and @jameskoster: - Auto-only behavior, no configurable prop - Nested forms now correctly inherit labeling
|
Thanks @jameskoster and @ntsekouras for the feedback! I've refactored to the auto-only approach as suggested: Changes in this updateRemoved configurable
|
|
@czarflix, you have so far taken time and attention from at least 3 lead contributors, while communicating exclusively via LLM-generated responses. If you cannot defend the work nor explain it as a person when asked, and are unable to follow up, then let's stop wasting more time and close this pull request. I've labeled this PR accordingly and we'll allow a grace period while we wait for your feedback. |
Hey @mcsf, sorry for the confusion! English isn't my first language so I've been using AI rewriters to help clean up my grammar and formatting before posting , didn't mean to come across as impersonal. I also have a full-time job and things got hectic this past week, which is why I've been slow to respond. I'll go through all the feedback today and respond properly. Thanks for your patience |
I apologize for the delay, it was unavoidable due to work pressure. Here is what I meant, What I recommended was keeping the existing required toggle, but adding a second control called fieldDistribution with options: 'All Same' (current behavior) |
|
@oandregal I also noticed that the panel layouts (dropdown/modal) weren't passing markWhenOptional through the children render prop , will fix that. For the Storybook enhancement, let me know if the fieldDistribution approach works and I'll implement it and push the changes together. |
Thank you for your honesty, and for your graceful response to my impatience. Indeed, English isn't the mother tongue of most of us, and I recognise the inherent friction. I can only say this: your short reply here felt more concrete, readable and conducive to debate than anything that came before it. :) I'll also add that, when choosing to use an LLM for translation, one should make the LLM faithfully translate one's words, and never let it take over (substance, form, filler). |
I will keep this in mind. Thank you for understanding. |
Can we make a decision to move forward with this PR, as it's quite close to landing? I think the 'auto' algorithm works well, but we need to figure out what to do when all fields are required:
We could just go with option 2, which is already the case in trunk right now. @oandregal @jameskoster |
|
Option 2 could work as an interim 👍 |
I thought we had one: it's the consumer responsibility to display the message. A potential follow-up to this may be adding a description to layouts that don't support description. IMO, this PR only needs to update the validation story mentioned here. |
|
| ); | ||
|
|
||
| // Auto-compute: mark the minority of fields | ||
| // When counts are equal, mark optional fields (arbitrary but consistent) |
There was a problem hiding this comment.
I think in the case of equal fields we should show the required label.
… story - Move markWhenOptional computation from DataForm to DataFormLayout - Remove markWhenOptional from context (per @ntsekouras) - Change tie-breaker: equal counts now show required labels (>) - Fix panel/dropdown and panel/modal to pass markWhenOptional - Wire up validation story with fieldDistribution control - Add unit tests for auto-compute logic
- Fixed prettier formatting issues from GitHub merge - Added missing 'required' dependency to useMemo (for combobox field) - Added CHANGELOG entry for markWhenOptional feature
|
Pushed the changes. Moved markWhenOptional computation to DataFormLayout, fixed the tie-breaker to use >, and wired up all the validation story fields with the fieldDistribution control. @ntsekouras @oandregal Ready for re-review when you get a chance. |
|
@czarflix congrats on your 1st contribution. I've pushed some changes directly to the PR to speed things up:
|
Co-authored-by: czarflix <czarflix@git.wordpress.org> Co-authored-by: oandregal <oandregal@git.wordpress.org> Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org> Co-authored-by: jameskoster <jameskoster@git.wordpress.org> Co-authored-by: mcsf <mcsf@git.wordpress.org> Co-authored-by: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>
What?
Closes #74417
Adds the
markWhenOptionalprop to the DataViews Field API, allowing fields to display "(Optional)" instead of "(Required)" labels.Why?
When most fields in a form are required, it's more intuitive to highlight the optional fields rather than marking every required one. The underlying
@wordpress/componentsvalidated controls already supportmarkWhenOptional, but it wasn't exposed through the DataViews Field API.How?
markWhenOptional?: booleantoDataFormControlPropsinfield-api.tsmarkWhenOptional?: booleantoFieldLayoutPropsindataform.tshideLabelFromVisionFiles modified: 25 files across types, controls, and layouts
Testing Instructions
isValid: { required: true }on most fieldsmarkWhenOptional: trueto an optional field (one withoutrequired)Testing Instructions for Keyboard
No keyboard-specific testing needed - this change passes an existing prop through the component hierarchy without modifying any interactive behavior.
Screenshots or screencast
N/A - No visual changes to document. The feature enables an existing capability in the underlying components.