-
Notifications
You must be signed in to change notification settings - Fork 180
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
Gutenberg compatibility for PHP/AJAX Validation #113
Comments
@noisysocks Adding Gutenberg compatibility for PHP/AJAX Validation will be our next focus. Thanks for the info provided over on the original ticket. I added an extra note in the topic above: Can we work on a solution for validating the form data via AJAX that also keeps the above in mind? |
Copying some answers over from WordPress/gutenberg#12692 (comment).
const { getEditedPostAttribute } = wp.data.select( 'core/editor' );
const title = getEditedPostAttribute( 'title' );
const content = getEditedPostAttribute( 'content' );
Yes, the Notices API is what we'll want here. Namely, wp.data.dispatch( 'core/notices' ).createErrorNotice( 'Something is not good!' ); |
Got it. I don't think this is currently possible but it's a new hook that we can discuss adding over in the Gutenberg project. To clarify: do we need to hook into when the post is being saved, or when the post is being published? |
We validate the custom field values only when publishing a post, or when updating a published post. At the moment, we listen to the "form submit" action, and "prevent default" to stop the form from submitting. We then validate the form data via AJAX, and either display errors or re-submit the form (but this time allow the event to run). |
My current thinking is that ACF could use
Very quick mockup of what this could look like: Does that make sense? Would this be a workable approach? |
Hi @noisysocks This is a great idea, but will this also work for updating a published post? |
Hey @elliotcondon. That's a good point. No, unfortunately this won't handle the case where a published post is updated. We may have to add a new filter for this after all. I've suggested this over in WordPress/gutenberg#13413. |
Hi @noisysocks Just following up on this one. Did we get anywhere with a solution to "validate" and "prevent" the form from being submit? If not yet explored, would it be possible to use a promise object + a JS filter to allow 3rd party (or core) logic to stop/trigger the saving process? |
Hey @elliotcondon! I like your new avatar 🙂 I haven't been actively working on this. WordPress/gutenberg#13413 tracks adding the API which I prefer which is a promise object + JS filter. WordPress/gutenberg#13718 is an unrelated PR that also had a need for such an API. @talldan: What's the status on WordPress/gutenberg#13718? Do you think we can include the mentioned API? |
Any update here? |
@matgargano - Not from my end. I'm still waiting for WordPress to add in some sort of action/filter to hook in and customize the saving process. |
Is there a ticket on the Wordpress side we can comment on/contribute to? |
@benbowler - There are a few GitHub threads listed in previous replies on this thread. If you're reading this via email, click the GitHub link to view this issue in your browser so you can scroll up and check those links. For example: WordPress/gutenberg#13413 |
Hi all, Do we have any update here? |
Hi @elliotcondon, Any update on this? As the WordPress 5.3 has been released, You probably able to fix the issue now. Best |
@jenilk I'm not aware of any changes in WP 5.3 that introduced an API for PHP validation. If you can provide some reference, that would be great :) |
Hi @elliotcondon I am also in a same phase as you're. However, I have created a ticket to WordPress where we can all contribute there. As this issue goes to a blocker we need to find the solutions as earliest. Ticket: https://core.trac.wordpress.org/ticket/48959 Best |
Hi @elliotcondon - although there is no specific API for PHP side validation, you should be able to achieve something close to what you want with the existing extensibility provided in Gutenberg (unless I am missing something):
If this doesn't work for your use case, can you explain why? Can you identify exactly what type of hook or filter in GB would enable your use case? This plugin might be useful to look at: https://github.com/humanmade/publication-checklist |
Thanks for the information. The "Post saving lock" API and the example plugin look very promising and I'll begin some testing with this shortly! |
Hi @isuke01 Thank you very much for your contribution here. I too have thought about using a click event listener, but ultimately scrapped the idea as it would not comply with Gutenberg's keyboard accessibility features. If you are able to find a more holistic way to listen for (and prevent) the "save process", this would be greatly appreciated! |
Thanks @elliotcondon, yes, I totally forgot about that thing! I'll look into it. EDIT: Anyway I have solved that thing using
EDIT: wp.data check |
Hi @isuke01 Thanks for looking into the keyboard shortcut issue. I'll be sure to test out something similar. |
Actually one more save of post that should we take care is switch from any status to Another thing. "Preview" it does not call save for current post, so no problem but I noticed Preview in this case is not saving any meta. (Tested on Posts, not custom CTP), maybe because fields are not And about clicking "mod+s" shortcut to save, it works only when something is edited. But actually this does not work on ACF fields, so if you edit something inside ACF only, not in Gutenberg container, you're unable to save post using key commands. BUT! Since in my above function I'm not checking for Would be nice to have |
Hi @isuke01 Thank you for all the extra info! It looks like we are not the only ones looking for a solution to this problem. Here are some GithUb issues you might be interested in looking over / following:
I agree that the best case scenario would be a If you have some free time, please read over those GitHub issues mentioned above, and let me know if you see anything that might be useful 😄. |
Hey @elliotcondon Thanks, actually my first experience with Gutenberg was 4 days ago, and I just spend few hours on it ... and it was painfully (I hope they will fix docs at some point it is agrrggrr)😄 I just cant deliver to client something that does not work so I tried quick fix it. Anyway I have find out Gutenberg is not saving meta fields during preview/autosave/auto-draft save. WordPress/gutenberg/#20755. So basically until I find a way how to solve saving those meta for preview, Gutenberg is not usable for me :/ Thing is, without Gutenberg everything works fine. Im out of time to solve this issue by now, I'll look into that issues as soon as I can find some time, but for next couple of days Im totally occupied, and simply I'll use classic editor instead for now :) |
hi @isuke01 I was so in trouble, tried your code.
|
@altendorfme Thanks for the feedback! |
@todd-uams Please note this validation improvement is not for "ACF Blocks". The code mentioned above will validate metaboxes. |
Sorry for the mixup. The code did work for metaboxes on a Gutenberg page. Thanks |
@elliotcondon I'll look at @benfrem 's code for a potential solution for ACF Blocks. |
@todd-uams Fantastic! Block validation will be coming soon, but only after metabox validation is considered stable :) |
I can confirm that a mixture of the code above works great when using 'edit' mode. It also works when in 'preview' mode and editing a specific block, but only validates that block and not the others. Thanks! However, I am still looking for a way to get this working in 'preview' mode for all blocks. @elliotcondon I know your working on a solution (Thank You), but since we don't have an ETA |
@elliotcondon I have downloaded v5.9.0-beta1.
[EDITED] Also I think when there is an error on a block there should be a class added to the "wp-block" tag or the "acf-block-component" tag like 'acf-validation-error' and some sort of error indicator like a red outline etc. That way the User can see which blocks have errors and click on them to resolve. Probably only needed for 'preview' mode or 'auto' mode when block is in preview state. I am writing a custom class for my clients so I will most likely override the styles for my own and that is why a css class is important for me. Let me know if there is a better place to report issues for v5.9.0-beta |
Hi @ggedde. Please note the "Gutenberg Validation" feature in 5.9.0-beta1 is for metaboxes only - not "ACF Blocks". Block validation will be coming soon in a future update 👍. |
@elliotcondon Any updates on block validation? |
Hi @andjoen 👋. Block validation is planned or a "post 5.9" release. For now, we will focus on just the entire post/page validation. Looking forward to the challenge when we do dip our toes into that pond! |
I have seen a few updates to 5.9, but nothing mentions this issue. Do you have an idea as to when you might start working on this issue for 5.9.x. I am willing to test anything if needed. |
Thanks @ggedde. No updates just yet. |
Just to clarify, I am seeing neither block validation nor "entire post/page validation" working as of 5.10.x. Is that correct? Is this still actively being worked on? Thanks for all your hard work so far. |
@StudioAl that's strange. We don't yet have block validation, but regular validation (with fields attached to the post/page itself) should work fine. Could you please share an export of a field group where post/page validation doesn't work for you? |
@mattgrshaw, yes, strange — I just went back to verify and, and now the post/page field validation is working. I'm not sure what the issue was for me at the time that prevented it from working — I was definitely on the latest version of the plugin. |
Still no solution for this. Has WordPress said anything about this? It's a very important feature... I'll dig through the code and see how the post/page field validation is implemented, and see if there's something we can do about the block editor itself. |
Hi @elliotcondon any updates about this ? We are talking about this bug since 2019 and still not working it's a major issue. Your page https://www.advancedcustomfields.com/resources/known-issues/ talking about it say : "Status: Similar to above, this feature is no longer working and is scheduled to be fixed in ACF 5.8." We are in 5.12.2... what's going on ? I know Gutenberg is very involved in this problem and I know you haven't found an "elegant" solution but can't you make a temporary "less elegant" fix in the mean time? I don't want to sound like an angry guy, I can understand that some things take time, but we are talking about more than 2 years on a major problem. Thanks |
@alexandre-tobia I think the issue is "I know Gutenberg is very involved in this problem" that sentence is not true. The Gutenberg team is holding this back, they have to fix issue in their end ;/ |
@isuke01 Thanks for your answer Sorry if my message was rude, not intented. Why not add thoses "temp" solution in the acf plugin ? it's better than nothing, no ? I don't see any update from gutenberg workign on this since 2020, so if acf don't include this temp solution i think we will never reach the end of the tunnel... |
Sorry for the rant, @benbowler, have you found a solid solution yet? it seems like this Issue is just getting ignored and it is a vital problem in my opinion. My client's keep asking, can you make that field required? Then I have to give them this long explanation. It honestly sounds like, to them, that I don't know what I am doing. "Why can't you just make the field required?" they say, and I don't blame them. I have already been looking at different solutions and have started migrating to building custom Gutenberg Blocks, but they are much more difficult to manage and create. The main issue with them is that they are much more difficult for clients to manage. It almost is like they need to go through a dedicated course to learn it. Not to mention keeping up to date on all the changes that happen on every WP update. It doesn't work for most clients. ACF Block's is the way to go and I appreciate the work and dedication that Elliot has provided and that Delicious Brains has taken on. We just need this last feature to tie it all in. I am willing to test / research / and work on this issue with others to find a solid solution. With the Temp solutions above it gets us 70% of the way there. I have worked on this, but I am not familiar with ACF's and Gutenberg's underlining js code and need help with getting each block's fields and submitted values and checking for required and conditionals, then reference the ACF field settings. I am able to do this with an open edited ACF block, but having a difficult time with the others. If you have some insights to that, then please let me know. Thanks |
Any update? Why has Wordpress not added a simple hook before saving forms on the admin side that ACF can hook into? This seems like a simple simple simple fix. And over 4 years of waiting seems like a ridiculous amount of time to wait for such a small addition. During this time we have seen both ACF, and Wordpress go through countless updates, and new {insert year here} themes (that nobody I know uses) but no new simple little hook prior to saving forms on the admin side. For ACF being one of Wordpress' #1 plugins it is really surprising to me how little time or effort they put into helping ACF succeed when it was something WP did that borked the whole thing. |
Any of you have solved or hacked this? I'm trying to require a field if a block is present, but validation doesn't work |
Is there any update regarding this issue? What is the current status? Does it still lie with wordpress/core or gutenberg? What are the relevant tickets/issues so we can add our support there? Its absolutely incomprehensible why this critical issue doesnt have at least a functioning workaround after more than 4 years. |
Just encountered that issue. Anyone has a solution for this? |
Also just came across this issue, and it's a pretty big one. I'm surprised it's still lingering... |
about 5 years later, it's finally happening! |
Isn't it fixed yet? |
Description
A highly used feature in ACF is our PHP validation powered via AJAX. This allows developers to highly customize the validation process via PHP whist providing convenient feedback to the user when a field value does not meet its requirements.
To do this, we use JS to intercept the "submit" action on the form and stop it. We then send all
$_POST
data via an AJAX request and use the returned JSON to either display errors or re-submit the form.It is important for UX that we validate the form data only when the user clicks the "publish" button.
Issues
Questions
The text was updated successfully, but these errors were encountered: