Friday, May 11, 2018

Laravel | Errors validation returning null - development

Laravel | Errors validation returning null

Yesterday I started with Laravel, currently busy with my first project, a simple news page.

Unfortunately, I’ve met some problems while validating my post request, I’ve tried to search on Google for my issue… And I tried to use those fixes, but strange enough I had no result.

The problem is:
When I post data the normal ‘form page’ will be shown without any errors. I’m aware that I have double error outputs, it’s just for the test.

What do I want to reach?
I want that the validation error will be shown

Image of page

routes.php

<?php

Route::group(['middleware' => ['web']], function() {

Route::get('/', function() {
    return redirect()->route('home');
});

Route::get('/home', [
    'as' => 'home',
    'uses' => 'HomeController@home',
]);

Route::get('/add_article', [
    'as' => 'add_article',
    'uses' => 'HomeController@add_article',
]);

Route::post('/add_article', [
    'as' => 'add_article.newarticle',
    'uses' => 'HomeController@post_article',
]);

});

HomeController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppHttpRequests;

use AppNews;

class HomeController extends Controller
{

public function home(News $news) 
{
    $articles = $news->orderBy('id','desc')->take(3)->get();
    return view('home.home')->with('articles', $articles);
}

public function add_article()
{
    return view('home.add_article');
}

public function post_article(Request $request)
{

    $this->validate($request, [
        'title' => 'required|max:64',
        'content' => 'required|max:2048',
    ]);

    // $newNews = new News;
    // $newNews->title = $request->title;
    // $newNews->content = $request->content;
    // $newNews->save();
}

}

add_article.blade.php

@extends('templates.default')

@section('content')
    <div class="row">
        <div class="col-sm-12 col-md-6 col-lg-6 col-xl-6 offset-md-3 offset-lg-3 offset-xl-3">
            <p class="lead">New news article</p>

            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"> </h5>
                </div>
                <div class="card-body">
                    @if (count($errors) > 0)
                        <div class="alert alert-danger">
                            <ul>
                            @foreach ($errors->all() as $error)
                                <li></li>
                            @endforeach
                            </ul>
                        </div>
                    @endif
                    <form method="post" action="">

                        <div class="form-group">
                            <label for="title" style="margin-bottom:0px;">
                                Title: 
                            </label>

                            <input class="form-control" type="text" name="title" placeholder="Please enter your title!" id="title">
                            @if ($errors->has('title'))
                                
                            @endif
                        </div>

                        <div class="form-group">
                            <label for="content" style="margin-bottom:0px;">
                                Content: 
                            </label>

                            <textarea class="form-control" rows="3" name="content" placeholder="Please enter your message!" id="content" style="resize:none;"></textarea>
                            @if ($errors->has('content'))
                                
                            @endif
                        </div>

                        <div class="form-group text-right">
                            <button class="btn btn-primary" type="submit">
                                Create news
                            </button>
                        </div>

                        
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

I hope someone can help me to resolve this issue!
Thanks in advance.
– Pascal



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-errors-validation-returning-null/
via Lzo Media

No comments:

Post a Comment