0

How can we show validation error message in LARAVEL where we have input array as:

<input type="text" name="start_time[]" class="form-control start_time" value="" >

because, start_time => 'required', does not work here

2
  • @Akintunde-Rotimi, I have tried the same initially but does not work, it works well in CodeIgniter. Commented Nov 12, 2018 at 7:52
  • @BhargavChudasama: Thanks for the link, but I did not get this, why is he including a blade file in between, I am new to laravel. Commented Nov 12, 2018 at 7:55

2 Answers 2

0

You should try this:

$validator = Validator::make($request->all(), [
    "start_time.*"  => "required",
]);

Updated Answer

$validator = $request->validate([
    "start_time"    => "required|array|min:10",
    "start_time.*"  => "required",
]);
Sign up to request clarification or add additional context in comments.

3 Comments

@Saurabh: If my answer work for you then please accept my answer
It is showing "The start time must have at least 10 items.". When I am changing "start_time" => "required|array|min:10", to "start_time" => "required|array|min:1", Then it is not showing any error. Thanks for replying again.
Hello Sir, Please reply.
0

Your rule has to be like this:

'start_time.*' => 'required'

I tested your situation with below details and everything worked fine.

view :

<form action="{{ route('test.store') }}" method="post">
    {{ csrf_field() }}

    <input type="text" name="start_time[]" class="form-control start_time" value="" >

    <input type="text" name="start_time[]" class="form-control start_time" value="" >

    <button type="submit" class="btn btn-primary">Submit</button>
</form>

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

controller :

public function store(Request $request)
{
    $request->validate([
        'start_time.*' => 'required'
    ]);

errors when form is blank :

The start_time.0 field is required.
The start_time.1 field is required.

4 Comments

@Saurabh if you show me a dd of your request()->all() that would be helpful
Right now I am submitting the blank form and I am expecting to get validation error messages. But I am providing you dd of the field I need to show error validation message, "start_time" => array:2 [▶] after expanding 'start_time', array:2 [▼ 0 => "12:10" 1 => "04:25" ]
@Saurabh Are you sure you're displaying errors correctly according to laravel doc : laravel.com/docs/5.7/validation
@Saurabh I tested your situation and there was no problem. you can see it in my updated answer. So maybe you should give me more details about your view and controller to find your problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.