laravel验证如果传递参数不相等

时间:2017-05-03 11:27:12

标签: php laravel

我正在创建一个允许用户以多种语言创建博客帖子的应用程序。每种语言都有一个包含不同字段的选项卡。他们可以定义标题,内容和slu ..当他们保存博客帖子时,所有参数都通过FormRequest类进行验证,如下所示:

{  
   "_method":"PUT",
   "_token":"eEDkVUU0cEcfAe3EQCiUIEdvCjTBqLFPT8CwW22D",
   "translations":{  
      "nl":{  
         "title":"A animi non est id repellat.nl",
         "slug":"veritatis-quos-iusto-eumnl",
         "content":"<p>ad-voluptate-sed-istenl<\/p>"
      },
      "en":{  
         "title":"Velit dignissimos suscipit unde et quod.en",
         "slug":"cupiditate-in-quaerat-uten",
         "content":"<p>rem-corporis-aut-aten<\/p>"
      },
      "fr":{  
         "title":"Mollitia reiciendis hic accusamus id dignissimos.fr",
         "slug":"voluptatem-iusto-quasi-rationefr",
         "content":"<p>accusantium-tempore-eos-nisifr<\/p>"
      },
      "de":{  
         "title":"Esse quasi ut delectus molestiae rem.de",
         "slug":"voluptates-excepturi-labore-mollitiade",
         "content":"<p>earum-delectus-velit-distinctiode<\/p>"
      }
   }
}

现在我希望slug是唯一的,但是正常的唯一规则不起作用,因为传递的params还没有添加到数据库中。什么是验证slu的最佳方法,所以这些都是独一无二的?

1 个答案:

答案 0 :(得分:0)

如果你绝对必须让他们为每个人创建自定义slu,你可以尝试使用different validation rule

$this->validate($request, [
    'slug_field_1' => 'different:slug_field_2|different:slug_field_3|...',
    'slug_field_2' => 'different:slug_field_1|different:slug_field_3|...','
    ...
]);