I have a form with these fields:
shipping_method (hidden field with value="2" in my testing case)
courier_id
tracking_no
delivery_date
The courier_id field is required only if the shipping_method is 2
<div class="form-group{{ $errors->has('courier_id') ? ' has-error' : ''}}">
<label>Courier</label>
<select name="courier_id" class="form-control">
<option value="">Select...</option>
@foreach($couriers as $courier)
<option value="{{ $courier->id }}">{{ $courier->name }}</option>
@endforeach
</select>
@if ($errors->has('courier_id'))
<span class="help-block">
<strong>{{ $errors->first('courier_id') }}</strong>
</span>
@endif
</div>
In my controller:
public function update(Request $request, $id)
{
$delivery_date = Null;
$courier_id = Null;
$order = Order::findOrFail($id);
$status = 0;
if($order->status == 2) {
$status = 3;
$courier_id = $request->courier_id;
$messages = [ 'courier_id.required' => 'Please select a courier'];
$v = Validator::make(Input::all(), [
'tracking_no' => 'required',
'delivery_date' => 'required|date_format:d/m/Y'
], $messages)->validate();
$v->sometimes('courier_id', 'required', function (Request $request) {
return true;
//return $request->shipping_method == 2;
});
Even though the courier_id is empty, I don't get the error for it.
I have three questions:
Why even when I just return true on the sometimes check, I don't get the error
Not sure what is the parameter I should send to the sometimes check, is it Request $request?
I tried to send the order to the sometimes check instead of the request so I won't have to have a hidden field for it on the form, e.g.
$v->sometimes('courier_id', 'required', function ($order) { return $order->shipping_method == 2; });
Again, no error is displayed.
The order status is 2 for sure.
I also tried:
$v->sometimes('courier_id', 'required', function ($order) {
return $order->shipping_method == '2';
});
If I remove the sometimes check, and just add the courier_id validation as required, I do get the error on the form.