Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 form validation request not working properly

When I hit submit button nothing happens its just refreshing the page.

Here's my code:

app/Http/routes.php

Route::group(['middleware' => ['web']], function () {
    Route::get('profile/edit', 'UserController@editProfile');
    Route::post('update_name', 'UserController@updateName');
});

app/Http/Request/UpdateNameRequest.php

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;
use Illuminate\Support\Facades\Auth;

class UpdateNameRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'first_name' => 'required|min:2|alpha',
            'last_name' => 'required|min:2|alpha',
        ];
    }
}

app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
use App\User;

class UserController extends Controller
{
    public function __construct() {
        $this->middleware('auth');
    }

    public function editProfile() {
        if (Auth::user()->role_id === 3) {
            return view('profile.crew.edit');          
        }
    }

    public function updateName(Requests\UpdateNameRequest $request) {
        return dd($request->all());
    }
}

and here's the html form

{!! Form::open(array('url' => 'update_name')) !!}
<div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
    <label class="control-label">First Name</label>
    <input type="text" class="form-control" name="first_name" value="{{ old('first_name') }}" placeholder="{{ Auth::user()->first_name }}">

    @if ($errors->has('first_name'))
        <span class="help-block">
            <strong>{{ $errors->first('first_name') }}</strong>
        </span>
    @endif
</div>
<div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}">
    <label class="control-label">Last Name</label>
    <input type="text" class="form-control" name="last_name" value="{{ old('last_name') }}" placeholder="{{ Auth::user()->last_name }}">

    @if ($errors->has('last_name'))
        <span class="help-block">
            <strong>{{ $errors->first('last_name') }}</strong>
        </span>
    @endif
</div>
<button type="submit" class="btn btn-success">Update name</button>
{!! Form::close() !!}

for reference heres the form output

<form method="POST" action="http://localhost:8000/update_name" accept-charset="UTF-8">
    <input name="_token" type="hidden" value="VViupfPaPCQCk5aeUdc27Pt2Z8J7Hx1Y2khC0IY9">
    <div class="form-group">
        <label class="control-label">First Name</label>
        <input type="text" class="form-control" name="first_name" value="" placeholder="Hans">

    </div>
    <div class="form-group">
        <label class="control-label">Last Name</label>
        <input type="text" class="form-control" name="last_name" value="" placeholder="Padberg">

    </div>
    <button type="submit" class="btn btn-success">Update name</button>
</form>

here's my output in php artisan route:list

+--------+----------+-------------------------+------+-----------------------------------------------------------------+--------------+
| Domain | Method   | URI                     | Name | Action                                                          | Middleware   |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+--------------+
|        | GET|HEAD | /                       |      | Closure                                                         | web          |
|        | GET|HEAD | home                    |      | App\Http\Controllers\HomeController@index                       | web,web,auth |
|        | GET|HEAD | login                   |      | App\Http\Controllers\Auth\AuthController@showLoginForm          | web,guest    |
|        | POST     | login                   |      | App\Http\Controllers\Auth\AuthController@login                  | web,guest    |
|        | GET|HEAD | logout                  |      | App\Http\Controllers\Auth\AuthController@logout                 | web          |
|        | POST     | password/email          |      | App\Http\Controllers\Auth\PasswordController@sendResetLinkEmail | web,guest    |
|        | POST     | password/reset          |      | App\Http\Controllers\Auth\PasswordController@reset              | web,guest    |
|        | GET|HEAD | password/reset/{token?} |      | App\Http\Controllers\Auth\PasswordController@showResetForm      | web,guest    |
|        | GET|HEAD | profile                 |      | App\Http\Controllers\UserController@getProfile                  | web,web,auth |
|        | GET|HEAD | profile/edit            |      | App\Http\Controllers\UserController@editProfile                 | web,web,auth |
|        | GET|HEAD | register                |      | App\Http\Controllers\Auth\AuthController@showRegistrationForm   | web,guest    |
|        | POST     | register                |      | App\Http\Controllers\Auth\AuthController@register               | web,guest    |
|        | POST     | update_email            |      | App\Http\Controllers\UserController@updateEmail                 | web,web,auth |
|        | POST     | update_name             |      | App\Http\Controllers\UserController@updateName                  | web,web,auth |
|        | POST     | update_password         |      | App\Http\Controllers\UserController@updatePassword              | web,web,auth |
|        | POST     | update_profile_picture  |      | App\Http\Controllers\UserController@updateProfilePicture        | web,web,auth |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+--------------+
like image 657
emurmotol Avatar asked May 11 '16 13:05

emurmotol


1 Answers

I just want to add Something for those who check the validation with postman or any other applications in this category, make sure you add

Accept: application/json

to Header tab or you will not get application error and the page is just refreshed and return 200

like image 118
Mhmd Avatar answered Sep 23 '22 10:09

Mhmd