Question

@(myForm: Form[FormObject]) 

@import helper._
@import helper.twitterBootstrap._

@main("Test") {
  @form(routes.Application.save) {
    @input(myForm("number"), '_label -> "Number") { (id, name, value, args) =>
      @if(value.isEmpty) {
        <input type="text" name="@name" id="@id" value="@value">
      } else {
        <input type="text" name="@name" id="@id" value="@value" disabled>
      }
    }
    @input(myForm("startDate"), '_label -> "Start Date") { (id, name, value, args) =>
      <div class="input-append date datepicker" data-date="@value" data-date-format="dd-mm-yyyy">
        <input class="span2" size="16" type="text" value="@value"><span class="add-on"><i class="icon-calendar"></i></span>
      </div>
    }
    <button type="submit" class="btn" title="Save"><i class="icon-ok"></i></button>
  }
}

public class FormObject {
public String number;
public String startDate;
}
  1. The first issue is that when the value of field 'number' is not empty (and therefore shown disabled) is not bind back to FormObject, so I lose this value.

  2. The second issue is that field 'startDate' is not bind to FormObject.

    Am I missing something?

Was it helpful?

Solution

First: It's common mistake - disabled attribute disables the field even from sending, so it isn't even available to play a the request time. Use readonly instead. (more about disabled vs readonly)

    @input(myForm("number"), '_label -> "Number") { (id, name, value, args) =>
       <input type="text" name="@name" id="@id" value="@value" @if(!value.isEmpty){ readonly="readonly" }>
    }

Maybe you will need to use some CSS/JS for accenting that the field is readonly, otherwise often users considers it as a bug (I can't edit this field and don't know why)

Second: If it's really String field - I don't know the reason. If it's Date problem is caused by format change, Play can not parse automatically. You will need to parse it as well with the given format in your controller before the save/update.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top