Normally a search will have the first request a post, and subsequent request are get.
how can i validate a form with Get method usng FormRequest class?
hi I have implemented search and filter functionality in my project, and I want to validate the form using a Form Request class. The form has three parts:
A search input and a "Sort By" select field
Five "Search By" options
A submit button
What I need is a validation rule that triggers an error if the search input is not empty but none of the "Search By" options are selected. (Note: I’m using query strings for all of them.)
//controller
public function home(SearchFilterRequest $request){
/* $searchQuery = $request->query('search') ?? '';
$searchByQuery = $request->query('searchBy') ?? [];*/
/* $users = User::filterdSearch($searchQuery, $searchByQuery)
->sort('first_name')
->get();*/
$validation = $request->validated();
return view("home");
}
//form request
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
class SearchFilterRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
'search' => 'string',
'searchBy' => 'array',
'searchBy.*' => "string",
];
}
public function after(): array
{
return [
function(Validator $validator)
{
if( !empty($this->query('search')) && empty($this->query('searchBy')) ){
$validator->errors()->add('search', 'please select a search by');
}
}
];
}
}
//form
<form class="max-w-sm mx-auto" method="GET" action="{{ route('home') }}">
<div class="mb-5">
<label for="email" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Your
email</label>
<input type="text" id="email"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
placeholder="[email protected]" name="search" value="{{ old('search') }}"/>
</div>
<fieldset>
<div class="flex items-center mb-4 gap-2">
<div class="flex items-center">
<input id="checkbox-1" type="checkbox"
value="email"
name="searchBy[email]"
class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded-sm focus:ring-blue-500">
<label for="checkbox-1" class="ms-2 text-sm font-medium text-gray-900 dark:text-gray-300">email</label>
</div>
<div class="flex items-center">
<input id="checkbox-1" type="checkbox"
value="first_name" name="searchBy[first_name]"
class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded-sm focus:ring-blue-500">
<label for="checkbox-1" class="ms-2 text-sm font-medium text-gray-900 dark:text-gray-300">first
name</label>
</div>
</div>
</fieldset>
<button type="submit"
class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">
submit
</button>
</form>
@minaremonshaker I don't know why you would do that. The HTTP verb doesn't matter.
Fix the code instead. Don't try to read arrays with $request->query(). Use $request->input().
I assume that's your issue. You still haven't explained what in your code doesn't work.
There's also no need for the after() method. You can use normal validation rules like @ghabe suggested. It makes no difference if the input is in a query string or a POST body.
Please or to participate in this conversation.