Skip to content

What is the Laminar way of form validation? #82

Answered by raquo
eliasson asked this question in Q&A
Discussion options

You must be logged in to vote

There are many ways to do this, but here's one:

case class MyFormState(
  name: String = "",
  description: String = ""
) {
  def nameError: Option[String] = ???
  def descriptionError: Option[String] = ???
  def hasError: Boolean = nameError.nonEmpty || descriptionError.nonEmpty
}

val formState = Var(MyFormState())

Then you can display error messages easily:

val descriptionUpdater = formState.writer.contramap[String](newDescription => formState.update(_.copy(description = newDescription))

fieldSet(
  label("Description"),
  textArea(
    inContext(thisNode => onInput.mapTo(thisNode.ref.value) --> descriptionUpdater)))
  ),
  child.maybe <-- formState.signal.map(_.nameError.map(err =>

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@yurique
Comment options

@yurique
Comment options

Comment options

You must be logged in to vote
1 reply
@eliasson
Comment options

Answer selected by eliasson
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants