6

我有一个包含 2 个字段的表单 -empnoname. 两者都用默认值填充。在视图中显示时,我想要empno的是只读且name可编辑的。

在视图创建中,我使用@leaveform.value.get.empno的是仅显示就绪并且工作正常。该问题仅在插入错误时发生([NoSuchElementException: None.get])

问题:

  1. 问题是返回错误没有值属性。我还能用什么来获得价值?
  2. 我可以跳过@inputText只读字段吗?

请参阅下面我的代码:

// ***** 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"))
        }
4

2 回答 2

16

使用表单助手不是强制性的。如果您使用它们,您可以readonly使用 CSS 传递属性或样式字段以使其看起来只读。

  • CSS 禁用 Twitter 引导程序:

    @inputText(
        editForm("createdOn"), 
        'id -> "createdOn", 
        'class -> "input-xlarge disabled", 
        '_label -> Messages("createdOn"), 
        '_help -> ""
    )
    
  • 传递可选属性:readonly

    @inputText(
        editForm("createdOn"), 
        'id -> "createdOn", 
        'class -> "input-xlarge", 
        '_label -> Messages("createdOn"), 
        'readonly -> "readonly", 
        '_help -> " This is read only"
    )
    
  • 您也可以不重新发送该字段,但显示其值:

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

Play field 现在返回 a Optional请参阅 docs。这意味着您可以从以下字段中获取值:

  • @form("fieldName").getValue.get(可以扔一个NPE
  • @form("fieldName").getValue.getOrElse("defaultValue")
于 2013-01-15T19:53:15.343 回答
0

在向用户返回表单时尝试使用 Flash 上下文:

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,埃里希

于 2013-01-17T09:27:18.877 回答