I have a form in which I show fines and consumptions, and a button that sends the selected elements. The question is whether you can make the FormRequest
validate that one of the two is sent.
return [
'consumos'=>'required',
'multas'=>'required'
];
The previous code does not work for me because it forces to choose both, so can it be with formRequest? or you should validate with javascript
before submitting the form. Thanks.
Solution
public function generarPago(Request $peticion)
{
$num_medidor = $peticion->get('num_medidor');
$prueba1 = Validator::make($peticion->all(), [
'consumos' => 'required',
]);
$prueba2 = Validator::make($peticion->all(), [
'multas' => 'required',
]);
if ($prueba1->fails() && $prueba2->fails()) {
return redirect('http://localhost/getHistorial/'.$num_medidor)
->withErrors('Para generar el pago es necesario seleccionar una multa o un consumo.')
->withInput();
}else if(!$prueba1->fails()){
foreach ($peticion->get('consumos') as $m)
{
$this->pconsumo($num_medidor, $m);
}
}else{
foreach ($peticion->get('multas') as $m)
{
$this->pmulta($num_medidor, $m);
}
}
}
public function pmulta($num_medidor, $id)
{
echo 'Multa '.$id. ' Cancelada! <br>';
}
public function pconsumo($num_medidor, $id)
{
echo 'Consumo '.$id. ' Cancelado! <br>';
}