From 8d79345095ed2583b19dcf2150ba0e43a3bb3cdc Mon Sep 17 00:00:00 2001 From: Miguel Nogueira Date: Wed, 6 May 2020 05:42:55 +0100 Subject: [PATCH] Store form structure in DB --- app/Form.php | 8 +++- app/Http/Controllers/FormController.php | 47 ++++++++++++++++++- .../dashboard/administration/forms.blade.php | 5 +- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/app/Form.php b/app/Form.php index d96da60..02c1dd2 100644 --- a/app/Form.php +++ b/app/Form.php @@ -6,5 +6,11 @@ use Illuminate\Database\Eloquent\Model; class Form extends Model { - // + public $fillable = [ + + 'formName', + 'formStructure', + 'formStatus' + + ]; } diff --git a/app/Http/Controllers/FormController.php b/app/Http/Controllers/FormController.php index 2f51fcb..ddbf059 100644 --- a/app/Http/Controllers/FormController.php +++ b/app/Http/Controllers/FormController.php @@ -2,7 +2,9 @@ namespace App\Http\Controllers; +use App\Form; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Validator; class FormController extends Controller { @@ -14,7 +16,50 @@ class FormController extends Controller public function saveForm(Request $request) { - dd($request->all()); + + $formFields = $request->all(); + + $formStructure = []; + $excludedNames = [ + '_token', + 'formName' // It's added outside the loop. Not excluding causes unwanted duplication. + ]; + $validator = [ + 'formName' => 'required|string|max:100' + ]; + + foreach ($formFields as $fieldName => $field) + { + if(!in_array($fieldName, $excludedNames)) + { + $validator[$fieldName . ".0"] = 'required|string'; + $validator[$fieldName . ".1"] = 'required|string'; + + $formStructure['fields'][$fieldName]['title'] = $field[0]; + $formStructure['fields'][$fieldName]['type'] = $field[1]; + } + } + + $validation = Validator::make($formFields, $validator); + + if (!$validation->fails()) + { + $storableFormStructure = json_encode($formStructure); + + Form::create( + [ + 'formName' => $formFields['formName'], + 'formStructure' => $storableFormStructure, + 'formStatus' => 'ACTIVE' + ] + ); + + $request->session()->flash('sucesss', 'Form created! You can now link this form to a vacancy.'); + return redirect()->back(); + } + + $request->session()->flash('errors', $validation->errors()); + return redirect()->back(); } } diff --git a/resources/views/dashboard/administration/forms.blade.php b/resources/views/dashboard/administration/forms.blade.php index e3f323e..ad84a6c 100644 --- a/resources/views/dashboard/administration/forms.blade.php +++ b/resources/views/dashboard/administration/forms.blade.php @@ -23,7 +23,10 @@ @csrf
- Form Builder + Form Builder + + +