Skip to content

Commit

Permalink
Add type classes to allow use didChange
Browse files Browse the repository at this point in the history
  • Loading branch information
amitaibu committed Sep 16, 2023
1 parent fd70ef9 commit b7394ca
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions IHP/View/Form.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import IHP.ViewSupport
import qualified Text.Blaze.Html5 as Html5
import IHP.HSX.ToHtml
import GHC.Types
import IHP.ModelSupport (getModelName, inputValue, isNew, Id', InputValue)
import IHP.ModelSupport (didChange, getModelName, inputValue, isNew, Id', InputValue)
import IHP.HSX.QQ (hsx)
import IHP.View.Types
import IHP.View.Classes ()
Expand Down Expand Up @@ -724,10 +724,12 @@ selectField :: forall fieldName model item.
( ?formContext :: FormContext model
, HasField fieldName model (SelectValue item)
, HasField "meta" model MetaBag
, Typeable model
, KnownSymbol fieldName
, KnownSymbol (GetModelName model)
, CanSelect item
, InputValue (SelectValue item)
, Eq (SelectValue item)
) => Proxy fieldName -> [item] -> FormField
selectField field items = FormField
{ fieldType =
Expand All @@ -738,15 +740,7 @@ selectField field items = FormField
SelectInput (map itemToTuple items)
, fieldName = cs fieldName
, fieldLabel = removeIdSuffix $ fieldNameToFieldLabel (cs fieldName)
-- The select field is always displaying the value it gets from the nodel passed to the formFor.
-- The issue is introduced basically by the `newRecord @Record`. The `newRecord` call in the controller creates an empty record setting all fields to a default empty value.
-- The default empty value for UUIDs is the 00000000-0000-0000-0000-000000000000 and the default empty value for
-- enums is the first enum value.
-- Now, if we have a required field, we want to make sure the user selects a value, in the same
-- way they have to select for a reference field.
, fieldValue = if isNew model
then ""
else inputValue (getField @fieldName model :: SelectValue item)
, fieldValue = fieldValue
, fieldInputId = cs (lcfirst (getModelName @model) <> "_" <> cs fieldName)
, validatorResult = getValidationViolation field model
, fieldClass = ""
Expand All @@ -765,6 +759,15 @@ selectField field items = FormField
where
fieldName = symbolVal field
FormContext { model } = ?formContext
-- The select field is always displaying the value it gets from the nodel passed to the formFor.
-- The issue is introduced basically by the `newRecord @Record`. The `newRecord` call in the controller creates an empty record setting all fields to a default empty value.
-- The default empty value for UUIDs is the 00000000-0000-0000-0000-000000000000 and the default empty value for
-- enums is the first enum value.
-- Now, if we have a required field, we want to make sure the user selects a value, in the same
-- way they have to select for a reference field.
fieldValue = if isNew model && not (didChange field model)
then ""
else inputValue (getField @fieldName model :: SelectValue item)
{-# INLINE selectField #-}

-- | Radio require you to pass a list of possible values to select. We use the same mechanism as for for 'selectField'.
Expand Down

0 comments on commit b7394ca

Please sign in to comment.