0
votes

I've been stuck checking all the related rules for an hour:

Here is my validation rule request:

class CreateRewardRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public static function rules()
    {
        return [
            'parameters.name' => ['required', 'string', 'min:1'],
            'parameters.maxQty' => ['required', 'integer', 'min:1'],
            'parameters.maxPerUser' => ['nullable', 'min:1'],
            'parameters.maxPerDay' => ['nullable', 'min:1'],
            'parameters.pickWeight' => ['required', 'integer', 'min:1'],
        ];
    }
}

Here is my test request:

parameters[name]: null
parameters[maxQty]: null
parameters[maxPerUser]: null
parameters[maxPerDay]: null
parameters[pickWeight]: null

The error response:

{
    "message":"The given data was invalid.",
    "errors":{
        "parameters.maxQty":["The parameters.max qty must be an integer."],
        "parameters.pickWeight":["The parameters.pick weight must be an integer."],
    }
}

The parameters.name field is not even mentioned in the error, but the doc says:

required

The field under validation must be present in the input data and not empty. 
A field is considered "empty" if one of the following conditions are true:

The value is null.

The value is an empty string.

I want to reject NULL value for this field, because it is "required", but it doesn't seem to work.

If I don't send the field I do have the validation error showing up, but I don't want an empty/null value to pass validation.

Did I miss anything here?

Thanks!

PS: Here is the VueJS code in case my form submission might be the culprit:

  let formData = new FormData();
  for (let param in this.parameters) {
    if (this.parameters.hasOwnProperty(param)) {
      formData.append('parameters[' + param + ']', this.parameters[param]);
    }
  }
  axios.post("/api/JdCode", formData).then(response => {
    console.log(response);
  });

EDIT: Using dd($request->all()), I noticed that all null FormData values are treated as string "null", which is why required rule doesn't work on name field, and that maxQty and pickWeight required rule does not trigger any error, but integer fires a must be an integer error, since it receives a "null" string. Not sure if I'm clear, but it looks like a bug related to requests sent with Content-Type: multipart/form-data.

Any insight?

1

1 Answers

0
votes

Maybe add the 'present', 'not_in:null' or Rule::requiredIf(true),

public static function rules()
    {
        return [
            'parameters.name' => ['required', 'string', 'min:1', 'not_in:null'],
            'parameters.maxQty' => ['required', 'integer', 'min:1', 'present'],
            'parameters.maxPerUser' => ['nullable', 'min:1'],
            'parameters.maxPerDay' => ['nullable', 'min:1'],
            'parameters.pickWeight' => ['required', 'integer', 'min:1', 'present'],
        ];
    }