【问题标题】:Laravel only validate items that are posted and ignore rest of validation arrayLaravel 仅验证已发布的项目并忽略验证数组的其余部分
【发布时间】:2014-06-29 00:26:41
【问题描述】:

对于一个使用 Laravel 4.1 的项目,我有一个小 UI 问题想要解决。

一些输入在模糊时对 laravel 进行 ajax 调用,效果很好。它只是发送它的价值。然后在 laravel 中检查验证器。

public function validate() {
        if(Request::ajax()) {
            $validation = Validator::make(Input::all(), array(
                'email' => 'unique:users|required|email', 
                'username' => 'required'
            ));
            if($validation->fails()) {
                return $validation->messages()->toJson();
            }
            return "";
        }
        return "";
    }

虽然这可行,但 json 字符串还包含我不需要检查的字段。准确地说,这是我得到的反馈:

{"email":["The email field is required."],"username":["The username field is required."]}

但是看到它是模糊的,我只想要我实际检查的那个。因此,如果我模糊了电子邮件,我希望返回:

{"email":["The email field is required."]}

现在我知道这显然是因为我的数组包含多个字段,但我不想为我所做的每个可能的输入编写完整的验证。

我的问题是:我能否以某种方式只返回实际发布的帖子值,即使该值可能为 null 并且无法取回数组的其余部分。

【问题讨论】:

  • here
  • 对不起,不是我要找的。我有比 2 更多的输入。这将需要很多 if 语句。我基本上是在寻找替换 only() 或 all() 语句,它只检查发布的值。类似posted() 的东西(不存在)。

标签: php jquery arrays validation laravel-4


【解决方案1】:

试试这个(未经测试,如果它不起作用,请随时评论/否决):

// Required rules, these will always be present in the validation
$required = ["email" => "unique:users|required|email", "username" => "required"];

// Optional rules, these will only be used if the fields they verify aren't empty
$optional = ["other_field" => "other_rules"];

// Gets input data as an array excluding the CSRF token
// You can use Input::all() if there isn't one
$input = Input::except('_token');

// Iterates over the input values
foreach ($input as $key => $value) {
    // To make field names case-insensitive
    $key = strtolower($key);

    // If the field exists in the rules, to avoid
    // exceptions if an extra field is added
    if (in_array($key, $optional)) {
        // Append corresponding validation rule to the main validation rules
        $required[$key] = $optional[$key];
    }
}

// Finally do your validation using these rules
$validation = Validator::make($input, $required);

将您的必填字段添加到$required数组中,键是POST数据中的字段名称,以及$optional数组中的可选字段-仅当该字段存在于提交中时才使用可选字段数据。

【讨论】:

  • 我更喜欢这个,因为它有 except 标记。我要把这个给你:)
  • 我看到你忘记了一些东西:“if(Input::has($key)) {”。因为如果你不这样做,如果我没记错的话,它仍然会检查所有内容。
  • @Matt 查看我的更新版本,它消除了字段名称中的大小写敏感性,并且如果在提交的数据中添加了额外的字段,它也不会失败。
  • @Matt 但我看不到Input::has() 的需要,因为如果该值不存在,那么它就不会在$input 数组中并且不会被迭代.
【解决方案2】:

你还可以以更简洁的方式使用 Laravel 请求

  public function rules(){

     $validation = [];

     $input = Request::all();

     if (array_key_exists('email', $input)) {
         $validation['email'] = 'unique:users|required|email';
     }
     if (array_key_exists('username', $input)) {
         $validation['username'] = 'required|min:6';
     }

     return  $validation;
  }

【讨论】:

    【解决方案3】:

    我找到了。它会是这样的:

    if(Request::ajax()) {
    
            $arr = array();
            $arr['email'] = 'unique:users|required|email';
            $arr['username'] = 'required|min:6';
    
            $checks = array();
    
            foreach($arr as $key => $value) {
                if(Input::has($key)) {
                    $checks[$key] = $value;
                }
            }
    
            if(count($checks)) {
                $validation = Validator::make(Input::all(), $checks);
                if($validation->fails()) {
                    return $validation->messages()->toJson();
                }
            }
            return "ok";
        }
        return "";
    

    【讨论】:

    • 快要发的差不多了,还要发吗?
    猜你喜欢
    • 1970-01-01
    • 2016-09-01
    • 2020-09-04
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多