ripper234
ripper234

Reputation: 230038

Why aren't my Play! validation errors displayed?

I'm trying to follow Play!'s documentation for validation. I have a controller with 3 actions:

  1. add() - an "add website" form
  2. added() - called when the add form is submitted, does the actual adding.
  3. edit() - a successful added() call leads to editing the added website (a failed added leads back to the add() form).

When validation fails, I am redirected to the add() action as planned. The #{ifErrors} template works (<h1>Oops…</h1> is displayed) ... but there are two problems:

  1. The website URL parameter is not "flashed" - in the add form, it doesn't appear in the appropriate field.
  2. Specific errors do not appear - this field remains empty: <span class="error">#{error 'website.url' /}</span>

What am I missing?

My Controller:

public class Sites extends Controller {
    private static final WebsiteRepository websiteRepo = new WebsiteRepository();

    public static void add() {
        render();
    }

    public static void added(@Valid Website website) {
        if (Validation.hasErrors()) {
            Validation.keep();
            params.flash();
            add();
        }

        websiteRepo.save(website);
        edit(website.id);
    }

    public static void edit(long websiteId) {
        Website website = websiteRepo.getById(websiteId);
        render(website);
    }

}

add.html

<div id="addsite">

    <h1>Add a new site</h1>

    #{ifErrors}
   <h1>Oops…</h1>
    #{/ifErrors}

    #{form @added()}

        #{if flash.error}
            <p class="error">
                &{flash.error}
            </p>
        #{/if}
        #{if flash.success}
            <p class="success">
                &{flash.success}
            </p>
        #{/if}


        <p id="url-field">
            <label for="url">URL</label>
            <input type="text" name="website.url" id="url" class="url-field" value="${flash.url}" />
            <span class="error">#{error 'website.url' /}</span>
        </p>

        <p id="add-field">
            <input type="submit" id="add" value="Add" />
        </p>
    #{/form}
</div>

Upvotes: 1

Views: 891

Answers (2)

Marius Soutier
Marius Soutier

Reputation: 11274

The value format is wrong, for complex objects use:

${flash['website.url']}

Edit: I cloned your repo and you have the following errors:

  • In your template replace ${flash[website.url]} with ${flash['website.url']}
  • You marked the URL as @Required, but not as @URL; add the @URL annotation and the error will display "Not a valid URL"
  • An owner is required, but there is no way to provide one, so there will always be a validation error for this

Upvotes: 2

Pere Villega
Pere Villega

Reputation: 16439

Instead of calling add() try:

 render("Sites/add.html", website);

where the first term is the path to the Add template and the second the form parameter you get in the POST call.

The first parameter can be referenced also as "@add" (shortcut)

Upvotes: 0

Related Questions