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 a dataTurbo field to form and add test for fieldset TCK. #787

Merged
merged 6 commits into from
Apr 25, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<th:block th:fragment="form(form)" xmlns:th="http://www.thymeleaf.org"><form th:action="${form.action()}" th:method="${form.method()}" th:enctype="${form.enctype()}"><fieldset th:replace="~{fieldset/fieldset :: fieldset(${form.fieldset()})}"></fieldset></form></th:block>
<th:block th:fragment="form(form)" xmlns:th="http://www.thymeleaf.org"><form th:action="${form.action()}" th:method="${form.method()}" th:enctype="${form.enctype()}" th:data-turbo="${form.dataturbo()}"><fieldset th:replace="~{fieldset/fieldset :: fieldset(${form.fieldset()})}"></fieldset></form></th:block>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.views.fields.tck;

import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import io.micronaut.views.ViewsRenderer;
import io.micronaut.views.fields.Fieldset;
import io.micronaut.views.fields.Form;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;


@SuppressWarnings({"java:S5960"}) // Assertions are fine, these are tests
@MicronautTest(startApplication = false)
class FormDataTurboRenderTest {

@Test
void renderDataTurboTrueByDefault(ViewsRenderer<Map<String, Object>, ?> viewsRenderer) throws IOException {
assertNotNull(viewsRenderer);
Form form = new Form("/foo/bar", "post", new Fieldset(Collections.emptyList(), Collections.emptyList()), "application/x-www-form-urlencoded");
assertEquals("""
<form action="/foo/bar" method="post" enctype="application/x-www-form-urlencoded">\
</form>""",
TestUtils.render("fieldset/form.html", viewsRenderer, Map.of("form", form)));
}

@Test
void renderDataTurboTrue(ViewsRenderer<Map<String, Object>, ?> viewsRenderer) throws IOException {
assertNotNull(viewsRenderer);
Form form = new Form("/foo/bar", "post", new Fieldset(Collections.emptyList(), Collections.emptyList()), "application/x-www-form-urlencoded", true);
assertEquals("""
<form action="/foo/bar" method="post" enctype="application/x-www-form-urlencoded" data-turbo="true">\
</form>""",
TestUtils.render("fieldset/form.html", viewsRenderer, Map.of("form", form)));
}

@Test
void renderDataTurboFalse(ViewsRenderer<Map<String, Object>, ?> viewsRenderer) throws IOException {
assertNotNull(viewsRenderer);
Form form = new Form("/foo/bar", "post", new Fieldset(Collections.emptyList(), Collections.emptyList()), "application/x-www-form-urlencoded", false);
assertEquals("""
<form action="/foo/bar" method="post" enctype="application/x-www-form-urlencoded" data-turbo="false">\
</form>""",
TestUtils.render("fieldset/form.html", viewsRenderer, Map.of("form", form)));
}
}
44 changes: 40 additions & 4 deletions views-fieldset/src/main/java/io/micronaut/views/fields/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,59 @@

