How to create a read-only field in a view

I have a form with two fields - empno and name . Both values ​​are populated with a default value. When displayed in a view, I want empno to be read-only and name to be editable.

In the creation view, I use @leaveform.value.get.empno to display finished and work fine. The problem only occurs when inserting with an error ([NoSuchElementException: None.get]) .

Questions:

  • The problem is the return error does not have the value property. What else can I use to get the value?
  • Can I skip @inputText for a read-only field?

See my code below:

 // ***** CONTROLLER *****// val leaveform = Form[LeaveModel]( mapping( "empno" -> nonEmptyText, "name" -> nonEmptyText )((no, empno) => LeaveModel(empno, name)) ((leave: LeaveModel) => Some(leave.empno, leave.name)) ) def create = withAuth { username => implicit request => // Define default values val empno = "STUDENT" val name = "" // Set default values val filledForm = leaveform.fill(LeaveModel(empno,name)) Ok(html.leave.form(filledForm)) } def insert = Action ( implicit request => { leaveform.bindFromRequest.fold( error => { BadRequest(html.leave.form(error)) // Question 1. Here is the error. }, leave => { LeaveModel.insert(leave) Redirect(routes.indexController.index()) } ) } ) // ***** VIEW START***** // @(leaveform: Form[LeaveModel]) @leaveform.value.get.empno @helper.form( action = (routes.LeaveController.update(oid)), 'id -> "leaveform") { @inputText(leaveform("empno")) // Question 2. @inputText(leaveform("name")) } 
+6
source share
2 answers

You do not have to use form helpers. If you use them, you can pass the readonly attribute or create a field using CSS so that it looks read-only.

  • Twitter bootstrap using CSS:

     @inputText( editForm("createdOn"), 'id -> "createdOn", 'class -> "input-xlarge disabled", '_label -> Messages("createdOn"), '_help -> "" ) 
  • Skip optional attribute: readonly

     @inputText( editForm("createdOn"), 'id -> "createdOn", 'class -> "input-xlarge", '_label -> Messages("createdOn"), 'readonly -> "readonly", '_help -> " This is read only" ) 
  • You can also not resubmit this field, but display its value:

     <span class="date">Created on: @editForm("createdOn").value</span> 
  • Update 2018-01-24

Playback field now returns Optional , see documents . This means that you can get the value from the field, for example:

  • @form("fieldName").getValue.get (may cause NPE )
  • @form("fieldName").getValue.getOrElse("defaultValue")
+15
source

Try using the Flash context when returning the form to the user:

 def create = withAuth { username => implicit request => // leaveForm initialization as before // use form data from flash if present val form = if (flash.get("error").isDefined) leaveForm.bind(flash.data) else leaveForm Ok(stakeholders.register(form)) } def insert = Action { implicit request => leaveForm.bindFromRequest.fold( hasErrors = { form => Redirect(routes.Leaves.create). // put correct route here flashing(Flash(form.data) + ("error" -> Messages("validation.errors"))) // customize error message }, leave => { LeaveModel.insert(leave) Redirect(routes.indexController.index()) } ) } 

HTH, Erich

0
source

All Articles