Last active
April 1, 2019 10:14
-
-
Save grandmanitou/2a1c16eb32ddaa68543f4aab02ce458d to your computer and use it in GitHub Desktop.
Laravel conditional validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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