Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

AlpineJS pass prefilled values to x-model

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  <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

>Solution :

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 ?? ''}}'">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading