<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## laravel 自定義驗證消息 ### 自定義錯誤消息 你可以通過重寫表單請求的`messages`方法來自定義錯誤消息。此方法應返回屬性 / 規則對及其對應錯誤消息的數組:. ~~~php /** * 獲取已定義驗證規則的錯誤消息 * * @return array */ public function messages() { return [ 'title.required' => 'A title is required', 'body.required' => 'A message is required', ]; } ~~~ ### 自定義驗證屬性 如果你希望將驗證消息的`:attribute`部分替換為自定義屬性名稱,則可以重寫`attributes`方法來指定自定義名稱。此方法應返回屬性 / 名稱對的數組: ~~~php /** * 獲取驗證錯誤的自定義屬性 * * @return array */ public function attributes() { return [ 'email' => 'email address', ]; } ~~~ ### 準備驗證輸入 如果您需要在應用驗證規則前清除請求中的任何數據,您可以使用`prepareForValidation`方法: ~~~php use Illuminate\Support\Str; /** * 準備驗證數據 * * @return void */ protected function prepareForValidation() { $this->merge([ 'slug' => Str::slug($this->slug), ]); } ~~~ ## 處理錯誤信息 通過`Validator`實例調用`errors`方法,它會返回一個`Illuminate\Support\MessageBag`實例,該實例包含了各種可以很方便地處理錯誤信息的方法。并自動給所有視圖提供`$errors`變量,也是`MessageBag`類的一個實例。 #### 檢索特定字段的第一個錯誤信息 您可以使用`first`方法檢索給定字段的第一個錯誤信息: ~~~php $errors = $validator->errors(); echo $errors->first('email'); ~~~ #### 檢索特定字段的所有錯誤信息 您可以使用`get`方法檢索給定字段所有信息的數組: ~~~php foreach ($errors->get('email') as $message) { // } ~~~ 如果要驗證表單的數組字段,您可以使用`*`字符來獲取每一個數組元素的所有錯誤信息: ~~~php foreach ($errors->get('attachments.*') as $message) { // } ~~~ #### 檢索所有字段的所有錯誤信息 您可以使用`all`方法獲取所有字段的所有錯誤信息的數組: ~~~php foreach ($errors->all() as $message) { // } ~~~ #### 判斷特定字段是否含有錯誤信息 `has`方法可用于判斷給定字段是否包含任何錯誤信息: ~~~php if ($errors->has('email')) { // } ~~~ ### 自定義錯誤信息 如果有需要,您可以使用自定義的錯誤信息來替換默認的錯誤信息。有幾種方法可以指定自定義錯誤信息。首先,您可以將自定義信息作為`Validator::make`方法的第三個參數: ~~~php $messages = [ 'required' => 'The :attribute field is required.', ]; $validator = Validator::make($input, $rules, $messages); ~~~ 在該例中,`:attribute`占位符將被驗證字段的實際名稱替換。您亦可在驗證消息中使用其他占位符。例如: ~~~php $messages = [ 'same' => 'The :attribute and :other must match.', 'size' => 'The :attribute must be exactly :size.', 'between' => 'The :attribute value :input is not between :min - :max.', 'in' => 'The :attribute must be one of the following types: :values', ]; ~~~ #### 為給定的屬性自定義信息 有時,您可能只想為特定字段指定自定義錯誤信息。您可以屬性名稱后使用「點」標記來實現。例如: ~~~php $messages = [ 'email.required' => 'We need to know your e-mail address!', ]; ~~~ #### 在語言文件中自定義信息 在大多數情況下,您可能選擇在語言文件中指定自定義信息,而不是將其傳遞給`Validator`。您可以在`resources/lang/xx/validation.php`語言文件中的`attributes`數組指定自定義信息來實現: ~~~php 'custom' => [ 'email' => [ 'required' => 'We need to know your e-mail address!', ], ], ~~~ #### 指定自定義屬性值 如果您想要將錯誤信息中的`:attribute`部分替換為自定義的屬性名稱,您可以在`resources/lang/xx/validation.php`語言文件中的`attributes`屬性數組中指定自定義名稱以實現: ~~~php 'attributes' => [ 'email' => 'email address', ], ~~~ 您亦可通過將自定義屬性作為`Validator::make`的第四個參數傳遞給它來實現: ~~~php $customAttributes = [ 'email' => 'email address', ]; $validator = Validator::make($input, $rules, $messages, $customAttributes); ~~~ #### 在語言文件中指定自定義值 有時,您可能需要將錯誤信息中的`:value`部分替換為自定義的表示形式。例如,下方規則中將`cc`指定為`payment_type`的值: ~~~php $request->validate([ 'credit_card_number' => 'required_if:payment_type,cc' ]); ~~~ 如果規則校驗失敗,它將生成如下的錯誤信息: ~~~php The credit card number field is required when payment type is cc. ~~~ 要將 payment type 顯示的`cc`替換為自定義的顯示形式,您可以通過在`validation`語言文件中定義`values`數組來實現之: ~~~php 'values' => [ 'payment_type' => [ 'cc' => 'credit card' ], ], ~~~ 現在,如果規則校驗失敗,將生成如下的錯誤信息: ~~~php The credit card number field is required when payment type is credit card. ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看