草庐IT

php - Laravel 表单数组验证

coder 2024-04-07 原文

我需要一些帮助来使用 Laravel 5.4 验证我的表单。

我的表格:

{{Form::bsText('general[firstname]')}} 
{{Form::bsText('general[lastname]')}}

然后我有一个使用以下规则进行验证的 RequestObject:

'general[firstname]' => 'required|string:max:255',
'general[lastname]' => 'required|string:max:255',

通过这种方式,当它不像预期的那样为空时,它会生成错误“required”。 虽然当我填写一个字符串时,它仍然会给出所需的错误消息。

我还尝试了 laravel 文档中的以下内容:

'general.firstname' => 'required|string:max:255',
'general.lastname' => 'required|string:max:255',

和:

'general.*.firstname' => 'required|string:max:255',
'general.*.lastname' => 'required|string:max:255',

以上两个都不报错。

根据要求,这是我的完整请求对象:

/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    $user = Auth::user();

    return ($user && $user->isProjectManager()) ||
            ($user && $user->isAdmin());
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    switch($this->method()){
        case 'GET':
        case 'DELETE':
            return [];
        case 'POST':
            return [
                'name' => 'required|string|max:255',
                'email' => 'required|string|email|max:255|unique:users',
                'role' => 'in:2,3,4,5,6,7',
                'password' => 'required|string|min:6|confirmed',
                'project_manager_gegevens_photo' => 'required_if:role,2|mimes:png,jpeg,gif',
                'general[voornaam]' => 'required|alpha:max:255',
                'general[achternaam]' => 'required|string:max:255',
                'general[date]' => 'required_if:role,3,4,5|date|after:today',
                'general[telefoonnummer]' => 'required_if:role,3,4,5',
                'general[interne_medewerker]' => 'boolean',
                'general[geslacht]' => 'in:m,v,o',
            ];
        case 'PUT':
        case 'PATCH':
            return [
                'name' => 'required|string|max:255',
                'password' => 'required|string|min:6|confirmed',
            ];
        default:return [];
    }
}

证明它与数组验证有关: 当我将名称更改为:

{{Form::bsText('general_firstname')}} 

'general_firstname' => 'required|string:max:255'

它的验证就像您期望的那样。虽然,我喜欢干净和独立的东西,并且想要一个包含所有一般字段的数组。

那么,我怎样才能验证它是一个数组呢?

最佳答案

使用字母而不是字符串,

喜欢,

'general.firstname' => 'required|alpha|max:255'

替换这个,

'general[firstname]' => 'required|string:max:255',

引用文档, https://laravel.com/docs/5.4/validation#rule-alpha

关于php - Laravel 表单数组验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46541323/

有关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

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  4. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  5. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  6. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  7. 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

  8. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

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

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

  10. ruby - 检查数组是否在增加 - 2

    这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife

随机推荐