【问题标题】:Auto populate form inputs with Laravel and Live Wire使用 Laravel 和 Live Wire 自动填充表单输入
【发布时间】:2021-12-03 07:25:54
【问题描述】:

目标:关于焦点事件我想自动填充 html 输入字段#City #State(全名)#State2(缩写)
问题
1) 如何将输入传递给函数?
2) 我需要做什么来填充字段?

在资源中\ 视图:locale.blade.php

<div>
<input type="text" name="City" id="City" value="{{ $city }}">
<input type="text" name="State" id="State" value="{{ $state }}">
<input type="text" name="State2" id="State2" value="{{ $state2 }}">
<input type="text" name="Zip" id="Zip" value="{{ $zip }}"
       wire:focusout="setlocale">
</div>

在 App\Http\ LiveWire Locale.php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\State;
use Illuminate\Support\Arr;

class Locale extends Component
{
    public $zip = "";
    public $city = "";
    public $state = "";
    public $state2 = "";
    
    public function setlocale()

    {   
        $locale_arr=[];
        $g = $this->zip;
        dd($g);       ################## its only returning "" in my dd(); ###################
        $z = State::get()->where('zip',$g);
        $city = $z->city;
        $state = $z->state_name;
        $state2 = $z->state2_id;
        //TODO somehow render these variables in input fields.
        
    }
    public function render()
    {
        return view('livewire.locale');
    }
}

在 App\Models\ ** 状态**

class State extends Model
{
    use HasFactory;

}

修补匠

>>> use App\Models\State
>>> $a = State::get()->where('zip','10001');
=> Illuminate\Database\Eloquent\Collection {#70820
     all: [
       2569 => App\Models\State {#35131
         id: 2570,
         zip: "10001",
         lat: 40.7506,
         lng: -73.9972,
         city: "New York",
         state_id: "NY",
         state_name: "New York",
         county_name: null,
         timezone: null,
       },
     ],
   }

【问题讨论】:

标签: php laravel laravel-livewire


【解决方案1】:

使用 Livewire,您必须使用 wire:model 将输入绑定到您的类的属性。然后,Livewire 将处理将您的输入值设置为组件中的值。

<div>
   <input type="text" name="City" id="City" wire:model="city" />
   <input type="text" name="State" id="State" wire:model="state" />
   <input type="text" name="State2" id="State2" wire:model="state2" />
   <input type="text" name="Zip" id="Zip" wire:model="zip" wire:focusout="setlocale" />
</div>

然后,我建议您在 PHP 类中使用生命周期挂钩,而不是使用 wire:focusout。因此,从您的 HTML 中删除 wire:focusout="setlocale",并将以下内容添加到您的 PHP 类中,

public function updated($field, $value) 
{
    if ($field === 'zip') {
        $this->setlocale();
    }

}

public function setlocale()
{   
    $zip = State::where('zip', $this->zip)->first();
    $this->city = $zip->city;
    $this->state = $zip->state_name;
    $this->state2 = $zip->state2_id;
}

【讨论】:

  • @Quirel,wire:model.lazy 没有做到这一点?只是一个问题;-)
  • @Prospero .lazy 修饰符在用户点击离开输入时触发请求,类似于focosout,但关键是OP 完全缺少wire:model,并且它的最好使用生命周期挂钩来运行 setlocale() 方法,而不是使用 wire:focusout.lazy 修饰符可以在这里应用以使请求在点击外部时触发,没有问题。
  • @Qirel 我已经按照你说的那样工作了。我移除了焦点并使用了wire:model.lazy="zip",但后来我不得不使用$this-&gt;render(); 添加对我的渲染函数的调用,但它现在可以工作了,非常感谢!!!!!!
猜你喜欢
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-15
  • 1970-01-01
  • 2021-04-20
  • 2021-12-03
  • 2020-10-18
相关资源
最近更新 更多