Search code examples
phplaraveleloquentcontrollerrequest-validation

(Laravel) I want to store my data only if my "type_id" is a number between 0 ad 4


I have a form on my .blade that has a filed "type_id" and it must a number between 0 and 4

is it possible to create a multiple size? for example 'size:0 || size:1 || size:2 || size:3 || size 4' ???

that's my function store: (but the most important part is the type_id field)

public function store(Request $request)
    {
        $toCreate = $request->validate([
            'type_id' => ['required','integer','size:4'],
            'name' => ['required', 'string', 'max:255'],
            'partitaIva' => ['max:255'],
            'codiceFiscale' => ['max:255'],
            'sedeAmministrativa' => ['max:255'],
            'indirizzoNotifica' => ['max:255'],
            'referente' => ['max:255'],
            'responsabile' => ['max:255'],
            'telefono' => ['max:255'],
            'fax' => ['max:255'],
            'email' => ['max:255'],
            'pec' => ['max:255'],
            'capitale' => ['max:255'],
            'nDipendenti' => ['max:255'],
            'convenzioniDeleghe' => [],
            'note' => []
        ]);

        
        Administration::create($toCreate);

        return redirect()->route('administrations.index')->with('success','Amministratore aggiunto con successo.');
    }


Solution

  • You can use "between" validator. eg.

    'type_id' => ['required', 'integer', 'between:1,4']
    

    or

    'type_id' => 'required|integer|between:1,4',
    

    size is used to validate the length of array, string or a file.