Store form structure in DB

This commit is contained in:
Miguel Nogueira 2020-05-06 05:42:55 +01:00
parent 2df5bf144d
commit 8d79345095
3 changed files with 57 additions and 3 deletions

View File

@ -6,5 +6,11 @@ use Illuminate\Database\Eloquent\Model;
class Form extends Model class Form extends Model
{ {
// public $fillable = [
'formName',
'formStructure',
'formStatus'
];
} }

View File

@ -2,7 +2,9 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Form;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class FormController extends Controller class FormController extends Controller
{ {
@ -14,7 +16,50 @@ class FormController extends Controller
public function saveForm(Request $request) 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();
} }
} }

View File

@ -23,7 +23,10 @@
@csrf @csrf
<fieldset id="buildyourform"> <fieldset id="buildyourform">
<legend>Form Builder</legend> <legend class="text-center">Form Builder</legend>
<input type="text" name="formName" class="form-control mb-5" placeholder="Name your form..." required>
</fieldset> </fieldset>
</form> </form>