Laravel Validation Controls

Laravel Validation Controls
Laravel provides several different approaches to validate your application's incoming data. It is most common to use the validate method available on all incoming HTTP requests. However, we will discuss other approaches to validation as well. Laravel includes a wide variety of convenient validation rules that you may apply to data, even providing the ability to validate if values are unique in a given database table. We'll cover each of these validation rules in detail so that you are familiar with all of Laravel's validation features.

The Controller Code is

 $shouldRequireName = setting_by_store('name_check') == 1; 
        $shouldRequirePhone = setting_by_store('phone_check') == 1; 
        $shouldRequireAddress = setting_by_store('address_check') == 1; 
        $shouldRequireNote = setting_by_store('note_check') == 1; 

        $rules = [
            'name' => $shouldRequireName ? 'required' : 'nullable',
            'phone' => $shouldRequirePhone ? 'required' : 'nullable',
            'billing_address' => $shouldRequireAddress ? 'required' : 'nullable',
            'note' => $shouldRequireNote ? 'required' : 'nullable',
        ];

        // Retrieve dynamic content for messages
        $user = auth()->user();
        $store = Store::where('id', auth()->user()->current_store)->first();
        $storeId = $store->id;
        $nameLabel = setting_by_store_front('checkout_field_name', $storeId) ?? 'আপনার নাম লিখুন';
        $phoneLabel = setting_by_store_front('checkout_field_mobile', $storeId) ?? 'আপনার মোবাইল নাম্বারটি লিখুন';
        $addressLabel = setting_by_store_front('checkout_field_address', $storeId) ?? 'সম্পূর্ন ঠিকানা';
        $noteLabel = setting_by_store_front('checkout_field_note', $storeId) ?? 'বিঃদ্রঃ';
        $messages = [
            'name.required' => "{$nameLabel} field is required.",
            'phone.required' => "{$phoneLabel} field is required.",
            'billing_address.required' => "{$addressLabel} field is required.",
            'note.required' => "{$noteLabel} field is required.",
            // Add more custom messages as needed
        ];

        // Validate the request
        $validator = Validator::make($request->all(), $rules, $messages);

        if ($validator->fails()) {
            return redirect()->back()
                            ->withErrors($validator)
                            ->withInput();
        }

sample blade component

  <div class="mb-3">
                                    <div>
                                      <label class="mb-2 block flex-[250px] mr-3 font-normal text-sm" for="address">{{ setting_by_store_front('checkout_field_note', $store->id) ?? 'বিঃদ্রঃ' }}
                                        @if (setting_by_store_front('note_check', $store->id) == 1)
                                        <span class="text-red-600">*</span></label>
                                        @endif
                                      <textarea type="text" name="note"  class="w-full p-1 border border-solid input input-bordered border-slate-300 focus:outline-red-400 custom-checkout-bg" placeholder="{{ setting_by_store_front('checkout_placeholder_note', $store->id) ?? 'পণ্য বা ঠিকানা নোট লিখুন' }}"  ></textarea>
                                      @error('note')
                                        <div class="alert alert-danger" style="color: red">{{ $message }}</div>
                                    @enderror
                                    </div>
                                  </div>

Write a Associte Comment
Markdown Editor