Skip to content

Omit novalidate from comment form by default but introduce novalidate arg to override #8858

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

Closed
wants to merge 8 commits into from

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented May 31, 2025

The comment form currently outputs a FORM tag with a novalidate attribute if the theme supports html5. This is obsolete now that browsers have greatly improved their support for client-side validation. Omitting this attribute helps ensure that a commenter does not accidentally submit an incomplete comment form, potentially risking losing the drafted comment (e.g. if the browser's bfcache is not enabled).

With this PR, the novalidate attribute is omitted by default. If a site owner wants to retain the novalidate attribute on the comment form, they can opt-in via a filter, for example:

add_filter(
	'comment_form_defaults',
	static function ( array $defaults ): array {
		$defaults['novalidate'] = true;
		return $defaults;
	}
);

Or when they can pass in this argument when the theme calls comment_form():

comment_form( array( 'novalidate' => true ) );

Trac ticket: https://core.trac.wordpress.org/ticket/47595

Commit Message

Comments: Remove novalidate attribute from comments form by default.

Browser support for client-side validation is now reliable. Blocking the form submission when there is a missing/invalid field prevents the possibility of losing comment content when bfcache does not restore the page.

To retain the novalidate attribute, the comment_form()'s args now accepts a novalidate key which will add the attribute when it is true. This can also be supplied by the comment_form_default_fields filter, for example:

{{{#!php
add_filter( 'comment_form_defaults', function ( $args ) {
$args['novalidate'] = true;
return $args;
} );
}}}

Fixes #47595.
Props westonruter, joedolson, sabernhardt.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

github-actions bot commented May 31, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props westonruter, sabernhardt, joedolson.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@westonruter westonruter requested a review from joedolson May 31, 2025 19:39
Co-authored-by: Stephen A. Bernhardt <[email protected]>
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@@ -2446,6 +2446,7 @@ function wp_list_comments( $args = array(), $comments = null ) {
* @since 4.6.0 Introduced the 'action' argument.
* @since 4.9.6 Introduced the 'cookies' default comment field.
* @since 5.5.0 Introduced the 'class_container' argument.
* @since n.e.x.t Introduced the 'novalidate_form' argument.
Copy link
Member Author

Choose a reason for hiding this comment

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

I used this placeholder version since I wasn't sure how long it would take to merge this or if we'd target it for 6.8.2. But seems like it makes sense for a minor release, especially with the current relaxation of the criteria.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree; 6.8.2 seems reasonable. But with all the changes recently, I honestly don't have any idea...

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 234cd63

Copy link
Contributor

@joedolson joedolson left a comment

Choose a reason for hiding this comment

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

This isn't really a request changes; it's more of an 'open to discussion'. I think that this is fine to go as is, but I feel there might be room for a clearer variable name. Feel free to dismiss if you disagree or feel it's a net neutral.

@@ -2446,6 +2446,7 @@ function wp_list_comments( $args = array(), $comments = null ) {
* @since 4.6.0 Introduced the 'action' argument.
* @since 4.9.6 Introduced the 'cookies' default comment field.
* @since 5.5.0 Introduced the 'class_container' argument.
* @since n.e.x.t Introduced the 'novalidate_form' argument.
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree; 6.8.2 seems reasonable. But with all the changes recently, I honestly don't have any idea...

@@ -2646,6 +2648,7 @@ function comment_form( $args = array(), $post = null ) {
),
'comment_notes_after' => '',
'action' => site_url( '/wp-comments-post.php' ),
'novalidate_form' => false,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if this would be clearer as novalidate_att, and indicate that this is the behavior of the novalidate attribute. I'm on the fence about this, because we can't really avoid the double-negative. I'm not sure there's any foolproof way to communicate how the false state of a negative attribute is expressed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question. I picked this name to parallel to keys like class_container, id_form, and name_submit which is pattern is attribute name followed by the thing the attribute is for.

Copy link
Contributor

Choose a reason for hiding this comment

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

It makes sense; it just doesn't follow the pattern used elsewhere in the same array. E.g. 'action' => "value of the action attribute".

Copy link
Contributor

Choose a reason for hiding this comment

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

By that argument, maybe it would just be 'novalidate' => false

Copy link
Contributor

Choose a reason for hiding this comment

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

Consistency, apparently, is hard. ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

It couldn't be relevant to any other element anyway, same as action, as only the form has either of these attributes. It's different for id or class when they can be on anything.

Choose a reason for hiding this comment

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

For another idea: use_novalidate (which is similar to use_desc_for_title in wp_list_categories())

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so because the other keys don't use a verb like that in this function. I like novalidate as it can't be any simpler 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

I think just 'novalidate' makes sense. As @westonruter says, it's not valid on any other element, and it matches the method used for 'action'. Let's go with that.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, there we are! 3550454

@westonruter westonruter requested a review from joedolson June 12, 2025 16:43
@westonruter westonruter requested a review from sabernhardt June 12, 2025 18:32
@westonruter westonruter changed the title Omit novalidate from comment form by default but introduce novalidate_form arg to override Omit novalidate from comment form by default but introduce novalidate arg to override Jun 12, 2025
Copy link

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 60304
GitHub commit: b29a5c2

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants