Skip to content

Spam protection using HoneyPot field #276

Open
@martinpesout

Description

@martinpesout

I'm trying to create a form with spam protection using the HoneyPot field. So my code is something like this:

const contactFormDefaultValues = { email: "", message: "", somethingSweet: "" }

export const contactFormSchema = z.object({
  message: z
    .string()
    .min(1, "Please enter some message.")
    .min(10, "Can you be more specific?"),
  email: z
    .string()
    .min(1, "Please enter your e-mail.")
    .email("Please enter a valid e-mail."),
  somethingSweet: z // bot honey trap
    .string()
    .max(0, "Please leave this field empty."),
})

const [contactForm, { Form, Field }] = useForm<ContactForm, ResponseData>({
  loader: { value: contactFormDefaultValues },
  action: useFormAction(),
  validate: zodForm$(contactFormSchema),
})

return (
  <Form id="contact">
    <Field name="message">
      {(field, props) => (
        <input
          {...props}
          value={field.value}
          aria-invalid={!!field.error}
          placeholder="Your message"
          required
        />
      )}
    </Field>

    <Field name="email">
      {(field, props) => (
        <input
          {...props}
          value={field.value}
          aria-invalid={!!field.error}
          type="email"
          placeholder="your@email.com"
          required
        />
      )}
    </Field>

    <Field name="somethingSweet">
      {(field, props) => (
        <input
          {...props}
          value={field.value}
          aria-invalid={!!field.error}
          type="text"
        />
      )}
    </Field>
  </Form>
)

When I use only keyboard interaction, everything works (even validation of somethingSweet field is ok). However, this is useless to me in protecting against spam. Spam is still coming. I'll try to simulate filling the value of the field with id somethingSweet using JavaScript. For example, in DevTools I use $('#somethingSweet').value = 'test' or manually change the DOM again using DevTools. When I do this, validation isn't triggered because whole form frameworks isn't able to recognise that some value has been entered. How I can do HoneyPot spam protection using Modular Forms? Is that possible? Or am I doing something wrong?

I also tried to check values present in useFormAction() method, but I'm getting the same situation. When I fill in the value using $('#somethingSweet').value = 'test' in my browser, this field looks still as unchanged.

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions