Search code examples
trimlaravel-9

Laravel 9 Prevent trims on Old value


I have an old value containing the person's name, but when I put it in the input value it automatically trims (only the first word), I've tried adding exceptions in the AppServiceProvider and Trim middleware but it's not working, does anyone have a solution?

Top : The Old Value, Bottom : The Input Value

Top : The Old Value, Bottom : The Input Value

EDIT 1 (ADD CODE)

// File : input component (form-field.blade.php)
@props([
  'label' => 'undefined',
  'placeholder' => '',
  'type' => 'text',
  'disabled' => false,
  'required' => true,
  'value' => old($name) ?? '',
  'min' => '',
  'max' => '',
  'name' => '',
  'items' => [],
])

<label class="text-sm font-semibold" for={{ $name }}>{{ $label }} {!! $required ? '<span class="text-primary">*</span>' : '' !!}</label>
<div class="bg-gray-300 rounded-md @error($name) border-primary border-1 @enderror">
   <input 
      class="flex-grow bg-transparent border-gray-300 border-2 rounded-md w-full py-2 px-2 focus:border-black focus:outline-none"
      name={{ $name }}
      type={{ $type }}
      id={{ $name }}
      min={{ $min }}
      max={{ $max }}
      value={{ $value ?? old($name) }}
      {{ $disabled ? "disabled" : "" }}
   >
</div>


// File : View Form Page

<x-form-field 
  :name="'name'" 
  :label="'Nama'" 
  :value="session()->get('data')->employee_name ?? null"
/>

Solution

  • If you view the source, you'll see <input ... value=Prof Smiling ...>.

    value={{ $value ?? old($name) }} without quotation marks is your problem, because Smiling gets treated as a different HTML attribute, as if you wrote <input value="Prof" Smiling="">.

    value="{{ $value ?? old($name) }}" will avoid breaking when there are spaces.

    (You should fix this everywhere, not just for this particular occurrence.)