diff --git a/src/main/scala/gitbucket/core/controller/IssuesController.scala b/src/main/scala/gitbucket/core/controller/IssuesController.scala index 41308764ef..3ee85c8b2f 100644 --- a/src/main/scala/gitbucket/core/controller/IssuesController.scala +++ b/src/main/scala/gitbucket/core/controller/IssuesController.scala @@ -156,15 +156,12 @@ trait IssuesControllerBase extends ControllerBase { params.toMap.foreach { case (key, value) => if (key.startsWith("custom-field-")) { - for { - field <- getCustomField( - repository.owner, - repository.name, - key.replaceFirst("^custom-field-", "").toInt - ) - behavior <- CustomFieldBehavior(field.fieldType) - } { - behavior.validate(field.fieldName, value, messages) match { + getCustomField( + repository.owner, + repository.name, + key.replaceFirst("^custom-field-", "").toInt + ).foreach { field => + CustomFieldBehavior.validate(field, value, messages) match { case None => insertOrUpdateCustomFieldValue(field, repository.owner, repository.name, issue.issueId, value) case Some(_) => halt(400) @@ -388,16 +385,13 @@ trait IssuesControllerBase extends ControllerBase { Ok("updated") }) - ajaxPost("/:owner/:repository/issues/:id/customfield_validation/:fieldId")(writableUsersOnly { repository => + ajaxPost("/:owner/:repository/issues/customfield_validation/:fieldId")(writableUsersOnly { repository => val fieldId = params("fieldId").toInt val value = params("value") getCustomField(repository.owner, repository.name, fieldId) .flatMap { field => - CustomFieldBehavior(field.fieldType).map { behavior => - behavior.validate(field.fieldName, value, messages) match { - case None => Ok() - case Some(error) => Ok(error) - } + CustomFieldBehavior.validate(field, value, messages).map { error => + Ok(error) } } .getOrElse(Ok()) @@ -411,9 +405,8 @@ trait IssuesControllerBase extends ControllerBase { for { _ <- getIssue(repository.owner, repository.name, issueId.toString) field <- getCustomField(repository.owner, repository.name, fieldId) - behavior <- CustomFieldBehavior(field.fieldType) } { - behavior.validate(field.fieldName, value, messages) match { + CustomFieldBehavior.validate(field, value, messages) match { case None => insertOrUpdateCustomFieldValue(field, repository.owner, repository.name, issueId, value) case Some(_) => halt(400) } diff --git a/src/main/scala/gitbucket/core/model/CustomField.scala b/src/main/scala/gitbucket/core/model/CustomField.scala index 2f1e21b6d0..d25c853d7b 100644 --- a/src/main/scala/gitbucket/core/model/CustomField.scala +++ b/src/main/scala/gitbucket/core/model/CustomField.scala @@ -37,7 +37,7 @@ case class CustomField( ) trait CustomFieldBehavior { - def createHtml(fieldId: Int): String + def createHtml(repository: RepositoryInfo, fieldId: Int)(implicit conext: Context): String def fieldHtml(repository: RepositoryInfo, issueId: Int, fieldId: Int, value: String, editable: Boolean)( implicit context: Context ): String @@ -45,6 +45,15 @@ trait CustomFieldBehavior { } object CustomFieldBehavior { + def validate(field: CustomField, value: String, messages: Messages): Option[String] = { + if (value.isEmpty) None + else { + CustomFieldBehavior(field.fieldType).flatMap { behavior => + behavior.validate(field.fieldName, value, messages) + } + } + } + def apply(fieldType: String): Option[CustomFieldBehavior] = { fieldType match { case "long" => Some(LongFieldBehavior) @@ -83,8 +92,29 @@ object CustomFieldBehavior { trait TextFieldBehavior extends CustomFieldBehavior { protected val fieldType = "text" - def createHtml(fieldId: Int): String = { - s"""""" + def createHtml(repository: RepositoryInfo, fieldId: Int)(implicit context: Context): String = { + val sb = new StringBuilder + sb.append( + s"""""" + ) + sb.append(s""" + |""".stripMargin) + sb.toString() } def fieldHtml(repository: RepositoryInfo, issueId: Int, fieldId: Int, value: String, editable: Boolean)( @@ -100,45 +130,45 @@ object CustomFieldBehavior { s"""""" ) sb.append(s""" |""".stripMargin) } diff --git a/src/main/twirl/gitbucket/core/issues/create.scala.html b/src/main/twirl/gitbucket/core/issues/create.scala.html index 35a73fb08e..045acbefad 100644 --- a/src/main/twirl/gitbucket/core/issues/create.scala.html +++ b/src/main/twirl/gitbucket/core/issues/create.scala.html @@ -28,7 +28,7 @@ elastic = true )