Make WordPress Core


Ignore:
Timestamp:
06/12/2025 08:45:40 PM (7 months ago)
Author:
westonruter
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 previous page when going back.

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:

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

Fixes #47595.
Props westonruter, joedolson, sabernhardt, afercia, SergeyBiryukov, guddu1315, pfefferle, oglekler, bugnumber9.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment-template.php

    r60249 r60304  
    24472447 * @since 4.9.6 Introduced the 'cookies' default comment field.
    24482448 * @since 5.5.0 Introduced the 'class_container' argument.
     2449 * @since 6.8.2 Introduced the 'novalidate' argument.
    24492450 *
    24502451 * @param array       $args {
     
    24682469 *     @type string $comment_notes_after  HTML element for a message displayed after the textarea field.
    24692470 *     @type string $action               The comment form element action attribute. Default '/wp-comments-post.php'.
     2471 *     @type bool   $novalidate           Whether the novalidate attribute is added to the comment form. Default false.
    24702472 *     @type string $id_form              The comment form element id attribute. Default 'commentform'.
    24712473 *     @type string $id_submit            The comment submit element id attribute. Default 'submit'.
     
    26472649        'comment_notes_after'  => '',
    26482650        'action'               => site_url( '/wp-comments-post.php' ),
     2651        'novalidate'           => false,
    26492652        'id_form'              => 'commentform',
    26502653        'id_submit'            => 'submit',
     
    27302733                esc_attr( $args['id_form'] ),
    27312734                esc_attr( $args['class_form'] ),
    2732                 ( $html5 ? ' novalidate' : '' )
     2735                ( $args['novalidate'] ? ' novalidate' : '' )
    27332736            );
    27342737
Note: See TracChangeset for help on using the changeset viewer.