Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

haakym's avatar

Multiple Forms, Multiple Requests?

I am creating a page that will search a database of students and return data on the student that matches it. I want to allow the user to search by different criteria such as bar-code, ID, email, etc.

My current approach is using a form for each search criteria as I only want the user to search by one criteria and not multiple, so I have a form to search for a bar-code, a form to search by ID and so on. Not one form with multiple fields and one submit button.

Of course I want rules on each form which will be different for each form, but it doesn't seem good practice to have a different method fire in the controller for each form and a Request for each form. So I'm trying to come up with a way to abstract the requests or rules for the request based on what form is fired but I'm coming up with nothing at the moment so I was hoping someone here could provide some guidance on how I could approach this in a better manner.

Thanks!

0 likes
8 replies
pmall's avatar

Just have a select box to select a search_type value, and your search field will be named search_term.

Then use only one StudentSearchFormRequest. In the rules method, construct the rule for search_term according to search_type.

# StudentSearchFormRequest.php

public function rules()
{
  if($this->search_type == 'type1') $rules = '...';
  if($this->search_type == 'type2') $rules = '...';

  return ['search_term' => $rules];
}

Then add a scopeSearch($search_type, $search_term) in your student model, and as many scopes as search types.

# Student.php

public function scopeSearch($query, $type, $term)
{
  if($type == 'type1') $query->searchType1($term);
  if($type == 'type2') $query->searchType2($term);
  // ...
}

public function scopeSearchType1($query, $term)
{
  // ...
}
bestmomo's avatar

Why do you say that "it doesn't seem good practice to have a different method fire in the controller for each form and a Request for each form" ? However looks like the simplest solution.

haakym's avatar

Thanks for your replies guys!

@RachidLaasri Wa alaykum salaam, - I'll definitely check this out. I am assuming you mean that I should use this as one form rather than multiple?

@pmall As for using a dropdown box I had considered this but the interface is for an event where the system will print off tickets and I want to have quick access to the any of the input boxes. In my opinion it just seems it might be a bit slower for that one extra click as we will be serving possibly more than 1000 customers in a day for the event so time is quite crucial. Thanks for your input though.

As for the if statements (or I guess I could use a switch?) I had considered something like this, and correct me if I'm wrong, but I remember Jeffrey saying in one of the videos something to the effect of if you're using switch (or ifs) to determine such behaviour you should be refactoring but perhaps this is for a different scenario.

@bestmomo I believe that all the search methods will be performing similar actions: grab the data coming in, validate it, search the db with the data, return something. So I thought it might make sense to group the functionality with a level of abstraction into one method. Perhaps I'm trying too hard and making something difficult out of something simple?

Again, thanks for all your input guys. Most appreciated!

Shovels's avatar

From a UX perspective you may be better off removing the different methods of performing different types of search. In the back-end you could then use reg-ex to detect what type of search they're performing, or loop through the various options (results could be grouped by type).

For example a user might not distinguish the difference in your terminology between a bar-code and an ID. Selecting the wrong option will return no results, but using the method above they'll always get results.

Simple UI = Happy user :)

pmall's avatar

@haakym

As for using a dropdown box I had considered this but the interface is for an event where the system will print off tickets and I want to have quick access to the any of the input boxes. In my opinion it just seems it might be a bit slower for that one extra click as we will be serving possibly more than 1000 customers in a day for the event so time is quite crucial. Thanks for your input though.

So just put a hidden field with the search_type alongside every searchboxes.

As for the if statements (or I guess I could use a switch?) I had considered something like this, and correct me if I'm wrong, but I remember Jeffrey saying in one of the videos something to the effect of if you're using switch (or ifs) to determine such behaviour you should be refactoring but perhaps this is for a different scenario.

Yes but here I think it is overkill.

uxweb's avatar

I agree with @shovels, one search form for any kind of search. And then in the controller method you can make an eloquent search "OR" criteria by the fields in context of the search like: find the search term by id or by bar_code or by other_field.

Hope it helps bro!.

Please or to participate in this conversation.