Skip to content

Commit

Permalink
Show error if account already exists (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquecbuss authored Sep 9, 2022
1 parent 5fb773c commit e9ca38c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
24 changes: 22 additions & 2 deletions src/elm/Form.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Form exposing
, optional, introspect, list, mapValues, mapOutput, withValidationStrategy, ValidationStrategy(..)
, textField, richText, toggle, checkbox, radio, select, file, fileMultiple, datePicker, userPicker, userPickerMultiple, arbitrary, arbitraryWith, unsafeArbitrary
, view, viewWithoutSubmit, Model, init, Msg, update, updateValues, getValue, hasFieldsLoading, msgToString
, withDisabled
, withDisabled, withShowAllErrors
, isDisabled, isShowingAllErrors
, parse
)
Expand Down Expand Up @@ -88,7 +88,7 @@ documentation if you're stuck.
### Changing attributes and state
@docs withDisabled
@docs withDisabled, withShowAllErrors
### Checking attributes and state
Expand Down Expand Up @@ -1023,6 +1023,26 @@ withDisabled disabled (Model model) =
Model { model | disabled = disabled }


{-| Control whether the form should show all errors or not. This is useful when
you want to show all errors after the user tried to submit the form, and you
have some custom action that doesn't automatically trigger all of the errors.
-}
withShowAllErrors : Bool -> Model values -> Model values
withShowAllErrors showAllErrors (Model model) =
let
(ErrorTracking errorTracking) =
model.errorTracking
in
Model
{ model
| errorTracking =
ErrorTracking
{ errorTracking
| showAllErrors = showAllErrors
}
}


{-| Checks if the form is disabled. It's useful to disable submit buttons when
using `viewWithoutSubmit` or when some parts of the UI should be disabled when
the form is disabled.
Expand Down
15 changes: 14 additions & 1 deletion src/elm/Page/Register.elm
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ formOutputDecoder =
]


withShowAllErrors : Bool -> FormModel -> FormModel
withShowAllErrors showAllErrors formModel =
case formModel of
NaturalForm form ->
NaturalForm (Form.withShowAllErrors showAllErrors form)

JuridicalForm form ->
JuridicalForm (Form.withShowAllErrors showAllErrors form)

DefaultForm form ->
DefaultForm (Form.withShowAllErrors showAllErrors form)


type alias SignUpFields =
{ name : String
, email : String
Expand Down Expand Up @@ -619,7 +632,7 @@ update _ msg model ({ shared } as guest) =
Set.insert (Eos.nameToString account)
unavailableAccounts
}
formModel
(withShowAllErrors True formModel)

_ ->
model.status
Expand Down
16 changes: 9 additions & 7 deletions src/elm/Page/Register/Common.elm
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,18 @@ accountNameField ({ t } as translators) { unavailableAccounts } =
{ parser =
Form.Validate.succeed
>> Form.Validate.eosName
>> Form.Validate.custom
(\accountName ->
if Set.member (Eos.Account.nameToString accountName) unavailableAccounts then
Err (\translators_ -> translators_.t "error.alreadyTaken")

else
Ok accountName
)
>> Form.Validate.validate translators
, value = .account
, update = \account input -> { input | account = account }
, externalError =
\{ account } ->
if Set.member account unavailableAccounts then
Just (t "error.alreadyTaken")

else
Nothing
, externalError = always Nothing
}


Expand Down

0 comments on commit e9ca38c

Please sign in to comment.