remanbala's avatar

Validation error not getting displayed, though dd is working, how to fix?

Blade file

   <div>
              <x-input-label for="rising_above_title" :value="__('Rising Above Title')" />
               <x-text-input id="rising_above_title" name="rising_above_title" type="text" class="mt-2.5 block w-full"
                                     autofocus />
               <x-input-error class="mt-2" :messages="$errors->get('rising_above_title')" /> 
     </div>

Store function

 public function store(Request $request)
    {
        $card = new RisingAbove();
        $validator = Validator::make($request->all(), [
            'rising_above_title' => 'required|string|max:50',
            'rising_above_desc' => 'required|string|max:255',
            'rising_above_image' => 'required|image|mimes:jpg,png,webp|max:1024',
        ]);

        if ($validator->fails()) {
            //dd($validator->errors());
            return redirect()->route('ra.index')->withErrors($validator)->withInput();
        }

        if ($request->hasFile('rising_above_image')) {
            $takeImg = $request->file('rising_above_image');

            // Create a new image manager instance
            $manager = new ImageManager(new Driver());

            // Generate a unique name for the image file
            $card_image_name = hexdec(uniqid()) . '.' . $takeImg->getClientOriginalExtension();

            // Read and process the image
            $img = $manager->read($takeImg);
            $img = $img->resize(532, 266);  // Resize the image to the specified dimensions
            $img->toWebp(80)->save(base_path('public/uploads/rising-above/' . $card_image_name));

            // Save the URL of the uploaded image
            $save_url = 'uploads/rising-above/' . $card_image_name;
            $card->rising_above_image = $save_url;
        }

        $card->rising_above_title = $request->rising_above_title;
        $card->rising_above_desc = $request->rising_above_desc;
        $card->save();

        return redirect()->route('ra.index')->with('success', 'Card Created Successfully');
    }
0 likes
3 replies
TruptMan-Solutions's avatar

but why do you use Validator Facade, is there any specific reason?

use $request->validate(), this will take care of redirecting back to the input form , and will display any error message.

like this :

store method $request->validate example:

/**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:200'],
            'is_home' => ['required', 'string'],
            'address' => ['nullable', 'string'],
            'address_lat' => ['nullable', 'string'],
            'address_long' => ['nullable', 'string'],
            'address_obj' => ['nullable'],
            'contact' => ['nullable', 'string', 'max:20'],
            'support' => ['required', 'string'],
            'remark' => ['nullable', 'max:1000'],
        ]);

        Survey::create($validated);

        flash()->addSuccess('Successfully Saved');

        return to_route('surveys.index');
}

and Blade error code example:

<div class="form-floating mb-3">
         <x-forms.text-input name="name" id="name" placeholder="enter your name"/>
         <label for="floatingInput">Name</label>
         <x-forms.error :messages="$errors->get('name')"/>
 </div>

this may help you out!. let us know what you get.

remanbala's avatar

@TruptMan-Solutions There is no specific reason, it was working fine with another controller but I faced issue on this one and couldn't figure out the issue. I will try your solution and let you know if it works. Thank you so much!

remanbala's avatar

I found what the issue was. the route redirection was the issue.

1 like

Please or to participate in this conversation.