dongren1011 2017-12-10 21:33
浏览 124
已采纳

在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!

  • 写回答

1条回答 默认 最新

  • dongwo6477 2017-12-10 21:52
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line