如何在html scala模板中使用helper验证java play2表单

时间:2016-04-28 10:10:44

标签: playframework playframework-2.0 playframework-2.2

  

我需要的是当我在我的疫苗接种场时出现现场错误   表格,当电话号码无效时,选项值为默认值   当金额小于100时。我们将非常感谢您的帮助。

我的home.scala.html:

 @(customerForm: Form[Users])

                                        @main("sending..") {

                                   <form action="@routes.Application.send()" method="post">


                                            <div class="form-group row  ">
                                                <label class="col-sm-2 form-control-label">Phone Number</label>
                                                <div class="col-sm-10">
                                                    <input
                                                    name="phoneNumber"
                                                    id="lastName"

                                                    class="form-control"
                                                    type="text"
                                                    placeholder="Phone Number">
                                                </div>
                                            </div>


                                                <div class="form-group row ">
                                                    <label class="col-sm-2 form-control-label">First Name</label>
                                                    <div class="col-sm-10">
                                                        <input
                                                        name="firstName"
                                                        id="firstName"

                                                        class="form-control"
                                                        type="text"
                                                        placeholder="First Name">
                                                    </div>
                                                   </div>



                                                <div class="form-group row">

                                                    <label class="col-sm-2 form-control-label">User</label>
                                                    <div class="col-sm-10">

                                                        <select class="form-control"
                                                        name="client"
                                                        id="client"
                                                        value="client"
                                                        >
                                                            <option value="select">select ..</option>
                                                            <option value="admin">admin</option>
                                                            <option value="supervisor">supervisor</option>
                                                            <option value="enduser">enduser</option>
                                                        </select>

                                                    </div>
                                                </div>


                                                <div class="form-group row  ">
                                                    <label class="col-sm-2 form-control-label">Amount</label>
                                                    <div class="col-sm-10">
                                                        <input
                                                        name="amount"
                                                        id=""
                                                        value="@customerForm("amount").value"
                                                        class="form-control"
                                                        type="number"
                                                        placeholder="Rwfr">
                                                    </div>
                                                </div> 
                                          </form>
                                           }

我希望模特herbenation是这样的:

@Entity
@Table(name = "Users")
public class Users extends Model {

@Id
public Long id;

@Required 
@Constraints.Pattern(value = "[0-9.+]+", message = "A valid phone number is required")
public String phoneNumber;

@Min(5)
@Required
public String firstName;


@Required
public String client ;

 @Min(100)
@Required
public int amount ;

}

我的ctroller方法如下:

   public static Result send( ) {


    Form<Users> formData_one = Form.form(Users.class).bindFromRequest();

   if (formData_one.hasErrors() )   {
        flash("error", " Please fill vaccant field ...");

          }
     else{

      }
       }

1 个答案:

答案 0 :(得分:0)

使用类似的东西

    @if(userForm.hasErrors) {
        <div class="alert alert-danger" role="alert">
        @for(error <- userForm.errors()) {
            <p>Error: @error._2.stream().findFirst().get().message()</p>
        }
        </div>
    }