0

i setup laravel 5.8 form request with custom message and its validate fine and redirect back to form on invalid input But dost error back or i don't look where for errors

this is my class

public function create(createUser $request){
    //on invalid input not enter in this class
        //validation
    //dd($request->all('name'));


        $validator = $request->validated();
            if ($validator->fails()) {
                 Session::flash('error', $validator->messages()->first());
                return redirect()->back()->withInput();
            }

request class

public function rules()
    {

        return [
            'name'      => 'required|max:255'
            ,'email'    => 'string|required|E-mail|unique:users,email'
            ,'birthY'   => 'required|min:4'
            ,'birthM'   => 'required'
            ,'birthD'   => 'required'
            ,'gender'   => 'required'
            ,'Phone'    => 'required|min:11|max:11'
            ,'password' => 'required|min:8'
        ];

    }

    public function messages(){

        return [
            'name.required' => "اسمت چیه ؟!!",
            'email.required' => 'ایمیلت چیه?',
            'gender.required' => 'سرکار خانم یا جناب آقای ؟',
            'Phone.required'    =>'شماره ما توی درباره ما هست  شما بدین شاید باهتون کار داشتیم',
            'birthY.required'  => 'تولدت کیه ؟',
            'birthY.min'  => 'سال ها چهار رقمیه گلم',
            'password.required' =>'رمز بزار تا امن باشی ' ,
            'password.min' =>'حداقل هشتا بزن که کسی شک نکنه ',
            'password.confirmed' =>'هنوز هیچی نشده یادت رفت پسوردت رو  تایید کن تا بفهمم یاد داری',
            'Phone.min' =>  'شمارتو با صفر بزن لطفا'         
        ];
    }

and this is my blade where i try to show errors Not Working!!!!


                @if ($errors->any())
                        <div class="alert alert-danger">
                            <ul>
                            @foreach ($errors->all() as $error)
                                    <li>{{ $error }} </li>
                            @endforeach
                            </ul>
                        </div>
                @endif


            @isset($messages)
            <?php dd($messages) ?>

            @endisset



        @if (count($errors) > 0)
      <div class="alert alert-danger">
        <?php dd($error) ?>

          <ul>
              @foreach ($errors->all() as $error)

              <li>{{ $error }}</li>

              @endforeach



if you need other piece of code just let me know tnx a lot :)

6
  • 3
    when you use Laravel Form Request Validation (laravel.com/docs/master/validation#form-request-validation), Laravel will handle everything for you. You don't have to manually check validation and manually redirect to the previous page. Read the documentation again. Also, use Laravel Telescope for better debugging to make sure you hit the correct end point. Commented Sep 16, 2019 at 17:28
  • i know that but i need to know how show my errors messages that is my main problem Commented Sep 16, 2019 at 17:46
  • you said the validation does not trigger. Install Laravel Telescope to debug. Start from scratch with Laravel Form Request Validation Documentation. When you get dd() validation message working, then move on next to display them on your form. Commented Sep 16, 2019 at 18:36
  • what the result of dd( $validator) Commented Sep 16, 2019 at 19:53
  • its not get there to get result Commented Sep 17, 2019 at 5:59

1 Answer 1

27

If this happens to you with postman remember to check that the header accepts json.

Accept: application/json Content-Type: application/json

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

3 Comments

Great you saved my day!
Nice, no problem guy!
i actually new another project and paste entire all my classes and migrations and its worked but its sounds like its work for a that dude above so thank you for collaboration

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.