-
-
Notifications
You must be signed in to change notification settings - Fork 271
Block Editor Form Validation #7369
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
Changes from all commits
d3758f7
cbc344e
4d0b2d1
2f05b19
51109c3
aa3e569
75b48a6
f0edad7
c3e1754
18824a6
cdef79b
ca62d24
a864be1
7535dea
e5269b9
28b9fbf
c85e513
3aebe4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| {"dependencies":["lodash","react","react-dom","wp-api-fetch","wp-autop","wp-block-editor","wp-blocks","wp-components","wp-compose","wp-date","wp-element","wp-i18n","wp-keycodes","wp-server-side-render","wp-url"],"version":"559be62f189fbb2870a7"} | ||
| {"dependencies":["lodash","react","react-dom","wp-api-fetch","wp-autop","wp-block-editor","wp-blocks","wp-components","wp-compose","wp-date","wp-element","wp-i18n","wp-keycodes","wp-server-side-render","wp-url"],"version":"455167998375a626b9ff"} |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| {"dependencies":["lodash","moment","react","react-dom","react-jsx-runtime","regenerator-runtime","wp-api-fetch","wp-autop","wp-components","wp-compose","wp-data","wp-element","wp-hooks","wp-i18n","wp-keycodes","wp-plugins","wp-primitives","wp-url"],"version":"073a6a8b184586b2e522"} | ||
| {"dependencies":["lodash","moment","react","react-dom","react-jsx-runtime","regenerator-runtime","wp-api-fetch","wp-autop","wp-components","wp-compose","wp-data","wp-element","wp-hooks","wp-i18n","wp-keycodes","wp-plugins","wp-primitives","wp-url"],"version":"e7b4f23bda29677f21a5"} |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import {__, sprintf} from '@wordpress/i18n'; | ||
|
|
||
| const useBlockEditor = () => { | ||
| const editorSelect = wp.data?.select('core/editor'); | ||
| const editorDispatch = wp.data?.dispatch('core/editor'); | ||
| const notices = wp.data?.dispatch('core/notices'); | ||
|
|
||
| // @todo Use hook instead of savePost override once stable. | ||
| if (!window.PodsBlockEditor && editorDispatch && editorDispatch.hasOwnProperty('savePost')) { | ||
| // First init. | ||
| window.PodsBlockEditor = { | ||
| // Store original. | ||
| savePost: editorDispatch.savePost, | ||
| messages: {}, | ||
| callbacks: {}, | ||
| }; | ||
|
|
||
| // Override the current editor savePost function. | ||
| editorDispatch.savePost = async (options) => { | ||
| options = options || {}; | ||
|
|
||
| const pbe = window.PodsBlockEditor; | ||
|
|
||
| if (!Object.values(pbe.messages).length) { | ||
| notices.removeNotice('pods-validation'); | ||
|
|
||
| // eslint-disable-next-line no-undef | ||
| return pbe.savePost.apply(this, arguments); | ||
| } | ||
|
|
||
| return new Promise(function (resolve, reject) { | ||
| // Bail early if is autosave or preview. | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sc0ttkclark Should we allow saving for a draft?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 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
} |
||
| if (options.isAutosave || options.isPreview) { | ||
| return resolve('Validation ignored (autosave).'); | ||
| } | ||
|
|
||
| let validationFieldErrorList = []; | ||
| for (const fieldName in pbe.messages) { | ||
| editorDispatch?.lockPostSaving(fieldName); | ||
|
|
||
| let fieldRealName = fieldName.replace('pods-field-', ''); | ||
| let fieldData = window.PodsDFVAPI.getField(fieldRealName); | ||
|
|
||
| validationFieldErrorList.push(fieldData?.fieldConfig?.label ?? realFieldName); | ||
| } | ||
|
|
||
| notices.createErrorNotice( | ||
| sprintf(__('Please complete these fields before saving: %s', 'pods'), validationFieldErrorList.join(', ')), | ||
| {id: 'pods-validation', isDismissible: true}, | ||
| ); | ||
|
|
||
| for (const fieldCallback in pbe.callbacks) { | ||
| if (pbe.callbacks.hasOwnProperty(fieldCallback) && 'function' === typeof pbe.callbacks[fieldCallback]) { | ||
| pbe.callbacks[fieldCallback](); | ||
| } | ||
| } | ||
|
|
||
| return reject('Pods validation failed'); | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| data: wp.data, | ||
| select: editorSelect, | ||
| dispatch: editorDispatch, | ||
| notices, | ||
| lockPostSaving: (name, messages, callback) => { | ||
| // @todo Use hook instead of savePost override once stable. | ||
| //wp.hooks.addFilter( 'editor.__unstablePreSavePost', 'editor', filter ); | ||
|
|
||
| if (messages.length) { | ||
| window.PodsBlockEditor.messages[name] = messages; | ||
| window.PodsBlockEditor.callbacks[name] = callback; | ||
| } | ||
| }, | ||
| unlockPostSaving: (name) => { | ||
| // @todo Use hook instead of savePost override once stable. | ||
| //wp.hooks.removeFilter( 'editor.__unstablePreSavePost', 'editor', filter ); | ||
|
|
||
| delete window.PodsBlockEditor.messages[name]; | ||
| delete window.PodsBlockEditor.callbacks[name]; | ||
|
|
||
| editorDispatch?.unlockPostSaving(name); | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export default useBlockEditor; | ||
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.