0

How to validate with custom request, my request with an array key

$request = [
  'link_inc_characteristic_id' => $inc_char_id[$i],
  'value' => $value[$i],
  'created_by' => $created_by,
  'last_updated_by' => $last_updated_by,
];

$this->validate($request, [
   'value['.$i.']' => 'max:30'
]);

$linkIncCharacteristicValue = LinkIncCharacteristicValue::create($request);
return Response::json($linkIncCharacteristicValue);

[EDIT] [CODE UPDATED] display error:

Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given,

5
  • What errors are you getting? Commented Mar 2, 2016 at 12:05
  • Validation does not work and no error..... input value automatic cut on database field length, no error from validation.. Commented Mar 2, 2016 at 12:39
  • I just renewed code.. and getting error Commented Mar 2, 2016 at 12:50
  • 1
    I think, you store not array to $request['value'] = $value[$i] but you trying validate 'value['.$i.']' => 'max:30', if you need validate array you must type 'value.*' => 'max:30' Commented Mar 2, 2016 at 12:55
  • this my submitted data on console, with ajax ; value[18] value[19] value[20] value[5] asasasaasshgafshgafshgafshgafsfafsasasasa value[7] value[8] value[9] Commented Mar 2, 2016 at 13:06

3 Answers 3

2

The Controller::validate() is not a generic validation method, it validates requests. For this use case, simply use the validator directly:

Validator::make($data, ['value' => 'max:30']);
Sign up to request clarification or add additional context in comments.

Comments

1

The error message is telling you what’s wrong:

Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given

So pass the Request instance to the validate() method:

public function store(Request $request)
{
    $this->validate($request, [
        'value.*' => 'max:30',
    ]);
}

Check out the following resources:

Comments

0

Its because I am putting the validation inside loop and not use * character to validate an array

...
for ($i=0; $i < $count ; $i++) {
   ...
   $this->validate($request, [
      'value['.$i.']' => 'max:30'
   ]);
   ...
   // save etc
}

right code, put validation before loop, and use * character for array validation :

$this->validate($request, [
      'value.*' => 'max:30'
]);
...
for ($i=0; $i < $count ; $i++) {
   // save etc
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.