0
@(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?

Community
  • 1
  • 1
Art Vandelay
  • 55
  • 1
  • 9

1 Answers1

1

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.

Community
  • 1
  • 1
biesior
  • 55,576
  • 10
  • 125
  • 182
  • Thanks, didn't know that disabled caused that. The date field is just a String, so the formatting should not be the issue. – Art Vandelay Feb 24 '13 at 17:44
  • In such case try do bind it from request and use `Logger.debug(field)` to display it in the terminal. I have no idea, why simple `String` field isn't binded :/ – biesior Feb 24 '13 at 20:20
  • I used the debugger to check the request, but the request does not contain the field 'startDate'. The HTML looks like this:
    – Art Vandelay Mar 03 '13 at 21:03