You could parse your separate fields and populate a hidden field with the parsed data with javascript. Then it would already be getting submit as an input value.
How do I add an input value on the fly inside a request instance?
I'm validating a form's input using a request object. The form contains three fields (dd-mm-yyyy) that I parse into a date object so it can be validated as such. However, since I'm doing that right before the validation anyway, I want it to be stored in the request as its own input value so it can be passed on the the command bus when validation succeeds.
I just can't seem to figure out how to add it to the request as an input value.
Now I have got it working... Not that pretty yet though but I'll see if I can extract this to somewhere later.
In the Form Request's constructor I inject \Illuminate\Http\Request. I can then add my value to the request instance as such:
public function __construct(\Illuminate\Http\Request $request)
{
$request->request->add(['date_of_birth' => implode('-', $request->only('year', 'month', 'day'))]);
}
After that I can just add the rule for validating date_of_birth in the rules method.
This has three advantages in my opinion:
- Laravel will pick it up as if the parsed date were posted along with the form.
- All validation will be done before my controller's method is called and any invalid date will be returned as error (plus some other validation like before or after)
- I can use dispatchFrom to very easily generate my command DTO
All in all my controller will stay very very lean this way.
Please or to participate in this conversation.