Search code examples
haskellihp

IHP: Can I still use formFor if I want to include a field that's not a column of the record?


Say I've got some NewView for a record Order which is table order in the database with columns qt and item, but when users submit to create a order row I'd like to also send along some extra data to CreateOrderAction which will be stored elsewhere or thrown away or logged or whatever. Can I still use formFor to get the auto-generated form with validation and type-checked fields?

Possible use-cases I've come across so far: honeypot for comment fields, extra row to be created in other tables, count of duplicate entries to add.

If I just

renderForm :: Order -> Html
renderForm order = formFor order [hsx|
    {(textField #source) { fieldLabel = "where did you hear about us?" }}
    {(numberField #qt)}
    {(textField #item)}
    |]

where source is not in Order, I get Could not deduce (InputValue value0).


Solution

  • This was so simple it seems I had found the solution already in my previous IHP project, hopefully a self-answer will save me time in the future :)

    Just don't use the textField (or other *field helpers), but write out the field html explicitly, and you can still use formFor for the other fields:

    renderForm :: Order -> Html
    renderForm Order = formFor order [hsx|
        <div class="form-group" id="form-group-order_source">
            <label for="order_source">where did you hear about us?</label>
            <input type="text" name="source" id="order_source" class="form-control" />
        </div>
        {(numberField #qt)}
        {(textField #item)}
        |]