8

I am using Laravel 5.4.

I have two fields in a form: travel_km and travel_rate

  1. Both fields must be numeric
  2. Both fields are optional but, travel_rate must be entered if travel_km is not empty

My rules: 'travel_km' => 'numeric|nullable', 'travel_rate' => 'sometimes|required_with:travel_km'

The problem is if travel_km is empty I still get an error on travel_rate because it is null.

If I put nullable on travel_rate I do not get an error even if travel_km is not empty.

To solve this I did this in my Controller:

if(empty($request->travel_km) && empty($request->travel_rate)) {
        $this->validate($request,
        [
            'customer_id' => 'required',
            'service_date' => 'required',
            'labour_hrs'    => 'numeric|nullable',
            'hourly_rate' => 'sometimes|required_with:labour_hrs',
        ],
        [
            'customer_id' => 'Please enter the customer',
            'service_date' => 'Please select the service date',
            'labour_hrs' => 'Please enter a valid number',
            'hourly_rate' => 'Please enter a valid number',
        ]);
       } else {
        $this->validate($request,
        [
            'customer_id' => 'required',
            'service_date' => 'required',
            'labour_hrs'    => 'numeric|nullable',
            'hourly_rate' => 'sometimes|required_with:labour_hrs',
            'travel_km' => 'numeric|nullable',
            'travel_rate' => 'sometimes|required_with:travel_km'
        ],
        [
            'customer_id' => 'Please enter the customer',
            'service_date' => 'Please select the service date',
            'labour_hrs' => 'Please enter a valid number',
            'hourly_rate' => 'Please enter a valid number',
            'travel_rate' => 'Please enter a valid number',
            'travel_km' => 'Please enter a valid number'
        ]);
    }

Is there any other way to solve this? I have this issue if a few cases and I don't want to more generic solution.

I looked at Custom Validation but could not understand how do I check if the travel_km is empty or not.

Validator::extend('foo', function ($attribute, $value, $parameters, $validator) {
        return is_numeric($value) && $parameter ????;
    });

Thank you

1 Answer 1

12

Try using required_if as below

'travel_km' => 'numeric|nullable', 
'travel_rate' => 'sometimes|required_if:travel_km'

This will validate travel_rate only if travel_km is present and non-empty. Here it will, allow you to put travel_km null when travel_km is not empty and travel_rate is empty it will show you error but won't show you error when travel_km has null value. Hope this helps.

UPDATE: nullable validation doesn't works for numeric value.

It can be accomplished by applying conditional rule as below,

if( !empty(Input::get('travel_km')) ){
  $rules['travel_km'] = 'numeric';
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, but it gives the same result, still get an error even though travel_km is empty: 'travel_rate' => 'sometimes|numeric|required_if:travle_km, true'
I would like to add that I did google it and it seems the numeric rule cannot be null because null is not a number, and that's where the problem comes in. That's why I need a custom validation
@SigalZahavi True. I have updated my answer as well
Great. Keep coding!

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.