scala - 如何在 View 中创建只读字段

标签 scala playframework-2.0

我有一个包含2个字段的表单-empnoname。两者都填满默认值。在 View 中显示时,我希望empno是只读的,并且name是可编辑的。

在创建 View 时,我使用@leaveform.value.get.empno显示仅准备就绪并且可以正常工作。仅在插入时出现问题([NoSuchElementException: None.get])错误。

问题:

  • 问题是返回错误没有值属性。我还能用什么来获得值(value)?
  • 我可以跳过只读字段的@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"))
            }
    

    最佳答案

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

  • Twitter bootstrap 已被CSS禁用:
    @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字段将返回Optional see the docs。这意味着您可以从以下字段中获取值:
  • @form("fieldName").getValue.get(可以抛出NPE)
  • @form("fieldName").getValue.getOrElse("defaultValue")
  • 关于scala - 如何在 View 中创建只读字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14342234/

    相关文章:

    java - 玩2.2.2(Scala),如何处理HttpServletRequests

    playframework-2.0 - Play Framework 2 显示来自数据库或外部资源的图像

    scala - 使用单个开发/测试机器同时拥有 Spark 进程分区

    scala - Akka Streams - 根据某些谓词拆分传入源数据

    scala - object db 不是 play 包的成员

    java - 玩Java的框架?版本 1.x.x 或 2.x.x

    java - 如何从 PlayFramework 中的 Java 代码访问 SBT 设置?

    scala - 错误 : could not find implicit value for parameter timeout: akka. util.Timeout

    scala - Spark,在 Scala 中添加具有相同值的新列

    Scala尾递归