Skip to content

Instantly share code, notes, and snippets.

@boska
Last active January 22, 2025 22:06
Show Gist options
  • Select an option

  • Save boska/afd74a25bdf61a991c76a69369f4c499 to your computer and use it in GitHub Desktop.

Select an option

Save boska/afd74a25bdf61a991c76a69369f4c499 to your computer and use it in GitHub Desktop.
Form in Swift
struct FormRendererView_Previews: PreviewProvider {
static var previews: some View {
FormRendererView_Previews.feedbackExample
}
static var feedbackExample: some View {
FormRendererView(
submitButtonTitle: "Send Feedback",
formFields: [
FormFieldConfiguration(
id: "name",
type: .text,
label: "Name",
placeholder: "Enter your name",
isRequired: true,
validation: FormValidation(
minLength: 3,
maxLength: 50,
pattern: nil,
customRule: nil,
errorMessage: "Name must be between 3 and 50 characters long"
),
defaultValue: nil,
options: nil
),
FormFieldConfiguration(
id: "email",
type: .email,
label: "Email",
placeholder: "Enter your email",
isRequired: true,
validation: FormValidation(
minLength: nil,
maxLength: nil,
pattern: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}",
customRule: nil,
errorMessage: "Please enter a valid email address"
),
defaultValue: nil,
options: nil
),
FormFieldConfiguration(
id: "message",
type: .text,
label: "Message",
placeholder: "Enter your message",
isRequired: true,
validation: FormValidation(
minLength: 10,
maxLength: 500,
pattern: nil,
customRule: nil,
errorMessage: "Message must be between 10 and 500 characters long"
),
defaultValue: nil,
options: nil
)
])
}
static var loginExample: some View {
FormRendererView(
submitButtonTitle: "Login",
formFields: [
FormFieldConfiguration(
id: "email",
type: .email,
label: "Email",
placeholder: "Enter your email",
isRequired: true,
validation: FormValidation(
minLength: nil,
maxLength: nil,
pattern: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}",
customRule: nil,
errorMessage: "Please enter a valid email address"
),
defaultValue: nil,
options: nil
),
FormFieldConfiguration(
id: "password",
type: .secureText,
label: "Password",
placeholder: "Enter your password",
isRequired: true,
validation: FormValidation(
minLength: 6,
maxLength: 20,
pattern: nil,
customRule: nil,
errorMessage: "Password must be at least 6 characters long"
),
defaultValue: nil,
options: nil
),
FormFieldConfiguration(
id: "stayLoggedIn",
type: .checkbox,
label: "Stay logged in on this device",
placeholder: nil,
isRequired: false,
validation: nil,
defaultValue: nil,
options: nil
)
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment