The question is already asked here for a previous version of laravel and not yet answered.
I have a html form which is validated using three different
If I understand correctly, you have 3 forms, each with their own form requests to deal with their respective validation. You also have another form which combines those 3 forms somewhere else and you don't want to repeat yourself by rewriting those validation rules.
In which case, I would still suggest going with a single form request, but try to combine the rules of each of those individual requests. For example, you use static methods to define your rules on the 3 individual form requests and have each individual request call its own static method to grab them:
class TransportationRequest extends FormRequest
{
public static function getRules()
{
return []; // Return rules for this request
}
public function rules()
{
return static::getRules();
}
}
class PurchaseRequest extends FormRequest
{
public static function getRules()
{
return []; // Return rules for this request
}
public function rules()
{
return static::getRules();
}
}
class SaleRequest extends FormRequest
{
public static function getRules()
{
return []; // Return rules for this request
}
public function rules()
{
return static::getRules();
}
}
And then have your combined request merge all three sets:
class CombinedRequest extends FormRequest
{
public function rules()
{
return array_merge(
TransportationRequest::getRules(),
SaleRequest::getRules(),
PurchaseRequest::getRules()
);
}
}
Then you can use the single CombinedRequest in your controller method. Of course, if you don't like the static method approach, in your combined request rules method you could just new up each individual request and call the rules method on each of them and merge the results.
class CombinedRequest extends FormRequest
{
public function rules()
{
$transportation = (new TransportationRequest())->rules();
$sale = (new SaleRequest())->rules();
$purchase = (new PurchaseRequest())->rules();
return array_merge(
$transportation,
$sales,
$purchase
);
}
}