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

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.
@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 Versioning Identifier
    The version of a certain file (pods-dfv.min.asset.json) has been updated. This often means that some functionality within that file has been improved or bugs have been fixed.

  • Improved Handling of Editor Save Actions
    Two new 'hooks' or pieces of logic (useEffect and useBlockEditor) have been added to field-wrapper/index.js file. These will handle saving within the block editor and also manage its locking.

  • Enhanced Post Saving Based on Validation
    We introduced a mechanism in FieldWrapper that can lock (prevent) or unlock (permit) the act of saving posts based on whether their data passes certain checks (validation).

  • Improved Condition Checking
    The condition used to check the validity of data within the FieldWrapper has been tweaked for better accuracy. Now the status of data (valid or invalid) will be reflected correctly.

  • Added New File for Managing Editor Save Actions
    A new file named useBlockEditor.js has been added. It provides functions to manage actions associated with saving in the editor. It covers aspects like displaying validation messages and controlling the locking mechanism.

  • Integrated Save Management in Editor's Initialization
    The useBlockEditor hook introduced earlier is also integrated within app.js. This is essentially to initialize the management of saving within the editor whenever Pods block editor is being used.

}

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.

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