Maik Klein
Maik Klein

Reputation: 16158

Play 2 - templates without helpers

@inputText(
                signupForm("email"), '_label -> "Email",
                '_help -> "Enter a valid email address."
            )

How would I write this in pure html?

I have no idea how i add the value to the signupForm, so that I can use it in my controller with bindfromRequest() (in html)

Edit:

I normally used this approach

final static Form<User> signupForm = form(User.class);

and the the binding process

Form<User> filledForm = signupForm.bindFromRequest();

and my rendered form looks like this:

<div class="control-group ">
  <label class="control-label" for="email">Email</label>
  <div class="controls">

    <input type="text" id="email" name="email" value="" >

    <span class="help-inline"></span>
  </div>
</div>

And this worked for me I was just curious how to use pure html, so I could create my own little helpers.

Edit2:

public static Result blank() {
        return ok(form.render(signupForm));
    }

and in the template itself

@(signupForm: Form[User])

Edit 3:

I don't know if this helps but the helper looks like this. (for the inputtext) I just have no idea what this means, scala looks really cryptic to me.

@(field: play.api.data.Field, args: (Symbol,Any)*)(implicit handler: FieldConstructor, lang: play.api.i18n.Lang)

@input(field, args:_*) { (id, name, value, htmlArgs) =>
    <input type="text" id="@id" name="@name" value="@value" @toHtmlArgs(htmlArgs)>
}

Upvotes: 2

Views: 1652

Answers (1)

biesior
biesior

Reputation: 55798

Use your browser to check the source coderendered by Play and copy/paste the HTML into your template.

In general the most interesting for you is proper inserting value to the manually created field:

<input type="text" name="email" value='@signupForm.field("email").value' />

It's also important to set the proper name attribut otherwise filling Form in controller will fail.

Edit:

Of course in blank action your signupForm is empty so that's normal that there's no value, in next action let's name it saveBlank, you need to fill the form with request's data, validate and save. If there are errors in form you need to render form.scala.view again with data binded to the form from previous request:

public static Result saveBlank(){
    signupForm = signupForm.bindFromRequest();
    if (signupForm.hasErrors()){
        return badRequest(form.render(signupForm));
    }

    User user = new User();
    user = signupForm.get();
    user.save();

    return ok("user saved);
}

of course if you'll want to edit existing user, you have to prefill it with data from DB ie:

public static Result edit(Long id){
    signupform = signupForm.fill(User.find.byId(id));
    return ok(form.render(signupForm));
}

(note: I wrote it just from top of my head, so check pls if there are no typos or errors)

Finally you don't need to use Form object in every case, you can also use DynamicForm:

public static Result saySomething(){
    DynamicForm df = form().bindFromRequest();
    return("you entered :" + df.get("email"));
}

or even in one line:

public static Result sayShorter(){
    return("you entered :" + form().bindFromRequest().get("email"));
}

Upvotes: 4

Related Questions