3

I use Laravel 5.4 on hosting. My validation method is working, but it doesn't show messages. I do all the needed operations for displaying errors but it doesn't work.

@if($errors->has('recipient'))
<div class="form-group">
<label class="col-lg-2 control-label"></label>  
<div class="col-lg-10">                     
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->get('recipient') as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
</div>
</div>
@endif

I use {{ csrf_field() }} tokens. Validation rules work if wrong user types are used, but messages are not displayed.

My send method:

public function send(Request $request)
{
   $this->validator($request->all())->validate();
   $this->create($request->all());
   return redirect($this->redirectToAfterSendMessage);
}

My validator code:

protected function validator(array $data)
{        
    return Validator::make($data, [

        "$this->recipient" => [
            'sometimes',
            'required',
            'email',
            'exists:users,email',
            Rule::notIn([auth()->user()->email])
        ],

        "$this->subject" => 'sometimes|required|min:10',

        "$this->message" => 'sometimes|required|min:50',

    ], $this->validator_messages());
}

My routes:

Route::get('/compose', 'InboxController@compose')     ->name('compose');
Route::post('/compose', 'InboxController@send');

Result of dump($errors):

ViewErrorBag {#244 ▼
  #bags: []
}
3
  • Please include the code that does the validation along with the controller method. Commented Jan 22, 2018 at 6:37
  • Please show result of {{ dd($errors) }} Commented Jan 22, 2018 at 6:38
  • Why you used "$this->recipient"? Use 'recipient' only! It should work for you! Commented Jan 22, 2018 at 6:49

2 Answers 2

3

EDITED

To fix your current problem, change $errors->get('recipient') to $errors->all().

It should look like this:

@foreach ($errors->all() as $error)
   <li>{{ $error }}</li>
@endforeach

--

This isn't a direct solution for your question. However an even better approach (imo) than yours are custom requests. You can quickly generate custom requests by using the following command:

php artisan make:request CustomRequest

After that you can change type from Request to CustomRequest

public function send(CustomRequest $request)
{
   $this->create($request->all());
   return redirect($this->redirectToAfterSendMessage);
}

And in CustomRequest.php your extract your validation logic like so:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CustomRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'email' => 'required',
            'message' => 'required',

        ];
    }

    public function messages()
    {
        return [
            'name.required' => 'Name is required.',
            'email.required' => 'Email is required.',
            'message.required' => 'Message is required.',
        ];
    }
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

in my local site with $this->recipient also working. On hosting also this code working but only not return messages
@MaxNet I updated my answer. If it solved your problem please mark my answer as correct. If it is helpful, please upvote it. Tell me if you still have any problems.
1

You need to change:

"$this->recipient"
"$this->subject" 
"$this->message"

To:

'recipient'
'subject'
'message'

1 Comment

I deleted but not working show messages and $errors return null. But validation working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.