forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[4.x] Support Laravel Precognition on front end forms (statamic#8886)
Co-authored-by: Jason Varga <[email protected]>
- Loading branch information
1 parent
2113059
commit 61b7ee5
Showing
5 changed files
with
152 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace Statamic\Http\Requests; | ||
|
||
use Illuminate\Contracts\Validation\Validator; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Support\Facades\URL; | ||
use Illuminate\Support\Traits\Localizable; | ||
use Illuminate\Validation\ValidationException; | ||
use Statamic\Facades\Site; | ||
use Statamic\Support\Arr; | ||
use Statamic\Validation\AllowedFile; | ||
|
||
class FrontendFormRequest extends FormRequest | ||
{ | ||
use Localizable; | ||
|
||
private $assets = []; | ||
private $cachedFields; | ||
|
||
/** | ||
* Get any assets in the request | ||
*/ | ||
public function assets(): array | ||
{ | ||
return $this->assets; | ||
} | ||
|
||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Optionally override the redirect url based on the presence of _error_redirect | ||
*/ | ||
protected function getRedirectUrl() | ||
{ | ||
$url = $this->redirector->getUrlGenerator(); | ||
|
||
if ($redirect = $this->input('_error_redirect')) { | ||
return $url->to($redirect); | ||
} | ||
|
||
return $url->previous(); | ||
} | ||
|
||
public function validator() | ||
{ | ||
$fields = $this->getFormFields(); | ||
|
||
return $fields | ||
->validator() | ||
->withRules($this->extraRules($fields)) | ||
->validator(); | ||
} | ||
|
||
protected function failedValidation(Validator $validator) | ||
{ | ||
if ($this->ajax()) { | ||
$errors = $validator->errors(); | ||
|
||
$response = response([ | ||
'errors' => $errors->all(), | ||
'error' => collect($errors->messages())->map(function ($errors, $field) { | ||
return $errors[0]; | ||
})->all(), | ||
], 400); | ||
|
||
throw (new ValidationException($validator, $response)); | ||
} | ||
|
||
return parent::failedValidation($validator); | ||
} | ||
|
||
private function extraRules($fields) | ||
{ | ||
return $fields->all() | ||
->filter(fn ($field) => $field->fieldtype()->handle() === 'assets') | ||
->mapWithKeys(function ($field) { | ||
return [$field->handle().'.*' => ['file', new AllowedFile]]; | ||
}) | ||
->all(); | ||
} | ||
|
||
private function getFormFields() | ||
{ | ||
if ($this->cachedFields) { | ||
return $this->cachedFields; | ||
} | ||
|
||
$form = $this->route()->parameter('form'); | ||
|
||
$this->errorBag = 'form.'.$form->handle(); | ||
|
||
$fields = $form->blueprint()->fields(); | ||
|
||
$this->assets = $this->normalizeAssetsValues($fields); | ||
|
||
$values = array_merge($this->all(), $this->assets); | ||
|
||
return $this->cachedFields = $fields->addValues($values); | ||
} | ||
|
||
private function normalizeAssetsValues($fields) | ||
{ | ||
// The assets fieldtype is expecting an array, even for `max_files: 1`, but we don't want to force that on the front end. | ||
return $fields->all() | ||
->filter(fn ($field) => $field->fieldtype()->handle() === 'assets' && $this->hasFile($field->handle())) | ||
->map(fn ($field) => Arr::wrap($this->file($field->handle()))) | ||
->all(); | ||
} | ||
|
||
public function validateResolved() | ||
{ | ||
$site = Site::findByUrl(URL::previous()) ?? Site::default(); | ||
|
||
return $this->withLocale($site->lang(), fn () => parent::validateResolved()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters