Home > other >  AlpineJS pass prefilled values to x-model
AlpineJS pass prefilled values to x-model

Time:05-21

I have a form which can receive dynamically value and I would like to pass this initially to x-model.

How can I achieve this? What are the best to goes in this case?

Hier is an example

  <input name="zip" type="text" autocomplete="off" placeholder="{{__('PLZ, Ort', 'bdb')}}" value="{{$currentZip ?? ''}}"
               x-model="formData.zip">

formData.zip will be empty even if the value is filled $currentZip

CodePudding user response:

You can have an x-init directive on each input element, where you can set the optional dynamic value:

<input name="zip" 
       type="text" 
       autocomplete="off" 
       placeholder="{{__('PLZ, Ort', 'bdb')}}" 
       x-model="formData.zip"
       x-init="formData.zip = '{{$currentZip ?? ''}}'">
  • Related