douchen7555 2017-09-04 07:02
浏览 42

Laravel Validation独特的条件

I'm having trouble getting the Validator to work with the rule that I want to set up. I could have made multiple tables, but it's going to be a lot of work to undo everything, so I'd prefer to make it work if possible.

$validator = Validator::make($input, [
        'segment_name' => [
          'required',
        Rule::unique('groups')->where(function ($query) {
        $query->where('lang', $list_name)->where('group_type', 'auto_segment');
      }),
    ],
  ]);

I thought query was the way to do it, but I now realise that this adds another constraint instead. What I want is the validation to fail when the 2 query conditions are met, but otherwise then it should pass. Is there any easy way to do this?


After spending more time working on this, I've made some more attempts, but it hasn't yet worked. Using 2 rules, the validator fails when I would want it to pass, similar to using || in an if statement instead of &&. Using a pipe between the two rules failed and generated an offset error, but this might not have been correct.

'segment_name' => 'unique:groups,segment_name,NULL,id,lang,' . $list_name,
'segment_name' => 'unique:groups,segment_name,NULL,id,group_type,auto_segment'

'segment_name' => 'unique:groups,segment_name,NULL,id,lang,' . $list_name . '|unique:groups,segment_name,NULL,id,group_type,auto_segment'

After finally getting the original query to work without variables, ended up using a global variable to pass to the query, as a local wouldn't work.

  • 写回答

1条回答 默认 最新

  • dqt66847 2017-09-04 07:12
    关注

    For unique you can do like this -

    $rules = [
        'segment_name' => 'required|unique', // you can unique validate like this also 'unique:tablename', for specific table 
    ];
    
    //Validate inputs
    $input = Input::only('segment_name');
    
    $validator = Validator::make($input, $rules);
    

    Hope this is helpful for you.

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用