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

Time:10-31

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.');
    }

CodePudding user response:

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.

CodePudding user response:

For numbers "between", two approches:

Like @ericmp says, you have the "between" rule in form validation :

('type_id' => ['required', 'integer', 'size:4', 'between:0,4'])

But if you needs more rules, or custom rules in one, you can create a custom validation rule file:

php artisan make:rule MyRule

There is an example on this Laracast topic: Best way to validate range in Laravel?

There is laravel's documentation about Validation rules: Validation: Laravel Doc

  • Related