简体   繁体   中英

Laravel post form error

I'm having a problem for the first time when i submit a form.

When i submit the form it doesn't go to post route and i don't know why.

my post route is that:

    Route::post('/app/add-new-db', function()
  {
    $rules = array(
          'name' => 'required|min:4',
          'file' => 'required'
    );

    $validator = Validator::make(Input::all(), $rules);
    if ($validator->fails()) {

      // get the error messages from the validator
      $messages = $validator->messages();

      // redirect our user back to the form with the errors from the validator
      return Redirect::to('app/add-new-db')
         ->withErrors($validator);

     }
     else
     {
        $name = Input::get('name');
        $fname = pathinfo(Input::file('file')->getClientOriginalName(), PATHINFO_FILENAME);
        $fext = Input::file('file')->getClientOriginalExtension();
        echo $fname.$fext;
        //Input::file('file')->move(base_path() . '/public/dbs/', $fname.$fext);

        /*
        DB::connection('mysql')->insert('insert into databases (name, logotipo) values (?, ?)',
         [$name, $fname.$fext]);
         */
       //return Redirect::to('app/settings/');
     }
  });

And my html:

    <div class="mdl-cell mdl-cell--12-col content">
  {!! Form::open(['url'=>'app/add-new-db', 'files'=>true]) !!}
  <div class="mdl-grid no-verticall">
    <div class="mdl-cell mdl-cell--12-col">
      <h4>Adicionar Base de Dados</h4>
      <div class="divider"></div>
    </div>
    <div class="mdl-cell mdl-cell--6-col">
      <div class="form-group">
        {!! Form::label('name', 'Nome: ') !!}
        {!! Form::text('name', null, ['id'=> 'name', 'class' => 'form-control']) !!}
      </div>
      <div class="form-group">
        {!! Form::label('image', 'Logotipo:') !!}
        {!! Form::file('image', ['class'=>'form-control']) !!}
        @if ($errors->has('image'))
          <span class="error">{{ $errors->first('image') }}</span>
        @endIf
      </div>
      <div class="form-group">
        {!! Form::submit('Adicionar', ['id'=>'add-new-db', 'class'=>'btn btn-default']) !!}
        <p class="text-success"><?php echo Session::get('success'); ?></p>
      </div>
    </div>
  </div>
  {!! Form::close() !!}
</div>

I'm loading this from from a jquery get:

    function addDB()
   {
     $( "#settings-new-db" ).click(function(e)
     {
       $.get('/app/add-new-db', function(response)
       {
          $('.content-settings').html("");
          $('.content-settings').html(response);
          componentHandler.upgradeDom();
       });
       e.preventDefault();
     });
   }

When i try to submit the form i'm getting 302 found in network console from chrome.

I'm doing forms at this way and it is happens for the first time. Anyone can help me?

Thanks

Fix the name attribute for your image upload field. You refer it as image in the form but you are trying to fetch it as file in your controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM