duanjingwei7239 2019-04-07 10:48
浏览 114

如何在同一(数组)laravel中验证多个输入

I have input as shown below: enter image description here

<input name="keyword[]" type="text" id="keyword" value="" ... required>
<input name="url[]" type="text" id="url" value="" ... required>

This is the result of input request to form an array in it:

array:4 [▼
  "_token" => "LSgeBGHL6QQEkirJFcSLC4T045vb2M4afsi48NeC"
  "category_id" => "1266"
  "keyword" => array:2 [▼
    0 => "test"
    1 => "test"
  ]
  "url" => array:2 [▼
    0 => "/search/test"
    1 => "/search/test"
  ]
]

I want to make a validation if the input array is the same and the data is already in the database before it will not be able to input. I have tried using validation as below, but it didn't work and got an error message.

public function postAdd()
{
    $data = \Input::all();

    $this->validate($data, [
        'keyword.*' => 'required|sometimes|unique',
        'url.*' => 'required|sometimes|unique'
    ]);
}

Please help me to be able to make the same input validation in the array and validate if the inputted data already exists in the database. Thanks!

  • 写回答

2条回答 默认 最新

  • dsqpx86002 2019-04-07 11:15
    关注

    'keyword.0' => 'required|unique:tablename,fieldname', 'keyword.1' => 'required|unique:tablename,fieldname',

    This how to access an validate each member of the array.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用