• <i id='VvuCS'><tr id='VvuCS'><dt id='VvuCS'><q id='VvuCS'><span id='VvuCS'><b id='VvuCS'><form id='VvuCS'><ins id='VvuCS'></ins><ul id='VvuCS'></ul><sub id='VvuCS'></sub></form><legend id='VvuCS'></legend><bdo id='VvuCS'><pre id='VvuCS'><center id='VvuCS'></center></pre></bdo></b><th id='VvuCS'></th></span></q></dt></tr></i><div id='VvuCS'><tfoot id='VvuCS'></tfoot><dl id='VvuCS'><fieldset id='VvuCS'></fieldset></dl></div>

    <small id='VvuCS'></small><noframes id='VvuCS'>

  • <legend id='VvuCS'><style id='VvuCS'><dir id='VvuCS'><q id='VvuCS'></q></dir></style></legend>

      <tfoot id='VvuCS'></tfoot>
        <bdo id='VvuCS'></bdo><ul id='VvuCS'></ul>

        如何使 Laravel 的 Validator $rules 成为可选?

        时间:2023-09-22

        • <bdo id='znTh2'></bdo><ul id='znTh2'></ul>

              <tbody id='znTh2'></tbody>
            • <tfoot id='znTh2'></tfoot>
            • <small id='znTh2'></small><noframes id='znTh2'>

              <legend id='znTh2'><style id='znTh2'><dir id='znTh2'><q id='znTh2'></q></dir></style></legend>
            • <i id='znTh2'><tr id='znTh2'><dt id='znTh2'><q id='znTh2'><span id='znTh2'><b id='znTh2'><form id='znTh2'><ins id='znTh2'></ins><ul id='znTh2'></ul><sub id='znTh2'></sub></form><legend id='znTh2'></legend><bdo id='znTh2'><pre id='znTh2'><center id='znTh2'></center></pre></bdo></b><th id='znTh2'></th></span></q></dt></tr></i><div id='znTh2'><tfoot id='znTh2'></tfoot><dl id='znTh2'><fieldset id='znTh2'></fieldset></dl></div>

                  本文介绍了如何使 Laravel 的 Validator $rules 成为可选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  假设我有两种方法的 User 模型:

                  Let's say I have User model with two methods:

                  User.php

                  class User extends Eloquent
                  {  
                      /* Validation rules */
                      private static $rules = array(
                          'user'  => 'unique:users|required|alpha_num',
                          'email' => 'required|email'
                      );
                  
                      /* Validate against registration form */
                      public static function register($data)
                      {
                          $validator = Validator::make($data, static::$rules);
                          if($validator->fails())
                          {
                              /*... do someting */
                          }
                          else
                          {
                              /* .. do something else */
                          }
                      }
                  
                      /* Validate against update form */
                      public static function update($data)
                      {
                          $validator = Validator::make($data, static::$rules);
                          if($validator->fails())
                          {
                              /*... do someting */
                          }
                          else
                          {
                              /* .. do something else */
                          }
                      }
                  }
                  

                  我的问题:我怎样才能让验证规则成为可选的,所以即使 update() 的数据只是 email 字段,它会忽略 user 并仍然验证为 true.
                  这是可能的还是我遗漏了什么?

                  My question: How can I make validation rules optional, so even if data for update() would be just email field, it would ignore user and still validate to true.
                  Is this even possible or am I missing something?

                  抱歉我的英语不好.

                  推荐答案

                  不确定我的问题是否正确,但如果用户是可选的,则应从验证器中删除必需".这样你将有:

                  Not sure if I'm getting your question right but if the user is optional you should remove 'required' from the validator. This way you will have:

                  'user'  => 'unique:users|alpha_num',
                  

                  代替:

                  'user'  => 'unique:users|required|alpha_num',
                  

                  另一方面,我为我的模型创建了一个自定义方法,该方法能够根据传入参数返回自定义验证规则.

                  On the other hand I create a custom method for my models that is able to return custom validation rules depending on incoming parameters.

                  例如:

                  private function getValidationRules($rules)
                  {
                      if ($rules == UPDATE_EMAIL)
                      {
                          return array('email' => 'required|email');
                      } else {
                          return array(
                              'user'  => 'unique:users|required|alpha_num',
                              'email' => 'required|email'
                          );
                      }
                  }
                  

                  我想这只是个人选择,但我发现从方法中获取验证规则可以更好地控制我真正想要验证的内容,尤其是当您想要执行一些高级验证时.

                  I guess it's only a personal choice, but I have found that getting the validation rules from a method allows more control over what I really want to validate, especially when you want to perform some advanced validations.

                  希望对你有帮助.

                  这篇关于如何使 Laravel 的 Validator $rules 成为可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:L5.6 - 数据透视表的关系 下一篇:Laravel 多对多 - 意外的结果集 ->select()

                  相关文章

                  最新文章

                1. <legend id='mH8pc'><style id='mH8pc'><dir id='mH8pc'><q id='mH8pc'></q></dir></style></legend>
                    <bdo id='mH8pc'></bdo><ul id='mH8pc'></ul>

                      <small id='mH8pc'></small><noframes id='mH8pc'>

                      <i id='mH8pc'><tr id='mH8pc'><dt id='mH8pc'><q id='mH8pc'><span id='mH8pc'><b id='mH8pc'><form id='mH8pc'><ins id='mH8pc'></ins><ul id='mH8pc'></ul><sub id='mH8pc'></sub></form><legend id='mH8pc'></legend><bdo id='mH8pc'><pre id='mH8pc'><center id='mH8pc'></center></pre></bdo></b><th id='mH8pc'></th></span></q></dt></tr></i><div id='mH8pc'><tfoot id='mH8pc'></tfoot><dl id='mH8pc'><fieldset id='mH8pc'></fieldset></dl></div>
                      <tfoot id='mH8pc'></tfoot>