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

Block Editor Form Validation #7369

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Conversation

JoryHogeveen
Copy link
Member

Description

This PR adds a useBlockEditor React Hook which will initialize by overriding the default savePost function of WordPress core. Based on the code by ACF. The order of overriding should not matter as we are returning the previous value if no validation errors occur, therefore we should be compatible with ACF and other plugins using the same method.

The hook is further used within the field wrapper component which handles validation. Through this component I can set the valid state and pass any error messages.

This PR will not add extra validation on the current system but will be compatible with any extras we add to the useValidation hook as it is merely using it's response messages.

It should create error messages at the top if you click save and will auto-trigger that message to show on the field itself as well.
After validation it will also disable the save button until you've made the required changes.

Related GitHub issue(s)

Fixes #7343

Testing instructions

Just create a Pod with some required fields and test around by trying to save the post.

Screenshots / screencast

Changelog text for these changes

PR checklist

@JoryHogeveen JoryHogeveen added Status: PR > Pending Code Review PR is pending code review by core developers Status: PR > QA pending QA needs to be done Component: Block Editor Issues related to the Block Editor Component: React Issues related to React JS labels Oct 15, 2024
@JoryHogeveen JoryHogeveen added this to the Next Release milestone Oct 15, 2024
@JoryHogeveen JoryHogeveen self-assigned this Oct 15, 2024
Copy link

what-the-diff bot commented Oct 15, 2024

PR Summary

  • Updated Version Numbers
    The version numbers in pods-blocks-api.min.asset.json and pods-dfv.min.asset.json files have been updated. These changes ensure that we're always working with the latest version of the software components.

  • Improved Saving Process in Block Editor
    A new function (useEffect) has been added to the FieldWrapper component to better manage saving in the Block Editor. This utilizes a newly created tool called useBlockEditor. This makes things better by providing a smoother saving process for your data in the Block Editor.

  • More Precise Validation State Checks
    Validation checks in FieldWrapper have been made more explicit to avoid any ambiguity.

  • New Custom Editor Hook
    A new hook (useBlockEditor.js) has been introduced to manage custom editor behavior. This includes saving, locking, and displaying notices, which enhances the overall user experience while using the editor tool.

  • Integration of the New Hook
    The new useBlockEditor hook has been integrated into the app.js component. This means the initial settings of the editor now account for the new tools and features provided by the useBlockEditor hook, making the editor easier and more effective to use.

}

return new Promise( function( resolve, reject ) {
// Bail early if is autosave or preview.
Copy link
Member Author

Choose a reason for hiding this comment

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

@sc0ttkclark Should we allow saving for a draft?

Copy link
Member

Choose a reason for hiding this comment

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

I'm on the fence here. Maybe we can make that an option in settings or a constant/hook. Default to allow saves on drafts perhaps.

Copy link
Member

Choose a reason for hiding this comment

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

But there's no way to draft by default when you have a post that's already published. So we'd need to distinguish between save and save draft.

Copy link
Member Author

@JoryHogeveen JoryHogeveen Oct 15, 2024

Choose a reason for hiding this comment

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

We can also leave it as-is and add it later if you want. I can quite easily fetch the status value and check that, however, it's hard to define what statuses should be allowed or not.
For ease of use (and clarity) it might be better to just validate by default for now.

EDIT: example

const status = editorSelect.getEditedPostAttribute('status');
if ( [ 'draft', 'trash', 'otherStatus' ].includes( status ) ) {
    // Run default
}
// OR
if ( SOME_USER_SETTING_ARRAY.includes( status ) ) {
    // Run default
}

ui/js/dfv/src/components/field-wrapper/index.js Outdated Show resolved Hide resolved
// @todo Use hook instead of savePost override once stable.
if ( ! window.PodsBlockEditor && editorDispatch && editorDispatch.hasOwnProperty( 'savePost' ) ) {
// First init.
window.PodsBlockEditor = {
Copy link
Member Author

Choose a reason for hiding this comment

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

Optionally make this a class with private props and getters/setters. Maybe even an external tool like the API instead of a React hook.

Copy link
Member Author

Choose a reason for hiding this comment

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

Lets keep it like this since we should definitely change this code once a hook is available.
The added todo will inform any dev checking this code in that it's a temp solution.

JoryHogeveen and others added 11 commits November 16, 2024 18:14
This hook will override the WP Editor savePost action just like ACF (for the time being) until a stable PreSavePost hook becomes available. I've already added todo tags into the code as a reminder.
Code is based on ACF: https://github.com/AdvancedCustomFields/acf/blob/6.3.8/assets/build/js/acf-input.js#L11285
At this point `wp.data.dispatch('core/editor')` is available React is not rendered yet so we can properly override the savePost function.
@sc0ttkclark sc0ttkclark changed the base branch from main to release/3.2.8 November 17, 2024 00:14
@sc0ttkclark sc0ttkclark modified the milestones: Next Release, Pods 3.3 Nov 17, 2024
Base automatically changed from release/3.2.8 to main November 17, 2024 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Block Editor Issues related to the Block Editor Component: React Issues related to React JS Status: PR > Pending Code Review PR is pending code review by core developers Status: PR > QA pending QA needs to be done
Projects
Status: 👀 In review
Status: 👀 In Review / QA
Development

Successfully merging this pull request may close these issues.

Form Validation: Post editor (block editor)
2 participants