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
5 changes: 4 additions & 1 deletion src/wp-includes/comment-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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'.
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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' : '' )
);

/**
Expand Down
34 changes: 34 additions & 0 deletions tests/phpunit/tests/comment/commentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,38 @@ public function test_comment_form_should_display_for_specified_post_when_passed_
$post_hidden_field = "<input type='hidden' name='comment_post_ID' value='{$post_id}' id='comment_post_ID' />";
$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.' );
}
}
Loading