0
<input required name="myName" type="text" value="<?php echo $myValue; ?>" class="regular-text">

Does not produce an error message if the field is left blank when I attempt to Update.

What else do I need to do in order to tell the user that this field is required in Gutenberg?

Using the Classic Editor plugin it won't even let me update the page. It presents me with a "!" tooltip that says "Please fill out this field." :)

Thanks!

3
  • why does @myvalue start with a @? Note that this is much much easier to do if you're registering a panel or sidebar in Gutenberg via javascript/react, than if you use PHP for everything, there are even frameworks for pre-publish checklists that prevent publishing if requirements aren't met
    – Tom J Nowell
    Commented May 20, 2021 at 20:46
  • Sorry typo on the variable, fixed. So simple to add HTML5 required attribute which works in classic editor, but not available in Gutenberg? Is this a "bug" / outstanding update perhaps?
    – Drewdavid
    Commented May 20, 2021 at 20:52
  • required only works if you're submitting a form and relies on browser behaviour entirely, but the block editor uses the REST API, so you should not expect it to work in Gutenberg. PHP meta-boxes are there for backwards compatibility, they aren't a high priority for new features and aimed at not breaking existing code, rather than supporting new code. You would be far better served by a Gutenberg native UI. You're welcome to try and make it work with a PHP metabox but you'll end up implementing almost all of a native solution and then some to get there, javascript will be unavoidable
    – Tom J Nowell
    Commented May 20, 2021 at 21:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.