Laravel 5.5 - Validation Method does not exist?

Asked

Viewed 608 times

1

I’m having problems performing a data validation in the version 5.5, can you tell me where I’m going wrong?

Error:

inserir a descrição da imagem aqui

Here is the code of Controller

<?php

namespace App\Http\Controllers\PainelAdmin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\tbl_categoria;
use Validator;
use Illuminate\Validation;


    class CategoriaController extends Controller
    {

        private $tblcategoria;

        public function __construct(tbl_categoria $categoria)
        {

            $this->categoria = $categoria;

        }

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

        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function store(Request $request)
        {   
            // Obtendo todos os campos do Formulário 
            $dataForm = $request->all(); 

            //Validação de Dados            
            $this->validate($request, $this->categoria->rules);

            // realizando a inserção
            $insert =  $this->categoria->create($dataForm);

            // se inserir vai redirecionar para a pagina de consulta
            if ( $insert ){
                return redirect()->route('categoriaIndex');
            } else { 
                return redirect()->back();
            }
        }

Here’s the Model code

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class tbl_categoria extends Model
{
    // campos que podem ser inseridos pelo usuário
    protected $fillable = ['titulo', 'descricao'];

    // protegendo os campos e  a tabela de inserções 
    protected $guarded = ['id_categoria', 'created_at', 'update_at'];
    protected $table = 'tbl_categoria';

    // validando os campos
    public $rules = [
        'titulo'    => 'required|min:3|100',
        'descricao' => 'required|min:3|max:1000',
    ];

}

1 answer

1


In class $request of 5.5 which has the method it needs which is validate, then,

Change your code:

//Validação de Dados            
$this->validate($request, $this->categoria->rules); // Errado

for that code:

//Validação de Dados            
$request->validate($this->categoria->rules); // Correto

Final code of the method

public function store(Request $request)
{   
    // Obtendo todos os campos do Formulário 
    $dataForm = $request->all(); 

    //Validação de Dados            
    $request->validate($this->categoria->rules);

    // realizando a inserção
    $insert =  $this->categoria->create($dataForm);

    // se inserir vai redirecionar para a pagina de consulta
    if ( $insert ){
        return redirect()->route('categoriaIndex');
    } else { 
        return redirect()->back();
    }
}

Beyond that error coding also on array validation that you have placed in the class tbl_categoria has a value without saying which validation is to be made, the number 100 validates what, observe:

// validando os campos
public $rules = [
    'titulo'    => 'required|min:3|100',
    'descricao' => 'required|min:3|max:1000',
];

that is, forgot to put the validation before the number 100, which is the validation you need?

Observing: in the documentation Writing The Validation Logic explains well how this form would be, and it is worth saying that there are the previous validation methods with a good explanation.

References:

  • 1

    worst mistake was the same one. Solved... Missing a Max in public Ules getting that way 'title => 'required|min:3|max:100',

Browser other questions tagged

You are not signed in. Login or sign up in order to post.