Skip to content

Commit

Permalink
Fix error rendering in owners/find
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyer committed Feb 22, 2023
1 parent 0393ab9 commit 7fb1995
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.springframework.samples.petclinic.owner;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -28,10 +26,13 @@
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.support.BindStatus;
import org.springframework.web.servlet.support.RequestContext;

import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Mustache.CustomContext;
import com.samskivert.mustache.Template.Fragment;

import jakarta.servlet.http.HttpServletRequest;

/**
* Utilities for rendering views of owners and pets.
Expand All @@ -55,11 +56,16 @@ public FormWidgetControllerAdvice(Mustache.Compiler compiler) {
}

@ModelAttribute("form")
public CustomContext form(Map<String, Object> model) {
Map<String, Form> map = new HashMap<>();
public CustomContext form(Map<String, Object> model, HttpServletRequest context) {
Map<String, Object> map = new HashMap<>();
return key -> {
if (!map.containsKey(key)) {
map.put(key, new Form(key, model.get(key)));
Form form = new Form(key, model.get(key));
map.put(key, form);
BindStatus status = new RequestContext(context, model).getBindStatus(key + ".*");
if (status != null) {
form.put("fields", status);
}
}
return map.get(key);
};
Expand Down Expand Up @@ -108,18 +114,13 @@ private String match(Pattern pattern, String body, String fallback) {

}

class Form extends HashMap<String, Object> implements Mustache.Lambda {
class Form extends HashMap<String, Object> {

public Form(String name, Object target) {
put("name", name);
put("target", target);
}

@Override
public void execute(Fragment frag, Writer out) throws IOException {
frag.execute(this, out);
}

public Object getTarget() {
return get("target");
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/templates/owners/findOwners.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
<h2>Find Owners</h2>

<form action="/owners" method="get"
{{#form.owner}}
class="form-horizontal" id="search-owner-form">
<div class="form-group">
<div class="control-group" id="lastNameGroup">
<label class="col-sm-2 control-label">Last name </label>
<div class="col-sm-10">
<input name="lastName" class="form-control" value="{{owner.lastName}}" size="30"
maxlength="80" /> <span class="help-inline">
{{#fields.allErrors}}<p>{{.}}</p>{{/fields.allErrors}}
{{#fields.errorMessages}}<p>{{.}}</p>{{/fields.errorMessages}}
</span>
</div>
</div>
Expand All @@ -29,6 +30,7 @@
</div>
</div>

{{/form.owner}}
</form>

<br />
Expand Down

0 comments on commit 7fb1995

Please sign in to comment.