MisstypingMeerkat's avatar

Track form submit errors?

I'm having a form on my site with a number of inputs. When i look on my Analytics, the number of people who visit the page vs. the number of people who actually sucessfully submit the form is ~10:1. I assume this might be because of spam bots, but i would like to verify it. My form submit page returns usefull error messages on screen for each field that must be fixed, but maybe some error messages are unclear and users dont understand it.

Here is my code:

TestController::store(Request $request) {
        $validatedData = $request->validate([
            'product_id' => 'required|numeric|min:2',
            'variant_id' => 'required|numeric|min:2',
            'shipping_date' => 'required|date|after:yesterday|date_format:Y-m-d\TH:i',
            'email' => 'email',
            'color' => 'required|string',
            'custom_request'=> 'required|string|min:50'
        ]);
		...
		// process and store user input
}

As far as I understand it, I only get $validatedData if the validation is successfull, otherwise the requests redirects back with error messages. How would i write all invalid requests including their incomplete data to the laravel logfile so i can analyze it later?

0 likes
5 replies
Ben Taylor's avatar

If this is just a temporary measure while you assess your form, you could just stick

logger($request);

before you validate the inputs.

This will fill up your log file pretty quick depending on how many requests your endpoint gets, so make sure you clear it out regularly.

Tray2's avatar

Since you can create your own error messages, you could probably do a method call in your validation code.

'custom' => [
    'email' => [
        'required' => $this->requiredMessage('email'),
        'max' => 'Your email address is too long!'
    ],
],
protected function requiredMessage(string $field): string
{
		Validation::create(['reason' => 'invalid email']);
        return ''We need to know your email address!';
}

https://laravel.com/docs/10.x/validation#custom-messages-for-specific-attributes

Snapey's avatar

I would dump errors to log on the page that shows the form.

Don't look at it like how do I intercept validation when you can simply report any errors passed into the form from a previous attempt.

MisstypingMeerkat's avatar

@Snapey Yes, i'm already doing that so it shows exactly which field fails which validation - however I'm trying to figure out which fields cause the most friction for users to see where the UI needs improvements, or maybe some labels or help texts are not clear enough...

Snapey's avatar

@MisstypingMeerkat ok, so you are saying that writing to log file is not enough.

Let me see, how else could you store data? You wouldnt happen to have a database would you?

Please or to participate in this conversation.