簡體   English   中英

使用組件作為驗證錯誤消息

[英]use component as a validation error message

Laravel 8

我正在嘗試用組件替換重復的驗證錯誤消息

注冊.blade.php:

<div class="mb-3">
   <label for="email" class="form-label">Email address</label>
     <input name="email" type="email" class="form-control" value="{{ old('email') }}">
    <x-alert :input="email" />
  </div>

警報.php:

<?php

namespace App\View\Components;

use Illuminate\View\Component;

class Alert extends Component
{
  public $input;

  public function __construct($input)
  {
    $this->input = $input;
  }


  public function render()
  {
    return view('components.alert');
  }
}

alert.blade.php:

@error($input)
<span class="invalid-feedback" role="alert">{{ $message }}</span>
@enderror

但它給了我一個錯誤

使用未定義的常量電子郵件 - 假定為“電子郵件”(這將在未來版本的 PHP 中引發錯誤)(視圖:C:\\laragon\\www\\practice\\resources\\views\\auth\\register.blade.php)

只需按照以下方式進行

<div class="mb-3">
   <label for="email" class="form-label">Email address</label>
   <input name="email" type="email" class="form-control" value="{{ old('email') }}">    
   <x-alert input="email" />
</div>

:符號使它解析為 PHP 代碼而不是文字字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM