Search code examples
phplaravellaravel-livewire

Input not updating on changing variable value


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


Solution

  • try this complete example:

    Component Class

    public function updating($property, $value) // invoked this every time you update the $property model
    {
        if($property === "name") {
            $this->customerCode = generateUniqueUserCode($this->name);
            // change the 'customerCode' just before updating the 'name' model.
        }
    }
    

    Component Blade

    <!--- notice the absence of 'wire:model' attribut on the first input.  --->
    <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"
    /> 
    

    since the first input value is not a wire-model, you're sure that only one javascript code is controlling it. hope you find this useful, I try it on my local and it works!