如何使 Laravel 的 Validator $rules 成为可选?

2023-04-09php开发问题
5

本文介绍了如何使 Laravel 的 Validator $rules 成为可选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

假设我有两种方法的 User 模型:

Let's say I have User model with two methods:

User.php

class User extends Eloquent
{  
    /* Validation rules */
    private static $rules = array(
        'user'  => 'unique:users|required|alpha_num',
        'email' => 'required|email'
    );

    /* Validate against registration form */
    public static function register($data)
    {
        $validator = Validator::make($data, static::$rules);
        if($validator->fails())
        {
            /*... do someting */
        }
        else
        {
            /* .. do something else */
        }
    }

    /* Validate against update form */
    public static function update($data)
    {
        $validator = Validator::make($data, static::$rules);
        if($validator->fails())
        {
            /*... do someting */
        }
        else
        {
            /* .. do something else */
        }
    }
}

我的问题:我怎样才能让验证规则成为可选的,所以即使 update() 的数据只是 email 字段,它会忽略 user 并仍然验证为 true.
这是可能的还是我遗漏了什么?

My question: How can I make validation rules optional, so even if data for update() would be just email field, it would ignore user and still validate to true.
Is this even possible or am I missing something?

抱歉我的英语不好.

推荐答案

不确定我的问题是否正确,但如果用户是可选的,则应从验证器中删除必需".这样你将有:

Not sure if I'm getting your question right but if the user is optional you should remove 'required' from the validator. This way you will have:

'user'  => 'unique:users|alpha_num',

代替:

'user'  => 'unique:users|required|alpha_num',

另一方面,我为我的模型创建了一个自定义方法,该方法能够根据传入参数返回自定义验证规则.

On the other hand I create a custom method for my models that is able to return custom validation rules depending on incoming parameters.

例如:

private function getValidationRules($rules)
{
    if ($rules == UPDATE_EMAIL)
    {
        return array('email' => 'required|email');
    } else {
        return array(
            'user'  => 'unique:users|required|alpha_num',
            'email' => 'required|email'
        );
    }
}

我想这只是个人选择,但我发现从方法中获取验证规则可以更好地控制我真正想要验证的内容,尤其是当您想要执行一些高级验证时.

I guess it's only a personal choice, but I have found that getting the validation rules from a method allows more control over what I really want to validate, especially when you want to perform some advanced validations.

希望对你有帮助.

这篇关于如何使 Laravel 的 Validator $rules 成为可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17