Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm a beginner to Laravel, and also to front-end oriented programming (most of my background is in database management). I'm attempting to put together a multiple page form that allows users to move through a hierarchy of data:

  1. on the first page, they can select a project or create a new one,
  2. on the second they select a subsection of that project (or again, make a new one)
  3. on the third page they can perform file uploads or specific tasks for that subsection.

My hacked-together way of doing this was simply to have each page after the first be a POST route that grabs the identifying data of the previous step to generate the correct select fields for the view and populate the database properly. However, after some research I've learned that directly calling views from a post route is a clumsy way of doing things (mixing up GET and POST methodology), made especially evident by the fact that validation error redirects cannot work with post routes. Thus, if the user ever messes up in the form, rather than being redirected to the last stage with a list of errors, they end up with a Method Not Allowed exception.

What is a better (RESTful) way of achieving this kind of multiple-page form? I'm not very good at front end stuff and would rather avoid Ajax if I can.

Here's some of my code as it stands:

route file:

Route::get('newentry', 'NewEntryController@index');
Route::get('newentry/selectjob','NewEntryController@selectjob');
Route::post('newentry/selectday','NewEntryController@selectday');
Route::post('newentry/upload','NewEntryController@upload');

example method for a form page

public function selectday()
        {
            //get post from job form and validate
            $request = Request::all();
            $job_id = intval(Request::get('job_id'));

            //create data variable, pass job_id for hidden field
            $data = [];
            $data['job_id'] = $job_id;
            //apply ruleset
            $rules = array(
                'job_id'    => 'required'
            );
            $validator = Validator::make($request, $rules);

            if ($validator->passes()) {

                //get the subset of days
                $data['days'] = DB::table('daily_runs')
                    ->where('job_id',$job_id)
                    ->orderBy('id', 'asc')
                    ->lists('start_time','id');

                return View::make('newentry.day')->with('data', $data);
            } else {

                return Redirect::to('newentry/selectjob')->withInput()->withErrors($validator); //this redirect will not work. 
            }
        } //end selectday

This code will work if the user makes no mistakes (as I mentioned, if they mess up the validation redirect will throw a Method Not Allowed exception) but from what I'm hearing I'm approaching it with poor coding practice. What steps could I take in the controller/view structure to make it better?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
324 views
Welcome To Ask or Share your Answers For Others

1 Answer

You have to do this in Ajax on one page. It is nonsense using one page for every select list.

This is your FIRST SELECT LIST

<form>
  <input class="target" type="text" value="Field 1">
  <select class="target">
    <option value="option1" selected="selected">Option 1</option>
    <option value="option2">Option 2</option>
  </select>
</form>

THIS IS THE AJAX CODE

$(document).ready(function(){

// when the select list changes

$( ".target" ).change(function(e) {
e.preventDefault();

// gets the selected value
var selectedoption = $(this).text();


// sends it via Ajax Post to the controller

$.ajax({
type:"POST",
url: 'your uri to controller',
data: {'whatever waits at your controller' :selectedoption}

        });

    });  


});  

Note: In Laravel to avoid an issue with CSRF you need to check my other post, you just need to enter the URI to your controller in the page as I say on my link

Laravel: workaround the CSRF token via Ajax issue :

with the value the Controller gets, it sends the query to the model, gets the reply from the model and then returns the result back to the view.

In the view you are still on the same page where the first select list was.

The second select list picks that value and uses it in a foreach loop that populates the second select list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...