Skip to content

Form with conditional fields based on type #645

Answered by edmundhung
asaf asked this question in Q&A
Discussion options

You must be logged in to vote

Is this what you are looking for?

export function EmailForm() {
  const [lastResult, action] = useFormState(login, undefined);
  const [form, fields] = useForm({
    defaultValue: {
      type: 'smtp',
      config: {
        host: 'smtp.foo.com',
      },
    },
    // Sync the result of last submission
    lastResult,

    // Reuse the validation logic on the client
    onValidate({ formData }) {
      return parseWithZod(formData, { schema: appEmailConfigSchema });
    },

    // Validate the form on blur event triggered
    shouldValidate: 'onBlur',
    shouldRevalidate: 'onInput',
  });

  // To access fields of the `config` object
  const configFieldset = fields.config.getFieldset();

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by asaf
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@edmundhung
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants