Validation works with annotations from the jakarta.validation library. These are generated into both frontend javascript and backend java checks. As a developer, you only need to configure the annotations and the HTML.
Back to overviewValidate a form being submitted by adding the @Valid annotation in front of the form as method parameter. The annotations used in your form determine both the frontend and backend validation checks.
<form m:submit="validateMyForm(:{form})">
<ul m:validation="all"></ul> <!-- This creates a generic validation list up top -->
<p><label for="email">Email: </label>
<input type="text" id="email" name="email" /></p>
<p m:validation="email"></p> <!-- This adds the validation message for email -->
<p><label for="yearOfBirth">Year of birth: </label>
<input type="text" id="yearOfBirth" name="yearOfBirth" /></p>
<p m:validation="yearOfBirth"></p> <!-- Same for the year -->
<input id="btn_displayForm" type="submit" value="Submit">
</form>
<div class="example-result" th:text="${result}"></div>
package sample.getmedusa.showcase.samples.textinputs.validation;
import io.getmedusa.medusa.core.annotation.UIEventPage;
import io.getmedusa.medusa.core.attributes.Attribute;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import java.util.List;
@UIEventPage(path = "/detail/sample/validation", file = "/pages/sample/validation.html")
public class ValidationController {
public List<Attribute> setupAttributes(){
return Attribute.$$("result", "");
}
public List<Attribute> validateMyForm(@Valid SampleForm form){
return Attribute.$$("result", form.email() + " " + form.yearOfBirth());
}
public record SampleForm(
@Email String email,
@NotBlank @Pattern(regexp = "^(19\\d{2}|2\\d{3}|3000)$") Integer yearOfBirth) { }
}
When a validation fails, it will show a message. This message can be customized. By default, each annotation has a generic english message associated with it. You can customize the messages by adding a properties tag to the annotation. This will affect both the frontend and backend messaging. The locale used is based on what the browser sends during the creation of the session.
<form m:submit="validateMyFormWithACustomMessage(:{form}.email)">
<p><label for="email">Email: </label> <input type="text" id="email" name="email" /></p>
<p m:validation="email"></p>
<input id="btn_displayForm" type="submit" value="Submit">
</form>
<div class="example-result" th:text="${result}"></div>
package sample.getmedusa.showcase.samples.textinputs.validation;
import io.getmedusa.medusa.core.annotation.UIEventPage;
import io.getmedusa.medusa.core.attributes.Attribute;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import java.util.List;
@UIEventPage(path = "/detail/sample/validation", file = "/pages/sample/validation.html")
public class ValidationController {
public List<Attribute> setupAttributes(){
return Attribute.$$("result", "");
}
public List<Attribute> validateMyFormWithACustomMessage(
@Valid @Email(message = "{my.custom.message}") String email){
return List.of(new Attribute("result", email.split("@")[0]));
}
}