验证特定规则

编程入门 行业动态 更新时间:2024-10-27 08:34:03
本文介绍了验证特定规则-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用最新的Laravel版本.

I am using latest Laravel version.

我有requests/StoreUser.php:

public function rules() { return [ 'name' => 'required||max:255|min:2', 'email' => 'required|unique:users|email', 'password' => 'required|max:255|min:6|confirmed' ]; }

用于创建用户.

现在我需要并更新用户,但是如何仅执行特定规则?

Now I need and to update the user, but how can I execute only specific rules ?

例如,如果未提供name,而仅提供了email,那么如何仅对电子邮件运行验证?

For the example, if name is not provided, but only the email, how can I run the validation only for the email ?

推荐答案

这比您想象的要容易.使规则取决于HTTP方法.这是我的工作示例.

This is easier than you thought. Make rules depend on the HTTP method. Here is my working example.

public function rules() { // rules for updating record if ($this->method() == 'PATCH') { return [ 'name' => 'nullable||max:255|min:2', // either nullable or remove this line is ok 'email' => 'required|unique:users|email', ]; } else { // rules for creating record return [ 'name' => 'required||max:255|min:2', 'email' => 'required|unique:users|email', 'password' => 'required|max:255|min:6|confirmed' ]; } }

更多推荐

验证特定规则

本文发布于:2023-08-04 17:43:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:规则

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!