diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index cd41d4b200535..f023c03cd0c67 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -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 6.8.2 Introduced the 'novalidate' argument. * * @param array $args { * Optional. Default arguments and form fields to override. @@ -2467,6 +2468,7 @@ function wp_list_comments( $args = array(), $comments = null ) { * Default 'Your email address will not be published.'. * @type string $comment_notes_after HTML element for a message displayed after the textarea field. * @type string $action The comment form element action attribute. Default '/wp-comments-post.php'. + * @type bool $novalidate Whether the novalidate attribute is added to the comment form. Default false. * @type string $id_form The comment form element id attribute. Default 'commentform'. * @type string $id_submit The comment submit element id attribute. Default 'submit'. * @type string $class_container The comment form container class attribute. Default 'comment-respond'. @@ -2646,6 +2648,7 @@ function comment_form( $args = array(), $post = null ) { ), 'comment_notes_after' => '', 'action' => site_url( '/wp-comments-post.php' ), + 'novalidate' => false, 'id_form' => 'commentform', 'id_submit' => 'submit', 'class_container' => 'comment-respond', @@ -2729,7 +2732,7 @@ function comment_form( $args = array(), $post = null ) { esc_url( $args['action'] ), esc_attr( $args['id_form'] ), esc_attr( $args['class_form'] ), - ( $html5 ? ' novalidate' : '' ) + ( $args['novalidate'] ? ' novalidate' : '' ) ); /** diff --git a/tests/phpunit/tests/comment/commentForm.php b/tests/phpunit/tests/comment/commentForm.php index 771cbc1b57919..e3dab07e249e8 100644 --- a/tests/phpunit/tests/comment/commentForm.php +++ b/tests/phpunit/tests/comment/commentForm.php @@ -193,4 +193,38 @@ public function test_comment_form_should_display_for_specified_post_when_passed_ $post_hidden_field = ""; $this->assertStringContainsString( $post_hidden_field, $form ); } + + /** + * Tests novalidate attribute on the comment form. + * + * @ticket 47595 + */ + public function test_comment_form_and_novalidate_attribute() { + $post_id = self::$post_id; + + // By default, the novalidate is not emitted. + $form = get_echo( 'comment_form', array( array(), $post_id ) ); + $p = new WP_HTML_Tag_Processor( $form ); + $this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' ); + $this->assertNull( $p->get_attribute( 'novalidate' ), 'Expected FORM to not have novalidate attribute by default.' ); + + // Opt in to the novalidate attribute by passing an arg to comment_form(). + $form = get_echo( 'comment_form', array( array( 'novalidate' => true ), $post_id ) ); + $p = new WP_HTML_Tag_Processor( $form ); + $this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' ); + $this->assertTrue( $p->get_attribute( 'novalidate' ), 'Expected FORM to have the novalidate attribute.' ); + + // Opt in to the novalidate attribute via the comment_form_defaults filter. + add_filter( + 'comment_form_defaults', + static function ( array $defaults ): array { + $defaults['novalidate'] = true; + return $defaults; + } + ); + $form = get_echo( 'comment_form', array( array(), $post_id ) ); + $p = new WP_HTML_Tag_Processor( $form ); + $this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' ); + $this->assertTrue( $p->get_attribute( 'novalidate' ), 'Expected FORM to have novalidate attribute.' ); + } }