Laravel 5.2 form validation request does not work properly

When I click the submit button, nothing happens, just refreshing the page.

Here is my code:

application /Http/routes.php

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

application / 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',
        ];
    }
}

application / 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 is 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: 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 is 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 |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+--------------+
+4
source share
6 answers

Your error variable returns a null value. That is why no errors show just the seeming page is being updated. Validation works, but the error value is not displayed.

: \Http\Kernel.php \Illuminate\Session\Middleware\StartSession::class web $middlewareGroups $middleware

+3

Form Builder? Laravel HTML . : https://laravelcollective.com/docs/5.2/html

0

, , , :

method => post

{!! Form::open(array('url' => 'update_name', 'method' => 'post')) !!}

, , . Run:

`php artisan route:list`

, Postman POST. , , , POST.

, , . Request, .

public function updateName(Illuminate\Http\Request $request) {
    $this->validate($request, [
        'first_name' => 'required',
        'last_name' => 'required'
    ]);
    return dd($request->all());
}
0
source

You get a TokenMismatchException .. make sure your session storage path is writable

0
source

I solved the problem, I changed this:

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

at

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

Thanks guys:)

0
source

You may also receive this error if the number of fields you are checking is greater than in the submitted form.

Make sure that you do not check any more fields that are in the submitted form.

-2
source

All Articles