/**
* Representation of an HTML form.
* <p>
* When <code>dataturbo</code> is set to false, the form includes the <code>data-turbo="false"</code> attribute, which disables
* Turbo Drive on links and forms including descendants.
* See the <a href="https://turbo.hotwired.dev/reference/attributes">Turbo Data Attributes documentation</a>
* for more information.
*
* @author Sergio del Amo
* @since 4.1.0
* @param action Form Action
* @param method Form Method. either `get` or `post`
* @param fieldset Form fields
* @param enctype how the form-data should be encoded when submitting it to the server
* @param dataturbo enables Turbo Drive on a form, defaults to not disabled
*/
@Experimental
@EnctypePostRequired
@Introspected
public record Form(@NonNull @NotBlank String action,
@NonNull @NotBlank @Pattern(regexp = "get|post") String method,
@NonNull @NotNull @Valid Fieldset fieldset,
@Nullable @Pattern(regexp = "application/x-www-form-urlencoded|multipart/form-data|text/plain") String enctype) {
@Nullable @Pattern(regexp = "application/x-www-form-urlencoded|multipart/form-data|text/plain") String enctype,
@Nullable Boolean dataturbo) {

private static final String POST = "post";

/**
*
* @param action Form Action
* @param method Form Method. either `get` or `post`
* @param fieldset Form fields
* @param dataturbo Form data-turbo
*/
public Form(@NonNull String action,
@NonNull String method,
@NonNull Fieldset fieldset,
@Nullable Boolean dataturbo) {
this(action, method, fieldset, null, dataturbo);
}

/**
*
* @param action Form Action
* @param method Form Method. either `get` or `post`
* @param fieldset Form fields
* @param enctype how the form-data should be encoded when submitting it to the server
*/
public Form(@NonNull String action,
@NonNull String method,
@NonNull Fieldset fieldset,
@Nullable String enctype) {
this(action, method, fieldset, enctype, null);
}

/**
*
* @param action Form Action
Expand All @@ -50,7 +86,7 @@ public record Form(@NonNull @NotBlank String action,
public Form(@NonNull String action,
@NonNull String method,
@NonNull Fieldset fieldset) {
this(action, method, fieldset, null);
this(action, method, fieldset, null, null);
}

/**
Expand All @@ -60,7 +96,7 @@ public Form(@NonNull String action,
*/
public Form(@NonNull String action,
@NonNull Fieldset fieldset) {
this(action, POST, fieldset, null);
this(action, POST, fieldset, null, null);
}

/**
Expand All @@ -72,6 +108,6 @@ public Form(@NonNull String action,
public Form(@NonNull String action,
@NonNull Fieldset fieldset,
@Nullable String enctype) {
this(action, POST, fieldset, enctype);
this(action, POST, fieldset, enctype, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ void formValidation(Validator validator) {
assertFalse(validator.validate(new Form(null, "post", fieldset, "application/x-www-form-urlencoded")).isEmpty());

// method cannot be an empty string
assertFalse(validator.validate(new Form("/foo/bar", "", fieldset, null)).isEmpty());
assertFalse(validator.validate(new Form("/foo/bar", "", fieldset, null, null)).isEmpty());
// method cannot be null
assertFalse(validator.validate(new Form("/foo/bar", null, fieldset, null)).isEmpty());
assertFalse(validator.validate(new Form("/foo/bar", null, fieldset, null, null)).isEmpty());
// method can only be get or post
assertFalse(validator.validate(new Form("/foo/bar", "put", fieldset, null)).isEmpty());
assertFalse(validator.validate(new Form("/foo/bar", "put", fieldset, null, null)).isEmpty());

//method cannot be get if enctype
Set<ConstraintViolation<Form>> violations = validator.validate(new Form("/foo/bar", "get", fieldset, "application/x-www-form-urlencoded"));
Expand All @@ -50,11 +50,16 @@ void formValidation(Validator validator) {
assertFalse(validator.validate(new Form("/foo/bar", "post", fieldset, "text/html")).isEmpty());

// enctype can be null
assertTrue(validator.validate(new Form("/foo/bar", "post", fieldset, null)).isEmpty());
assertTrue(validator.validate(new Form("/foo/bar", "post", fieldset, null, null)).isEmpty());
assertTrue(validator.validate(new Form("/foo/bar", "post", fieldset, "application/x-www-form-urlencoded")).isEmpty());
assertTrue(validator.validate(new Form("/foo/bar", "post", fieldset, "multipart/form-data")).isEmpty());
assertTrue(validator.validate(new Form("/foo/bar", "post", fieldset, "text/plain")).isEmpty());
assertTrue(validator.validate(new Form("/foo/bar", "get", fieldset, null)).isEmpty());
assertTrue(validator.validate(new Form("/foo/bar", "get", fieldset, null, null)).isEmpty());

// dataturbo can be null, true or false
assertTrue(validator.validate(new Form("/foo/bar", "post", fieldset, null, null)).isEmpty());
assertTrue(validator.validate(new Form("/foo/bar", "post", fieldset, null, true)).isEmpty());
assertTrue(validator.validate(new Form("/foo/bar", "post", fieldset, null, false)).isEmpty());

}
}
Loading