草庐IT

php - Laravel - 所有默认验证错误消息的列表

coder 2024-04-28 原文

我正在尝试将 Laravel Validator 类用于不是在 Laravel 中创建的 PHP 项目。本质上我正在做类似于 Illuminate Validator in stand-alone non-Laravel application 的事情

这是我的代码:

        $filesystem = new \Illuminate\Filesystem\Filesystem();
        $fileLoader = new \Illuminate\Translation\FileLoader($filesystem, '');
        $translator = new \Illuminate\Translation\Translator($fileLoader, 'en_US');

        $messages = [
            'required' => 'The :attribute field is required.',
            'string' => 'The :attribute field must be a string.',
        ];


        $factory = new \Illuminate\Validation\Factory($translator);
        $rules = [
            'title' => 'required'
        ];


        $validator = $factory->make($this->request->post(), $rules, $messages);

        $validator = new \Illuminate\Validation\Validator($translator, $this->request->post(), $rules);
        if ($validator->fails()) {
            $errors = $validator->errors();
            foreach ($errors->all() as $message) {
                var_dump($message);
            }
        }die;

我的问题是我需要自己创建所有错误验证消息。我想知道是否有办法使用默认的 Laravel 错误消息。我什至考虑将所有这些默认错误消息设置为单独文件中的一个完整数组,如果我能得到的话。我可以在哪里找到 Laravel 的所有默认验证错误消息?

最佳答案

这是所有默认错误信息的列表

https://github.com/laravel/laravel/blob/master/resources/lang/en/validation.php

[
'accepted'             => 'The :attribute must be accepted.',
'active_url'           => 'The :attribute is not a valid URL.',
'after'                => 'The :attribute must be a date after :date.',
'after_or_equal'       => 'The :attribute must be a date after or equal to :date.',
'alpha'                => 'The :attribute may only contain letters.',
'alpha_dash'           => 'The :attribute may only contain letters, numbers, dashes and underscores.',
'alpha_num'            => 'The :attribute may only contain letters and numbers.',
'array'                => 'The :attribute must be an array.',
'before'               => 'The :attribute must be a date before :date.',
'before_or_equal'      => 'The :attribute must be a date before or equal to :date.',
'between'              => [
    'numeric' => 'The :attribute must be between :min and :max.',
    'file'    => 'The :attribute must be between :min and :max kilobytes.',
    'string'  => 'The :attribute must be between :min and :max characters.',
    'array'   => 'The :attribute must have between :min and :max items.',
],
'boolean'              => 'The :attribute field must be true or false.',
'confirmed'            => 'The :attribute confirmation does not match.',
'date'                 => 'The :attribute is not a valid date.',
'date_format'          => 'The :attribute does not match the format :format.',
'different'            => 'The :attribute and :other must be different.',
'digits'               => 'The :attribute must be :digits digits.',
'digits_between'       => 'The :attribute must be between :min and :max digits.',
'dimensions'           => 'The :attribute has invalid image dimensions.',
'distinct'             => 'The :attribute field has a duplicate value.',
'email'                => 'The :attribute must be a valid email address.',
'exists'               => 'The selected :attribute is invalid.',
'file'                 => 'The :attribute must be a file.',
'filled'               => 'The :attribute field must have a value.',
'gt'                   => [
    'numeric' => 'The :attribute must be greater than :value.',
    'file'    => 'The :attribute must be greater than :value kilobytes.',
    'string'  => 'The :attribute must be greater than :value characters.',
    'array'   => 'The :attribute must have more than :value items.',
],
'gte'                  => [
    'numeric' => 'The :attribute must be greater than or equal :value.',
    'file'    => 'The :attribute must be greater than or equal :value kilobytes.',
    'string'  => 'The :attribute must be greater than or equal :value characters.',
    'array'   => 'The :attribute must have :value items or more.',
],
'image'                => 'The :attribute must be an image.',
'in'                   => 'The selected :attribute is invalid.',
'in_array'             => 'The :attribute field does not exist in :other.',
'integer'              => 'The :attribute must be an integer.',
'ip'                   => 'The :attribute must be a valid IP address.',
'ipv4'                 => 'The :attribute must be a valid IPv4 address.',
'ipv6'                 => 'The :attribute must be a valid IPv6 address.',
'json'                 => 'The :attribute must be a valid JSON string.',
'lt'                   => [
    'numeric' => 'The :attribute must be less than :value.',
    'file'    => 'The :attribute must be less than :value kilobytes.',
    'string'  => 'The :attribute must be less than :value characters.',
    'array'   => 'The :attribute must have less than :value items.',
],
'lte'                  => [
    'numeric' => 'The :attribute must be less than or equal :value.',
    'file'    => 'The :attribute must be less than or equal :value kilobytes.',
    'string'  => 'The :attribute must be less than or equal :value characters.',
    'array'   => 'The :attribute must not have more than :value items.',
],
'max'                  => [
    'numeric' => 'The :attribute may not be greater than :max.',
    'file'    => 'The :attribute may not be greater than :max kilobytes.',
    'string'  => 'The :attribute may not be greater than :max characters.',
    'array'   => 'The :attribute may not have more than :max items.',
],
'mimes'                => 'The :attribute must be a file of type: :values.',
'mimetypes'            => 'The :attribute must be a file of type: :values.',
'min'                  => [
    'numeric' => 'The :attribute must be at least :min.',
    'file'    => 'The :attribute must be at least :min kilobytes.',
    'string'  => 'The :attribute must be at least :min characters.',
    'array'   => 'The :attribute must have at least :min items.',
],
'not_in'               => 'The selected :attribute is invalid.',
'not_regex'            => 'The :attribute format is invalid.',
'numeric'              => 'The :attribute must be a number.',
'present'              => 'The :attribute field must be present.',
'regex'                => 'The :attribute format is invalid.',
'required'             => 'The :attribute field is required.',
'required_if'          => 'The :attribute field is required when :other is :value.',
'required_unless'      => 'The :attribute field is required unless :other is in :values.',
'required_with'        => 'The :attribute field is required when :values is present.',
'required_with_all'    => 'The :attribute field is required when :values is present.',
'required_without'     => 'The :attribute field is required when :values is not present.',
'required_without_all' => 'The :attribute field is required when none of :values are present.',
'same'                 => 'The :attribute and :other must match.',
'size'                 => [
    'numeric' => 'The :attribute must be :size.',
    'file'    => 'The :attribute must be :size kilobytes.',
    'string'  => 'The :attribute must be :size characters.',
    'array'   => 'The :attribute must contain :size items.',
],
'string'               => 'The :attribute must be a string.',
'timezone'             => 'The :attribute must be a valid zone.',
'unique'               => 'The :attribute has already been taken.',
'uploaded'             => 'The :attribute failed to upload.',
'url'                  => 'The :attribute format is invalid.'
]

关于php - Laravel - 所有默认验证错误消息的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51285885/

有关php - Laravel - 所有默认验证错误消息的列表的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  4. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  5. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  6. ruby - 默认情况下使选项为 false - 2

    这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb

  7. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  8. ruby - RVM 使用列表[0] - 2

    是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论

  9. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  10. ruby-on-rails - 如何将验证与模型分开 - 2

    我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:

随机推荐