-
-
Notifications
You must be signed in to change notification settings - Fork 264
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
base: main
Are you sure you want to change the base?
Conversation
PR Summary
|
} | ||
|
||
return new Promise( function( resolve, reject ) { | ||
// Bail early if is autosave or preview. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
}
// @todo Use hook instead of savePost override once stable. | ||
if ( ! window.PodsBlockEditor && editorDispatch && editorDispatch.hasOwnProperty( 'savePost' ) ) { | ||
// First init. | ||
window.PodsBlockEditor = { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.
Signed-off-by: Jory Hogeveen <[email protected]>
60f9a41
to
0205024
Compare
6b7a7b5
to
f5884cd
Compare
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