Skip to content
This repository was archived by the owner on Oct 20, 2025. It is now read-only.

Commit 6eddd16

Browse files
Johan Montenijpascalbaljet
andauthored
Fixes for FormBuilder validation to match expected behaviour as described in the docs (#373)
Co-authored-by: Pascal Baljet <[email protected]>
1 parent 4abed07 commit 6eddd16

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

app/app/Http/Controllers/FormBuilderController.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function simple()
4444

4545
public function storeSimple(Request $request)
4646
{
47-
$result = $this->simpleForm()->validate($request);
47+
$result = $request->validate($this->simpleForm()->getRules());
4848

4949
return response()->json(['result' => $result]);
5050
}
@@ -80,11 +80,9 @@ public function model()
8080
]);
8181
}
8282

83-
public function storeModel(Request $request)
83+
public function storeModel(Request $request, ModelbindingForm $form)
8484
{
85-
$rules = ModelbindingForm::make()->getRules();
86-
87-
$result = $request->validate($rules);
85+
$result = $form->validate($request);
8886

8987
return response()->json(['result' => $result]);
9088
}

src/AbstractForm.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ProtoneMedia\Splade;
44

5+
use Illuminate\Http\Request;
6+
57
abstract class AbstractForm
68
{
79
/**
@@ -10,7 +12,7 @@ abstract class AbstractForm
1012
private ?SpladeForm $for = null;
1113

1214
/**
13-
* Adds fields to the form
15+
* Adds fields to the form.
1416
*/
1517
public function fields(): array
1618
{
@@ -30,7 +32,7 @@ public static function make(...$arguments): SpladeForm
3032
}
3133

3234
/**
33-
* Creates a new SpladeForm instance from the 'build()' method of this class
35+
* Creates a new SpladeForm instance from the 'build()' method of this class.
3436
*/
3537
public function build(): SpladeForm
3638
{
@@ -59,12 +61,25 @@ public function configure(SpladeForm $form)
5961
}
6062

6163
/**
62-
* Get the rules that are configured for the form
64+
* Get the rules that are configured for the form.
6365
*
6466
* @param mixed ...$arguments
6567
*/
6668
public static function rules(...$arguments): array
6769
{
6870
return self::make(...$arguments)->getRules();
6971
}
72+
73+
/**
74+
* Validate the request with the rules of the form.
75+
*
76+
* @param mixed ...$params
77+
*/
78+
public function validate(?Request $request = null, ...$params): array
79+
{
80+
/** @var Request */
81+
$request = $request ?? request();
82+
83+
return $this->build()->validate($request, ...$params);
84+
}
7085
}

src/SpladeForm.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace ProtoneMedia\Splade;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Http\Request;
67
use Illuminate\Support\Arr;
78

89
class SpladeForm
910
{
1011
protected ?AbstractForm $configurator = null;
1112

12-
protected $data = [];
13+
protected array|Model $data = [];
1314

1415
protected array $fields = [];
1516

@@ -51,7 +52,7 @@ public function setConfigurator(AbstractForm $configurator): self
5152
}
5253

5354
/**
54-
* Sets the id for the form
55+
* Sets the id for the form.
5556
*
5657
* @return $this
5758
*/
@@ -63,7 +64,7 @@ public function id(string $id = ''): self
6364
}
6465

6566
/**
66-
* Sets the action for the form
67+
* Sets the action for the form.
6768
*
6869
* @return $this
6970
*/
@@ -75,7 +76,7 @@ public function action(string $action = 'POST'): self
7576
}
7677

7778
/**
78-
* Add css-class(es) to the form
79+
* Add css-class(es) to the form.
7980
*
8081
* @return $this
8182
*/
@@ -112,7 +113,7 @@ public function confirm(
112113
}
113114

114115
/**
115-
* Sets the values for the form fields
116+
* Sets the values for the form fields.
116117
*
117118
* @return $this
118119
*/
@@ -124,7 +125,7 @@ public function fill($data): self
124125
}
125126

126127
/**
127-
* Adds fields to the form
128+
* Adds fields to the form.
128129
*
129130
* @return $this
130131
*/
@@ -136,7 +137,7 @@ public function fields(array $fields = []): self
136137
}
137138

138139
/**
139-
* Prevent the page from scrolling to the top after submit
140+
* Prevent the page from scrolling to the top after submit.
140141
*
141142
* @param bool $stay
142143
* @return $this
@@ -149,9 +150,9 @@ public function preserveScroll(bool $preserve_scroll = true): self
149150
}
150151

151152
/**
152-
* Require the user to confirm their password within the confirmation dialog
153+
* Require the user to confirm their password within the confirmation dialog.
153154
*
154-
* Add `Route::spladePasswordConfirmation();` to your routes to make this work
155+
* Add `Route::spladePasswordConfirmation();` to your routes to make this work.
155156
*
156157
* @return $this
157158
*/
@@ -175,7 +176,7 @@ public function requirePassword(
175176
}
176177

177178
/**
178-
* Prevent navigation on submit
179+
* Prevent navigation on submit.
179180
*
180181
* @param string $actionOnSuccess reset|restore
181182
* @return $this
@@ -194,7 +195,7 @@ public function stay(bool $stay = true, string $actionOnSuccess = ''): self
194195
* Submit the form whenever a value changes.
195196
*
196197
* If one or morge fieldnames are provided in $watch_fields,
197-
* the form will only be submitted on changes on these fields
198+
* the form will only be submitted on changes on these fields.
198199
*
199200
* @param array|string|null $watchFields
200201
* @return $this
@@ -213,7 +214,7 @@ public function submitOnChange(
213214
}
214215

215216
/**
216-
* Set the method of the form
217+
* Set the method of the form.
217218
*
218219
* @return $this
219220
*/
@@ -225,63 +226,63 @@ public function method(string $method = 'POST'): self
225226
}
226227

227228
/**
228-
* Returns the id for the form
229+
* Returns the id for the form.
229230
*/
230231
public function getId(): string
231232
{
232233
return $this->id;
233234
}
234235

235236
/**
236-
* Returns the action for the form
237+
* Returns the action for the form.
237238
*/
238239
public function getAction(): string
239240
{
240241
return $this->action;
241242
}
242243

243244
/**
244-
* Returns the data for the form
245+
* Returns the data for the form.
245246
*/
246247
public function getClass(): array|string
247248
{
248249
return $this->class;
249250
}
250251

251252
/**
252-
* Returns the data for the form
253+
* Returns the data for the form.
253254
*/
254255
public function getData()
255256
{
256257
return $this->data;
257258
}
258259

259260
/**
260-
* Returns all fields of the form
261+
* Returns all fields of the form.
261262
*/
262263
public function getFields(): array
263264
{
264265
return $this->fields;
265266
}
266267

267268
/**
268-
* Returns the method for the form
269+
* Returns the method for the form.
269270
*/
270271
public function getMethod(): string
271272
{
272273
return $this->method;
273274
}
274275

275276
/**
276-
* Return an option for the form arguments
277+
* Return an option for the form arguments.
277278
*/
278279
public function getOption(string $option): array|bool|string|null
279280
{
280281
return $this->options[$option] ?? false;
281282
}
282283

283284
/**
284-
* Returns the forms rules
285+
* Returns the forms rules.
285286
*/
286287
public function getRules(): array
287288
{
@@ -295,6 +296,7 @@ public function getRules(): array
295296
*/
296297
public function validate(?Request $request = null, ...$params): array
297298
{
299+
/** @var Request */
298300
$request = $request ?? request();
299301

300302
return $request->validate($this->getRules(), ...$params);

0 commit comments

Comments
 (0)