1

Why is my validator is not validating the input-email field, it accepts anything!

This is my controller to update the database. The name in form is correct Edit: This is my entire code, I post here my code with just email but one guy said that is working, so why this is not working with my entire code of update database?

public function atualizar(Request $req)
{
    $erros = [
        'input-nome.max' => 'Introduziu carateres acima do máximo no campo do nome!',
        'input-facebook.max' => 'Introduziu carateres acima do máximo no campo do facebook!',
        'input-instagram.max' => 'Introduziu carateres acima do máximo no campo do instagram!',
        'input-descricao.max' => 'Introduziu carateres acima do máximo no campo da descrição!',
        'input-profissao.max' => 'Introduziu carateres acima do máximo no campo da profissão!',
        'input-imagem.max' => 'O tamanho máximo do ficheiro é de 10mb!',
        'image' => 'O ficheiro que inseriu não é imagem!',
        'mimes' => 'A extensão da imagem não é permitida, apenas JPEG, JPG e PNG!',
        'email' => 'Introduza um e-mail válido!'
    ];
    $validator = Validator::make($req->all(), [
        'input-nome' => 'max:25',
        'input-email' => 'nullable|required_with:input-email|email:rfc,dns,filter',
        'input-facebook' => 'max:40',
        'input-instagram' => 'max:40',
        'input-profissao' => 'max:25',
        'input-descricao' => 'max:120',
        'input-imagem' => 'image|mimes:jpeg,png,jpg|max:10240'
    ], $erros);

    $id = $req->input('input-id');

    $membrosEquipa = DB::table('equipa')
        ->where('id', $id)
        ->get();

    $inputEmail = $req->input('input-email');
    if (!isset($inputEmail)) {
        $inputEmail = null;
    }
    if ($req->filled('input-email')) {
        $inputEmail = $req->input('input-email');
    }
    $inputNome = $req->input('input-nome');
    $inputFacebook = $req->input('input-facebook');
    $inputInstagram = $req->input('input-instagram');
    $inputProfissao = $req->input('input-profissao');
    $inputDescricao = $req->input('input-descricao');
    $inputImagem = $req->file('input-imagem');

    foreach ($membrosEquipa as $membro) {
        if (!isset($inputNome)) {
            $inputNome = $membro->nome;
        }
        if (!isset($inputFacebook)) {
            $inputFacebook = $membro->facebook;
        }
        if (!isset($inputInstagram)) {
            $inputInstagram = $membro->instagram;
        }
        if (!isset($inputProfissao)) {
            $inputProfissao = $membro->profissao;
        }
        if (!isset($inputDescricao)) {
            $inputDescricao = $membro->descricao;
        }
        if (isset($inputImagem)) {
            Storage::delete($membro->imagem);
            $inputImagem = $req->file('input-imagem')->store('storage/equipa');
        } else {
            $inputImagem = $membro->imagem;
        }
    }

    $timestamp = date('Y-m-d H:i:s');

    DB::table('equipa')
        ->where('id', $id)
        ->update([
            'nome' => $inputNome,
            'email' => $inputEmail,
            'facebook' => $inputFacebook,
            'instagram' => $inputInstagram,
            'profissao' => $inputProfissao,
            'descricao' => $inputDescricao,
            'imagem' => $inputImagem,
            'updated_at' => $timestamp
        ]);

    return redirect('/editar/equipa/')->with('status', 'Membro da equipa atualizado com sucesso');
}

1 Answer 1

2

you must use below code after define $validator

$validator = Validator::make($req->all(), [
    'input-email' => 'email:rfc,dns|required'
], $erros);


if ($validator->fails())
{
   return redirect()->back()->withErrors($validator)->withInput();
}
8
  • email still saving on database without validating Jan 19, 2020 at 0:46
  • but it does, that is required_with:input-email, if I fill in the email field it would have to be validated as it was when it was inserted in the database. Jan 19, 2020 at 0:50
  • even if I remove the nullable, the mail goes through validation with anything I enter even if I don't insert anything. Jan 19, 2020 at 0:51
  • why don't use clean validating like 'input-email' => 'email|required' ?
    – sajjad
    Jan 19, 2020 at 0:53
  • because I want it to be able to send the email null but if that happens I would send it with the value that is in the database, the problem is that if I put for example Test in the email field it will go to the database without validating Jan 19, 2020 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.