Skip to content
This repository has been archived by the owner on Jul 8, 2021. It is now read-only.

Commit

Permalink
Build validation on form submit
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam-PVTL authored Apr 2, 2020
1 parent e0ad5e9 commit 081e881
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/Http/Controllers/EnquiryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function submit(Request $request)
{
$form = Form::findOrFail($request->id);

$request->validate($this->buildValidation($form));

// Get $formData and $filesKeys verifying the MIME of files.
$formDataAndFilesKeys = $this->getFormDataAndFilesKeys($form, $request);
if($formDataAndFilesKeys instanceof RedirectResponse){
Expand All @@ -72,7 +74,7 @@ public function submit(Request $request)
if (empty($form->mailto)) {
$form->mailto = !empty(setting('forms.default_to_email'))
? setting('forms.default_to_email')
: false;
: '[email protected]';
}

// The from address
Expand All @@ -99,11 +101,9 @@ public function submit(Request $request)
// Debug/Preview the email
// return (new EnquiryMailable($form, $formData, $filesKeys))->render();

// Send the email if enabled
if ($form->mailto) {
Mail::to(array_map('trim', explode(',', $form->mailto)))
->send(new EnquiryMailable($form, $formData, $filesKeys));
}
// Send the email
Mail::to(array_map('trim', explode(',', $form->mailto)))
->send(new EnquiryMailable($form, $formData, $filesKeys));

return redirect()
->back()
Expand Down Expand Up @@ -273,4 +273,30 @@ protected function getMimesFromForm($form){
}
return $mimes;
}


/**
* Builds a validation array based on required / email input form fields.
* Returns an array to be fed into the validate() function.
*
* @param Form $form
* @return array $validationArray
*/
protected function buildValidation($form)
{
$validationArray = [];
foreach ($form->inputs as $input) {
if ($input->type === 'email') {
$additionalValidation = '|email';
} else {
$additionalValidation = '';
}

if ($input->required) {
$validationArray[$input->label] = 'required' . $additionalValidation;
}
}

return $validationArray;
}
}

0 comments on commit 081e881

Please sign in to comment.