mercredi 13 mars 2019

How to display validation errors?

Router

Route::get('/contact',['uses'=>'Admin\ContactController@show','as'=>'contact']);
Route::post('/contact',['uses'=>'Admin\ContactController@store']);

Controller

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Requests\ContactRequest;

use App\Http\Controllers\Controller;

use Validator;


class ContactController extends Controller
{
   
    public function store(Request $request,$id=FALSE) {
        
        if($request->isMethod('post')) {
                        
                        $messages = [
                                'name.required' => 'ПОЛЕ :attribute обязательно к заполнению!!!!',
                    'email.max' => 'Максимально допустимое количество символов - :max',
                        ];
                        
                        $validator = Validator::make($request->all(),[
                                'name'=>'required',
                                /*'email'=>'required'*/
                        ],$messages);
                        
                        $validator->sometimes(['email','site'],'required',function($input) {
                                
                                /*dump($input);
                                exit();*/
                                
                                return strlen($input->name) >= 10;
                        });
                        
                        $validator->after(function($validator) {
                                
                                $validator->errors()->add('name','ДОполнительное сообщение');
                                
                        });
                        
                        if($validator->fails()) {
                                
                                $messages = $validator->errors();
                                
                                //dump ($messages->first());
                                dump($validator->failed());
                                exit();

                                return redirect()->route('contact')->withErrors($validator)->withInput();
                        }
                        
                }
                
                return view('default.contact',['title'=>'Contacts']);
        }
        
        public function show() {

                return view('default.contact',['title'=>'Contacts']);
        }
}

Template

extends('default.layouts.layout')

@section('content')

<div class="col-md-9">

        <div class="">
                <h2>Contact us!</h2>
    </div>

        <p>
        This is a template for a simple marketing or informational website. It includes a large callout called a jumbotron and three supporting pieces of content. Use it as a starting point to create something more unique.
        </p>
    
        @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="name">Name</label>
            <input type="text" class="form-control" id="name" name="name" value="" placeholder="Jane Doe">
          </div>
          <div class="form-group">
            <label for="email">Email address</label>
            <input type="email" class="form-control" id="email" value="" name="email" placeholder="Email">
          </div>
          <div class="form-group">
            <label for="site">Site</label>
            <input type="text" class="form-control" id="site" value="" name="site" placeholder="Site">
          </div>
          <div class="form-group">
            <label for="text">Text</label>
            <textarea class="form-control" id="text" name="text" rows="3"></textarea>
          </div>
          <button type="submit" class="btn btn-primary">Submit</button>
        </form>
</div>    
@endsection

The problem is that I have not displayed validation error and the message "further message". Although the validation is successful. The problem is. I don't want to display $messages I tried to walk on it with foreach, but laravel says that it found the $messages. Although the controller $messages displayed. The pattern also shows that the errors I have 0, but in the controller I have shows validation errors. What's the problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire