3

How to validate by Id laravel

now i use this in my controller. My problem is In my html I've my input type text like this

<input type="text" name="quantity_box[]" class="form-control" autofocus="" />
<input type="text" name="quantity_box[1]" class="form-control" autofocus="" />
<input type="text" name="quantity_box[2]" class="form-control" autofocus="" />

well, as you see in my html It check only my first input if my quantity_box[1] is empty it will return error offset 1 , that's not sure how to work this out

 $this->validate($request, [
        'id_p' => 'required',
        'id_c' => 'required',
        'quantity_box'=>'required',


    ]
3
  • For best practise I recommend to make use of Form Request Validation laravel.com/docs/5.5/validation#form-request-validation Commented Oct 21, 2017 at 16:41
  • thank you sir i'll check it out Commented Oct 21, 2017 at 16:44
  • I've put an example of this method as a second answer in case you or someone else might want to use it sometime. Commented Oct 21, 2017 at 16:57

2 Answers 2

3

Well, since you are validating the $request variable, you should be validating the input name.

If you are using Laravel 5.2+ , you can validate arrays like so.

 $validator = Validator::make($request->all(), [
        'quantity_box.*' => 'required',
     ]);
Sign up to request clarification or add additional context in comments.

3 Comments

Is thier anyway to change validate by id ? be cause It can be quantity_box[10] I dnt know how many user will insert data
@test1321 Yes there is a method for that.
could you give me an example please @LarsMertens
1

For best practise I recommend working with Laravel's 5.5 Form Request Validation laravel.com/docs/5.5/validation#form-request-validation

Using this way you will keep your controller code as clean as possible.


First let's make a Request to store our validation and authentication rules in

php artisan make:request myQuantityBoxRequest

myQuantiyBoxRequest.php

<?php 

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Auth;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'quantity_box.*' => 'required'
        ];
    }
}

Controller Function Example

use App\Http\Requests\myQuantityBoxRequest;

public function postQuantityBoxData(myQuantityBoxRequest $request){
     // Do something after validation here
}

There you go. If you make use of this function it will validate the input as if you're using $this->validate()

1 Comment

thank you sir this make me more understand thank a lot sir

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.