Laravel only validates submitted items and ignores the rest of the validation array

For a project with Laravel 4.1, I have a small user interface problem that I would like to solve.

Some inputs make the laravel ajax call blurry, and this works fine. He just sends that value. In laravel, I check with a validator.

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 "";
    }

Although this works, the json line also contains fields that I do not need to check. To be precise, this is the feedback I get:

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

But, seeing that this is blurry, I only need the one on which I really check in return. Therefore, if I blur the email, I want to return:

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

Now I know this because my array contains several fields, but I do not want to write a full check for every possible input I have ever made.

: - , , .

+4
3

(, /downvote, ):

// 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 - , .

+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;
  }
+2

. :

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 "";
+1

Source: https://habr.com/ru/post/1540111/


All Articles