IHP is a modern batteries-included Web Framework, built on top of Haskell and Nix.

1 min read Original article ↗
instance View NewView where
    html NewView { .. } = [hsx|
        <h1>New Comment</h1>

        {renderForm comment}
    |]

renderForm comment = formFor comment [hsx|
    {hiddenField #threadId}

    <!-- Labels + Validation Results are taken care of -->
    {textareaField #body}

    {submitButton}
|]
instance View NewView where
    html NewView { .. } = [hsx|
        <h1>New Comment</h1>

        {renderForm comment}
    |]

renderForm comment = formFor comment [hsx|
    {hiddenField #threadId}

    {(textareaField #body) {
        fieldLabel = "Your Comment:",
        helpText = "You can use markdown here."
    }}

    <p>
        <!-- Use any HTML inside your forms -->
        Please double check your comment
        for spelling mistakes.
    </p>

    {submitButton}
|]
action CreateCommentAction = do
    let comment = newRecord @Comment

    comment
        |> fill @["body", "threadId"]
        |> set #userId currentUserId

        -- Actual validation here
        |> validateField #body nonEmpty
        |> validateField #threadId nonEmpty

        |> ifValid \case
            -- Validation Failed -> Render form + errors
            Left comment -> render NewView { .. }

            -- Validation Good
            Right comment -> do

                -- Insert comment to DB
                comment <- comment |> createRecord

                let commentId = get #id comment

                -- Redirect to comment
                redirectTo ShowCommentAction { commentId }