20
20
The source code for this website can be found at <a href =" {{ $gitHubRepo } }" >GitHub</a >.
21
21
</p >
22
22
<p >
23
- Below are instruction on how to set up in Laravel 7 <a href =" /en_US/programmer/create" >this dynamic form here</a >.
23
+ Below are instruction on how to set up in Laravel 8 <a href =" /en_US/programmer/create" >this dynamic form here</a >.
24
24
The form contains several dynamic sections (fields added by JavaScript). It also has dynamic elements within
25
25
other dynamic blocks. This makes building and validating the form more complex. Some quite complex validation
26
26
rules are applied.
32
32
</p >
33
33
<ul >
34
34
<li >
35
- Have a working instance of Laravel 7
35
+ Have a working instance of Laravel 8
36
36
</li >
37
37
<li >
38
38
Install Laravel Collective and create some form components
56
56
<br >
57
57
These files should be customized for your projects particular needs.
58
58
</li >
59
- <li >app/Providers - add <a href =" {{ $gitHubRepo } } /blob/master/app/Providers/FormServiceProvider.php" >FormServiceProvider.php</a ></li >
59
+ <li >app/Providers - add FormServiceProvider.php as follows:
60
+ <pre >
61
+ < ; ?php
62
+
63
+ namespace App\Providers;
64
+
65
+ use Illuminate\Support\ServiceProvider;
66
+ use Form;
67
+
68
+ class FormServiceProvider extends ServiceProvider
69
+ {
70
+ /**
71
+ * Register services.
72
+ *
73
+ * @return void
74
+ */
75
+ public function register()
76
+ {
77
+ }
78
+
79
+ /**
80
+ * Bootstrap services.
81
+ *
82
+ * @return void
83
+ */
84
+ public function boot()
85
+ {
86
+ Form::component('myInput', 'components.form.myInput', ['type', 'name', 'value' => null, 'attributes' => []]);
87
+ Form::component('myTextarea', 'components.form.myTextarea', ['name', 'value' => null, 'attributes' => []]);
88
+ Form::component('mySelect', 'components.form.mySelect', ['name', 'options', 'value' => null, 'attributes' => []]);
89
+ Form::component('myCheckboxList', 'components.form.myCheckboxList', ['name', 'options', 'attributes' => []]);
90
+ Form::component('myRadioList', 'components.form.myRadioList', ['name', 'options', 'attributes' => []]);
91
+ }
92
+ }
93
+ </pre >
94
+ </li >
60
95
<li >config/app.php - add App\Providers\FormServiceProvider::class to 'providers' array</li >
61
96
</ul >
62
97
</li >
@@ -256,7 +291,7 @@ function languageSwitch()
256
291
public function add(\App\Model\ProgrammingExperienceFormOptions $formOptions)
257
292
{
258
293
return view('programmer/edit', [
259
- 'countries' => $formOptions->getCountries(' en_US'),
294
+ 'countries' => $formOptions->getCountries(request('locale', ' en_US') ),
260
295
'languages' => $formOptions->getProgrammingLanguages(),
261
296
'workTypes' => $formOptions->getWorkTypeOptions(),
262
297
]);
@@ -324,10 +359,6 @@ function attributeTemplate($view, $attributes=[])
324
359
325
360
/**
326
361
* Used on a laravelcollective/html form to iterate through an array of fields
327
- *
328
- * @param string $field
329
- * @param \Illuminate\Database\Eloquent\Model|null $model
330
- * @return array
331
362
*/
332
363
function formIterator(?Eloquent\Model $model, string $field): array
333
364
{
@@ -474,10 +505,6 @@ function formIterator(?Eloquent\Model $model, string $field): array
474
505
<pre >
475
506
/**
476
507
* Used on a laravelcollective/html form to provide JavaScript with next key to use when adding to array of fields
477
- *
478
- * @param \Illuminate\Database\Eloquent\Model|null $model
479
- * @param string $parentField
480
- * @return int
481
508
*/
482
509
function nextKey(?Eloquent\Model $model, string $parentField): int
483
510
{
@@ -528,6 +555,9 @@ function nextKey(?Eloquent\Model $model, string $parentField): int
528
555
<li >
529
556
Follow the instructions from reCAPTCHA to install Version 2 onto the form
530
557
</li >
558
+ <li >
559
+ In the .env file add the reCAPTCHA key (as RECAPTCHA_KEY) and secret (as RECAPTCHA_SECRET).
560
+ </li >
531
561
<li >
532
562
To show an error message when reCAPTCHA fails to verify the user is human place the following
533
563
code at the top of the form
@@ -551,7 +581,33 @@ function nextKey(?Eloquent\Model $model, string $parentField): int
551
581
I have moved it into the Repository directory.
552
582
</li >
553
583
<li >
554
- Create the class <a href =" {{ $gitHubRepo } } /blob/master/app/Model/ReCaptchaV3.php" >App\Model\ReCaptchaV3</a >.
584
+ Create the validator for reCAPTCHA:
585
+ <ul >
586
+ <li >
587
+ In composer.json require - "guzzlehttp/guzzle": "^7.0.1"
588
+ <br >
589
+ Command: composer update
590
+ </li >
591
+ <li >
592
+ Create the class <a href =" {{ $gitHubRepo } } /blob/master/app/Entity/IpAddress.php" >App\Entity\IpAddress</a >.
593
+ </li >
594
+ <li >
595
+ Create the class <a href =" {{ $gitHubRepo } } /blob/master/app/Model/ReCaptchaV3.php" >App\Model\ReCaptchaV3</a >.
596
+ </li >
597
+ <li >
598
+ In App\Providers\FormServiceProvider above the class declaration add:<br >
599
+ use App\Model\ReCaptchaV3;<br >
600
+ use GuzzleHttp\Client;<br >
601
+ <br >
602
+ In the register() method add:
603
+ <pre >
604
+ $this->app->bind(ReCaptchaV3::class, function ($app) {
605
+ return new ReCaptchaV3(new Client(), env('RECAPTCHA_SECRET'), \Log::getLogger(), null);
606
+ });
607
+ </pre >
608
+ The reCAPTCHA secret (RECAPTCHA_SECRET) is to be held in the .env file.
609
+ </li >
610
+ </ul >
555
611
</li >
556
612
<li >
557
613
Run command "php artisan make:request ProgrammingExperienceSave" to create the file
@@ -561,7 +617,7 @@ class in it. This class will contain the form validation logic.
561
617
Add the contents from the
562
618
<a href =" {{ $gitHubRepo } } /blob/master/app/Http/Requests/ProgrammingExperienceSave.php" target =" _blank" >GitHub Repository</a >
563
619
to this file. The code here is quite self-explanatory if you are already familiar with writing
564
- <a href =" https://laravel.com/docs/7 .x/validation" target =" _blank" >Laravel Validation logic</a >.
620
+ <a href =" https://laravel.com/docs/8 .x/validation" target =" _blank" >Laravel Validation logic</a >.
565
621
</li >
566
622
<li >
567
623
Above the Controller (i.e. ProgrammerExperienceController) add the lines:
@@ -614,7 +670,7 @@ public function edit(FormOptions $formOptions)
614
670
615
671
return view('programmer/edit', [
616
672
'personExperience' => $personExp,
617
- 'countries' => $formOptions->getCountries(' en_US'),
673
+ 'countries' => $formOptions->getCountries(request('locale', ' en_US') ),
618
674
'languages' => $formOptions->getProgrammingLanguages(),
619
675
'workTypes' => $formOptions->getWorkTypeOptions(),
620
676
]);
0 commit comments