Laravel - Multi-page form; Best practices

Asked

Viewed 312 times

0

I’m developing a system in Laravel where there will be several research projects (the validation of each form is a little complex).

Schema::create('projetos', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name'); // Nome do projeto
    $table->string('view_route'); //rota do projeto
    $table->string('model_name'); //model que irá usar
    $table->string('title')->unique();
    $table->string('description');
    $table->string('slug')->unique();
    $table->timestamps();
});


<form method="POST" action="{{ action('SurveyController@insert') }}" >
    <input type="hidden" name="id" value="1"/>  
    <div class="page-1">
        <!-- Diversos Inputs aqui -->
    </div>
    <div class="page-2">
        <!-- Diversos Inputs aqui -->
    </div>
    <div class="page-3">
        <!-- Diversos Inputs aqui -->
    </div>
    <div class="page-4">
        <!-- Diversos Inputs aqui -->
    </div>
</form>

You’ll only have one Controller to delegate each survey.

My idea is to make a single <form /> (with all the inputs inside) to send to the bank at once (will be sent only on the last page, by clicking on the button finish), validation would only be done at front-end and for paging, would use a plugin (such as jquery-steps for example). It’s good practice to name Model and the route of View in the database? then, I wouldn’t need to create several Controllers for each new research that will be developed, would only need to create the Model and View. for example:

Surveycontroller

//Irá ter apenas um ``Controller`` para delegar cada pesquisa.
public function index($id)
{
    //$id é o id do projeto
    $project = Projeto::findOrFail($id);
    return view($project->view_route)->with(compact(['project']));
}

public function insert(Request $request)
{
    //não sei se vai funcionar, mas a idéia é essa:
    $project = Projeto::findOrFail($request->input('id'));
    $model = $project->model_name;
    $newModel = new $model;
    $newModel->insert(
        $request->all() //os inputs terão o mesmo nome das colunas do banco
    );
}
  • Your question is confused, there’s room for improvement?

  • @Virgilionovic rephrased the question...

  • Multiple research projects! It’s not a table that contains all of this.? I find it problematic to do so! by the maintenance factor

  • The part of View is cool !!! I think functional with that plugin.

  • @Virgilionovic is that each project will be different from the other (different inputs and different questions); some will have different questions, or will have more or will have less. I could make a database with this schema: http://stackoverflow.com/questions/1764435/database-design-for-a-survey . but it will be a lot of work.

  • I think you have to think outside the box, within all possibilities, and not have a controller for everything, this may cause more problem than solution, well this is my opinion

  • I liked the solution, of course you will have a certain complexity to handle specific cases, but you can do this by delegating to a domain class that implements standard methods for your differentiated handling needs. What I don’t think is safe is the customer only validation, I believe you could also save the validation rules in the base as you do with views and models

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.