Validator not found

1

To begin with, I have this migration:

Schema::create('animals', function (Blueprint $table){
        $table->increments('id');
        $table->string('nombre');
        $table->string('cientifico');
        $table->string('tipo');
        $table->boolean('peligro');
        $table->timestamps();
    });

And to insert data in the table I have the following form:

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Introduce los datos del animal</div>

                <div class="card-body">
                    <form method="POST" action="animal_crear">
                        @csrf

                        <div class="form-group row">
                            <label for="nombre" class="col-md-4 col-form-label text-md-right">Nombre comun</label>

                            <div class="col-md-6">
                                <input id="nombre" type="text" class="form-control{{ $errors->has('nombre') ? ' is-invalid' : '' }}" name="nombre" value="{{ old('nombre') }}" required autofocus>

                                @if ($errors->has('nombre'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('nombre') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="cientifico" class="col-md-4 col-form-label text-md-right">Nombre cientifico</label>

                            <div class="col-md-6">
                                <input id="cientifico" type="text" class="form-control{{ $errors->has('cientifico') ? ' is-invalid' : '' }}" name="cientifico" value="{{ old('cientifico') }}" required autofocus>

                                @if ($errors->has('cientifico'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('cientifico') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="tipo" class="col-md-4 col-form-label text-md-right">Tipo de animal</label>

                            <div class="col-md-6">
                                <input id="tipo" type="text" class="form-control{{ $errors->has('tipo') ? ' is-invalid' : '' }}" name="tipo" value="{{ old('tipo') }}" required autofocus>

                                @if ($errors->has('tipo'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('tipo') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="peligro" class="col-md-4 col-form-label text-md-right">¿Esta en peligro de extinción?</label>

                            <div class="col-md-6">
                                Si <input type="radio" name="peligro" value=1><br>
                                No <input type="radio" name="peligro" value=0 checked>

                                @if ($errors->has('peligro'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('peligro') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    Añadir animal
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Which is treated in this function:

use \App\Http\Requests\AnimalRequest;
public function sumar(AnimalRequest $request){
    Animal::create($request->all());
    return back()->with('message',['success','¡Los datos del '.$request->cientifico.' se han introducido con exito!']);
}

And I use the AnimalRequest validator:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class AnimalRequest extends FormRequest{
    public function authorize(){
        return true;
    }

    public function messages(){
        'nombre.required'=>'El nombre comun es obligatorio',
        'cientifico.required'=>'El nombre cientifico es obligatorio',
        'tipo.required'=>'El tipo de animal es obligatorio'
    }

    public function rules(){
        return [
            'nombre'=>'required',
            'cientifico'=>'required',
            'tipo'=>'required'
        ];
    }
}

But when I run the program, I find this:

What am I forgetting?

By the way, I have tried to use a normal Request and this one has worked, so the error has to be that I have not written the request correctly or that the Controller can not find AnimalRequest.

    
asked by Miguel Alparez 09.06.2018 в 14:09
source

2 answers

1

you are only validated if the fields are full, that can be valid in the front, but it is missing several data as if it is numeric or character,

Here I send you a quick example and check the messages in this library link

<?php 
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Validator;
use Response;

use App\MunicipiosModel;
use App\BarriosModel;
use View;
use App\Solicitude_tipo;
use App\ComunasModel;

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



    }
    /**
    * @var array
    */
    protected $rules =
    [

        //'id' => 'required|min:1|max:99999999',
        'nombre' => 'required|min:2|max:255|regex:/^([0-9a-zA-ZñÑáéíóúÁÉÍÓÚ_-])+((\s*)+([0-9a-zA-ZñÑáéíóúÁÉÍÓÚ_-]*)*)+$/',
        'cientifico' => 'required|min:1|max:99999999',
        'tipo' => 'required|min:1|max:99999999',

    ];
     /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($cientifico){

        //$id_tipo = Solicitude_tipo::select("solicitude_tipos.id","solicitude_tipos.descripcion as nombre")->get();
        $tipo = session()->get('tipo');
        $Barrios = BarriosModel::where("cientifico","=",$cientifico)->where("tipo","=",$tipo)->get();
        $cientifico = ComunasModel::select("id","nombre")->where('id','=',$cientifico)->get();
        $tipo = MunicipiosModel::select("id","nombre")->where('id','=',$tipo)->get();

        return view('Barrios.index', [ "cientifico" => $cientifico,"tipo" => $tipo, 'listmysql' => $Barrios] );

    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(){

    }
     /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make(Input::all(), $this->rules);
        if ($validator->fails()) {
            return Response::json(array('errors' => $validator->getMessageBag()->toArray()));
        } else {
            $Barrios = new BarriosModel();

             $Barrios->nombre=$request->nombre;
                 $Barrios->cientifico=$request->cientifico;
                 $Barrios->tipo=$request->tipo;
                 $Barrios->created_at=$request->created_at;
                 $Barrios->updated_at=$request->updated_at;

            $Barrios->save();
            return response()->json($Barrios);
        }
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $Barrios = BarriosModel::findOrFail($id);

        return view('Barrios.show', ['BarriosModel' => $Barrios]);
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make(Input::all(), $this->rules);
        if ($validator->fails()) {
            return Response::json(array('errors' => $validator->getMessageBag()->toArray()));
        } else {
            $Barrios = BarriosModel::findOrFail($id);

                $Barrios->nombre=$request->nombre;
                $Barrios->cientifico=$request->cientifico;
                $Barrios->tipo=$request->tipo;
                $Barrios->created_at=$request->created_at;
                $Barrios->updated_at=$request->updated_at;


            $Barrios->save();
            return response()->json($Barrios);
        }
    }
    
answered by 09.06.2018 в 16:01
0

I already discovered that I failed. It seems that it was by 2 factors: The first was that I lie with the keys organizing it badly. On the other hand, in "messages" forget the "return".

This is correct:

class AnimalRequest extends FormRequest{
    public function authorize(){
        return true;
    }

    public function messages(){
        return[
            'nombre.required'=>'El nombre comun es obligatorio',
            'cientifico.required'=>'El nombre cientifico es obligatorio',
            'tipo.required'=>'El tipo de animal es obligatorio'
        ];
    }

    public function rules(){
        return[
            'nombre'=>'required',
            'cientifico'=>'required',
            'tipo'=>'required'
        ];
    }
}
    
answered by 09.06.2018 в 15:45