skip to Main Content

i am using updated hook to update another wire:model value, if i dd or print the variable in the view i can see updated value but not in input itself
Same logic was working file on livewire 2
codes

{{ $customerCode }}

<input wire:model.live="customerCode" type="text" id="customer_code" name="customer_code" class="form-control" placeholder="" />

<input type="text" wire:model.blur="name" class="form-control" placeholder="John Doe"/> 
{{-- on changing this it run a updatedName hook runs and i can seed printed on view on above $customerCode but on in input --}}

Component Code

public function updatedName(){
    $this->customerCode =  'random string';//generateUniqueUserCode($this->name);
}

screenshot

2

Answers


  1. I think I understood what you want to do, you may misunderstand the role of live model:

    1. It does not import the value stored in your server-side value in the component (even after being changed in server-side), it rather waits client to change its value, againt, without looking out its old value.
    2. Moreover, if a blur model is modified by user, and you set an updated/updating hook triggered when you update this blur-modal, and changing the other component properties, live model are still not not capable to be updated on the client-side.

    I suggest that you abondon puting customerCode inside live-modal, print it simply as a text, since it’s not up to the user to generate their code.

    <input
      id="customer_code"
      name="customer_code"
      class="form-control"
      placeholder=""
      type="text"
      value="{{ $customerCode }}"
    />
    
    Login or Signup to reply.
  2. I will try to give you a complete example:

    Component Class

    public function updating($property, $value)
    {
        if($property === "name") {
            $this->customerCode = generateUniqueUserCode($this->name);
        }
    }
    

    Component Blade

    <input  
      id="customer_code"
      name="customer_code"
      class="form-control"
      placeholder=""
      type="text"
      value="{{ $customerCode }}"
    />
    
    <input
      class="form-control"
      placeholder="John Doe"
      type="text"
      wire:model.blur="name"
    /> 
    

    please notice that I didn’t put ‘wire:model’ attribut on the first input.
    hope you find this useful, I try it on my local and it works!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search