diff --git a/IHP/View/Form.hs b/IHP/View/Form.hs index 032db6b2f..67f5f39e9 100644 --- a/IHP/View/Form.hs +++ b/IHP/View/Form.hs @@ -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 () @@ -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 = @@ -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 = "" @@ -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'.