bruno-alod
bruno-alod

Reputation: 27

Validate form agaisnt modified selectable values in Laravel

I have this form.

{!! Form::open(['action' => 'ArticlesController@store', 'method' => 'post', 'enctype' => 'multipart/form-data']) !!}
   <div class="form-group">
      Form::select('size', array(
        'L' => 'Large', 
        'S' => 'Small'
     ));
   </div>
{!! Form::close() !!}

The user will have a dropdown list to select Large (value: L) or Small (value: S). But if the user, let say, changes the value of any of those options using the dev tools, or whatever.

How can I validate the form if the user sends the 'size' field with a value that wasn't originally in the select options?

I mean, how can I check that the sent value is L or S, but not anything else.

Because the user could easily edit the form and send whatever value he wants to send, he could send a value that wasn't suppose to be sent.

I can do that using the validate class, but if instead of a 2 options list it is a 100 options list that'd be impossible.

Thanks!

Upvotes: 0

Views: 37

Answers (1)

lewis4u
lewis4u

Reputation: 15047

This is how you can validate that:

$request->validate(['size' => 'required|in:L,S']);

this part after pipe "|in:L,S'" is used to check if the $request attribute value is equal to any value in that rule.

https://laravel.com/docs/5.5/validation#rule-in

Upvotes: 1

Related Questions