0
votes

I'm using Laravel 4.2.8 and trying to validate the next form: Form

The first select field is required. And only one is required from the next three fields. Phone with formatting is the last. And another two are for digits (some IDs). I validate in controller, the code is next:

public function getApplication()
{
    $input = Input::except('_token');
    Debugbar::info($input);
    $input['phone'] = preg_replace('/[^0-9]/', '', $input['phone']); // remove format from phone
    $input = array_map('intval', $input); // convert all numeric data to int
    Debugbar::info($input);

    $rules = [ // Validation rules
        ['operation-location' => 'required|numeric'],
        ['app-id' => 'numeric|min:1|required_without_all:card-id,phone'],
        ['card-id' => 'numeric|digits:16|required_without_all:app-id,phone'],
        ['phone' => 'numeric|digits:12|required_without_all:app-id,card-id']
    ];

    $validator = Validator::make($input, $rules);
    if ($validator->passes()) {
        Debugbar::info('Validation OK');

        return Redirect::route('appl.journal', ['by' => 'application']);
    }
    else { // Validation FAIL
        Debugbar::info('Validation error');
        // Redirect to form with error
        return Redirect::route('appl.journal', ['by' => 'application'])
            ->withErrors($validator)
            ->withInput();
    }
}

As you may see I convert numeric IDs to integers myself and leave only number for phone number. The problem is when I submit form as it is, it passes validation, despite one field is required and starter phone format is too short. I've tried changing required_without_all to just required on all fields (!), but it still passes fine with blank empty form submitted. And I expect at least one field to be properly filled.

Debug of my inputs. Initial:

    array(4) [
    'operation-location' => string (1) "0"
    'app-id' => string (0) ""
    'card-id' => string (0) ""
    'phone' => string (6) "+3 8(0"
]

After conversion to int:

    array(4) [
    'operation-location' => integer 0
    'app-id' => integer 0
    'card-id' => integer 0
    'phone' => integer 380
]

Posted similar smaller problem to Laravel issues.

1

1 Answers

0
votes

I know this sounds weird but I think this is just an issue with your rules array.

You current rules array is an array of arrays. The Validator looks for an array with keys and values. I believe your current rules are being parsed as keys, but with no value. And then the Validator was basically seeing no rules, and it automatically passed. Try this.

$rules = [
    'operation-location' => 'required|numeric',
    'app-id' => 'numeric|min:1|required_without_all:card-id,phone',
    'card-id' => 'numeric|digits:16|required_without_all:app-id,phone',
    'phone' => 'numeric|digits:12|required_without_all:app-id,card-id'
];