Safe HaskellNone

IHP.ValidationSupport.ValidateIsUnique

Synopsis

Documentation

validateIsUnique :: forall field model savedModel validationState fieldValue validationStateValue fetchedModel modelId savedModelId. (savedModel ~ NormalizeModel model, ?modelContext :: ModelContext, FromRow savedModel, KnownSymbol field, HasField field model fieldValue, HasField field savedModel fieldValue, KnownSymbol (GetTableName savedModel), ToField fieldValue, EqOrIsOperator fieldValue, HasField "meta" model MetaBag, SetField "meta" model MetaBag, HasField "id" savedModel savedModelId, HasField "id" model modelId, savedModelId ~ modelId, Eq modelId) => Proxy field -> model -> IO model Source #

Validates that e.g. an email (or another field) is unique across all users before inserting.

This validator reads the given field name (e.g. email) from the record, and runs a database query to check that there is no other record using the same field value (e.g. email value).

Example: Validate that an email is unique

action CreateUserAction = do
    let user = newRecord @User
    user
        |> fill @'["email"]
        |> validateIsUnique #email
        >>= ifValid \case
            Left user -> render NewView { .. } 
            Right user -> do
                createRecord user
                redirectTo UsersAction