Skip to content

Commit 2a12986

Browse files
committed
[EH] simplify request validation
1 parent 67572e3 commit 2a12986

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
2121
### Security
2222
- Nothing
2323

24-
## 1.0.0 - 2018-05-XX
24+
## 1.1.0 - 2018-05-23
25+
26+
### Added
27+
- now request validation can be used without overwriting `ajaxStore()` - just use the `setAjaxStoreRequest()` setter.
28+
29+
## 1.0.0/1.0.5 - 2018-05-21
2530

2631
### Added
2732
- initial Version

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,28 @@ If needed you are free to use the `data_source` attribute from the original fiel
158158

159159
### Request validation
160160

161-
You can also use Request Validation! Just copy the `ajaxStore()` method to your `EntityCrudController` and replace `Request` by your desired request (usually just `StoreRequest`).
161+
You can also use Request Validation! Just set the `$ajaxStoreRequest` property by using the provided setter method:
162+
163+
```php
164+
<?php
165+
166+
use Webfactor\Laravel\Backpack\InstantFields\InstantFields;
167+
168+
class EntityCrudController extends CrudController
169+
{
170+
use InstantFields;
171+
172+
public function setup()
173+
{
174+
// other Backpack options
175+
176+
$this->setAjaxEntity('entity');
177+
$this->setAjaxStoreRequest(\RequestNamespace\StoreRequest::class);
178+
179+
// fields/columns definitions
180+
}
181+
}
182+
```
162183

163184
### Fields
164185

src/Traits/InstantFields.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Webfactor\Laravel\Backpack\InstantFields;
44

5+
use Illuminate\Foundation\Http\FormRequest;
56
use Illuminate\Http\Request;
67

78
trait InstantFields
@@ -28,6 +29,26 @@ public function setAjaxEntity(string $entity)
2829
$this->ajaxEntity = $entity;
2930
}
3031

32+
/**
33+
* Returns instance of FormRequest for request validation if $ajaxStoreRequest is set
34+
*
35+
* @return FormRequest|null
36+
*/
37+
public function getAjaxStoreRequest()
38+
{
39+
return isset($this->storeAjaxRequest) ? new $this->storeAjaxRequest : null;
40+
}
41+
42+
/**
43+
* Sets $ajaxStoreRequest property to use for request validation
44+
*
45+
* @return void
46+
*/
47+
public function setAjaxStoreRequest(string $storeAjaxRequest)
48+
{
49+
$this->storeAjaxRequest = $storeAjaxRequest;
50+
}
51+
3152
/**
3253
* Handles the incoming ajax requests by default
3354
* @param Request $request
@@ -88,7 +109,7 @@ public function ajaxCreate()
88109

89110
/**
90111
* Checks permission and tries to store on-the-fly entity. If you want to enable request validation,
91-
* please copy this method in your EntityCrudController and replace Request by your StoreRequest.
112+
* please set your StoreRequest class by using setAjaxStoreRequest() in your EntityCrudController
92113
*
93114
* @param StoreRequest $request
94115
* @return \Illuminate\Http\JsonResponse
@@ -99,13 +120,35 @@ public function ajaxStore(Request $request)
99120
return $this->ajaxRespondNoPermission();
100121
}
101122

123+
if ($storeRequest = $this->getAjaxStoreRequest()) {
124+
if ($errors = $this->ajaxValidationFails($request, $storeRequest->rules())) {
125+
return response()->json($errors, 422);
126+
}
127+
}
128+
102129
if (parent::storeCrud($request)) {
103130
return $this->ajaxRespondCreated();
104131
}
105132

106133
return $this->ajaxRespondError();
107134
}
108135

136+
/**
137+
* Validates the request and returns an error bag if it fails
138+
*
139+
* @return mixed
140+
*/
141+
public function ajaxValidationFails(Request $request, array $rules)
142+
{
143+
$validator = \Validator::make($request->all(), $rules);
144+
145+
if ($validator->fails()) {
146+
return $validator->errors();
147+
}
148+
149+
return false;
150+
}
151+
109152
/**
110153
* Responses 403 No Permission
111154
*

0 commit comments

Comments
 (0)