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

feat: Add enctype to Form #676

Merged
merged 6 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@
<form th:fragment="form(form)" xmlns:th="http://www.thymeleaf.org" th:action="${form.action()}" th:method="${form.method()}"><fieldset th:replace="~{fieldset/fieldset :: fieldset(${form.fieldset()})}"></fieldset></form>
<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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 FormEncTypeRenderTest {

@Test
void render(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)));
}
}
39 changes: 38 additions & 1 deletion views-fieldset/src/main/java/io/micronaut/views/fields/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.views.exceptions.ViewRenderingException;
import jakarta.validation.constraints.Pattern;

/**
* Representation of an HTML form.
Expand All @@ -25,8 +29,41 @@
* @param action Form Action
* @param method Form Method. For example `post`
* @param fieldset Form fields
* @param enctype how the form-data should be encoded when submitting it to the server
*/
@Experimental
@Introspected
public record Form(String action, String method, Fieldset fieldset) {
public record Form(@NonNull String action,
@Nullable @Pattern(regexp = "get|post") String method,
@NonNull Fieldset fieldset,
@Nullable @Pattern(regexp = "application/x-www-form-urlencoded|multipart/form-data|text/plain") String enctype) {
sdelamo marked this conversation as resolved.
Show resolved Hide resolved

private static final String POST = "post";

public Form(@NonNull String action, @NonNull String method, @NonNull Fieldset fieldset) {
this(action, method, fieldset, null);
}

public Form(@NonNull String action, @NonNull Fieldset fieldset) {
this(action, POST, fieldset, null);
}

public Form(@NonNull String action,
@NonNull Fieldset fieldset,
@Nullable String enctype) {
this(action, POST, fieldset, enctype);
}

public Form(@NonNull String action,
@NonNull String method,
@NonNull Fieldset fieldset,
@Nullable String enctype) {
if (enctype != null && !method.equals(POST)) {
throw new IllegalArgumentException("enctype attribute can be used only if method equals post");
}
this.action = action;
this.method = method;
this.fieldset = fieldset;
this.enctype = enctype;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.micronaut.core.beans.BeanIntrospection;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.junit.jupiter.api.Assertions.*;

class FormTest {
Expand All @@ -11,4 +13,9 @@ void isAnnotatedWithIntrospected() {
assertDoesNotThrow(() -> BeanIntrospection.getIntrospection(Form.class));
}

@Test
void formEnctypeMethodGet() {
assertThrows(IllegalArgumentException.class, () -> new Form("/foo/bar", "get", new Fieldset(Collections.emptyList(), Collections.emptyList()), "application/x-www-form-urlencoded"));
assertDoesNotThrow(() -> new Form("/foo/bar", "post", new Fieldset(Collections.emptyList(), Collections.emptyList()), "application/x-www-form-urlencoded"));
}
}
Loading