Skip to content

Instantly share code, notes, and snippets.

@grandmanitou
Last active April 1, 2019 10:14
Show Gist options
  • Select an option

  • Save grandmanitou/2a1c16eb32ddaa68543f4aab02ce458d to your computer and use it in GitHub Desktop.

Select an option

Save grandmanitou/2a1c16eb32ddaa68543f4aab02ce458d to your computer and use it in GitHub Desktop.
Laravel conditional validation
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use App\Http\Controllers\Controller;
use Validator;
use Illuminate\Support\Fluent;
use App\Registration;
class ChartController extends Controller
{
public function index(Request $request)
{
if ($request->isMethod('post')) {
$validator = Validator::make(
$request->all(),
[
'periodicity' => 'required', // string: day|week|month
'filter' => 'required', // string: date|month number|week number
]
);
$validator->sometimes('filter', 'integer|between:1,52', function ($input) {
return $input->periodicity === 'week';
});
$validator->sometimes('filter', 'integer|between:1,12', function ($input) {
return $input->periodicity === 'month';
});
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
}
return view('charts/admin/index');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment