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

Add error message under input field. #8273

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/js/_enqueues/lib/nav-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -1103,12 +1103,38 @@

$('#add-custom-links input[type="text"]').on( 'keypress', function(e){
$('#customlinkdiv').removeClass('form-invalid');
$('#custom-menu-item-url').hide();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be #custom-url-error.


if ( e.keyCode === 13 ) {
e.preventDefault();
$( '#submit-customlinkdiv' ).trigger( 'click' );
}
});

$('#submit-customlinkdiv').on('click', function (e) {
var urlInput = $('#custom-menu-item-url'),
url = urlInput.val().trim(),
errorMessage = $('#custom-url-error'),
urlWrap = $('#menu-item-url-wrap');

// Hide the error message initially
errorMessage.hide();
urlWrap.removeClass('has-error');

if ('' === url || 'https://' === url || 'http://' === url) {
e.preventDefault();
urlInput.addClass('form-invalid')
.attr('aria-invalid', 'true')
.attr('aria-describedby', 'custom-url-error');

var errorText = wp.i18n.__('No content entered. Please enter a valid link.');
errorMessage.show();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I suggested this text, but given the flexibility in what the possible values can be, probably need to make this more general yet. Perhaps "Content is required for the link's href attribute."

urlWrap.addClass('has-error');

// Announce error message via screen reader
wp.a11y.speak(errorText, 'assertive');
}
});
},

/**
Expand Down
3 changes: 2 additions & 1 deletion src/wp-admin/css/nav-menus.css
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ input.bulk-select-switcher:focus + .bulk-select-button-label {

/* Add Menu Item Boxes */
.postbox .howto input,
.customlinkdiv .menu-item-textbox {
.customlinkdiv .menu-item-textbox,
.customlinkdiv .error-message {
width: 180px;
float: right;
}
Expand Down
1 change: 1 addition & 0 deletions src/wp-admin/includes/nav-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ function wp_nav_menu_item_link_meta_box() {
type="text"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?>
class="code menu-item-textbox form-required" placeholder="https://"
/>
<span id="custom-url-error" class="error-message" style="display: none;"><?php _e( 'No content entered. Please enter a valid link.' ); ?></span>
</p>

<p id="menu-item-name-wrap" class="wp-clearfix">
Expand Down
Loading