【问题标题】:PHP/Laravel: Nested object is turned into arrayPHP/Laravel:嵌套对象变成数组
【发布时间】:2021-11-12 11:31:06
【问题描述】:

为了验证传入的请求数据,我使用了 validate 方法。我必须创建不同的模型,所以我对不同的数据使用不同的验证规则。

我的传入数据如下所示

{
    "type": "b2b",
    "registration_number": "123456789",
    "vat_number": "123456789",
    "company_name": "Test company",
    "legal_entity_type": "NV",
    "phone": "1234567",
    "email": "test@company.com",
    "physical_address": {
        "street": "Teststreet",
        "number": "12",
        "addition": "5",
        "zip": "12345",
        "city": "Brussels",
        "country": "Belgium"
    },
    "billing_address": {
        "street": "Teststreet",
        "number": "12",
        "addition": "5",
        "zip": "12345",
        "city": "Brussels",
        "country": "Belgium"
    },
    "contacts": [
        {
            "function": "CEO",
            "first_name": "John",
            "last_name": "Doe",
            "phone": "123456789",
            "email": "john.doe@gmail.com"
        },
        {
            "function": "COO",
            "first_name": "Jane",
            "last_name": "Doe",
            "phone": "987654321",
            "email": "jane.doe@gmail.com"
        }
    ]
}

验证基本信息不是问题

$clientData = $request->validate([
    'type' => ['required', 'string', 'in:b2b,b2c'],
    'vat_number' => ['string', 'max:255'],
    'company_name' => ['string', 'max:255'],
    'legal_entity_type' => ['string', 'max:255'],
    'phone' => ['required', 'string', 'max:255'],
    'email' => ['required', 'string', 'max:255'],
    'first_name' => ['string', 'max:255'],
    'last_name' => ['string', 'max:255'],
    'date_of_birth' => ['date'],
]);

但是嵌套对象是。我想验证物理地址和帐单地址,但 php 正在将这些对象转换为数组。这样做会导致"Call to a member function validate() on array" 错误:

$physicalAddress = $request['physical_address']->validate([
    'street' => ['required', 'string', 'max:255'],
    'number' => ['required', 'string', 'max:255'],
    'addition' => ['string', 'max:255'],
    'zip' => ['required', 'string', 'max:255'],
    'city' => ['required', 'string', 'max:255'],
    'country' => ['required', 'string', 'max:255'],
]);

Laravel docs我发现你可以使用点来验证嵌套数组数据

$physicalAddress = $request->validate([
    'physical_address.street' => ['required', 'string', 'max:255'],
    'physical_address.number' => ['required', 'string', 'max:255'],
    'physical_address.addition' => ['string', 'max:255'],
    'physical_address.zip' => ['required', 'string', 'max:255'],
    'physical_address.city' => ['required', 'string', 'max:255'],
    'physical_address.country' => ['required', 'string', 'max:255'],
]);

但结果是一个只有一个元素的数组

[2021-09-17 15:24:59] local.DEBUG: array (
  'physical_address' => 
  array (
    'street' => 'Teststreet',
    'number' => '12',
    'addition' => '5',
    'zip' => '12345',
    'city' => 'Brussels',
    'country' => 'Belgium',
  ),
)  

要进一步使用它,我必须将 $physicalAddress 的第一个元素分配给 $physicalAddress:$physicalAddress = $physicalAddress[0]

因为这很简单,所以我不喜欢这个额外的行。对我来说,这感觉就像是 hacky 编码,我认为有更好的解决方案可以将它作为一个对象首先保留。

有什么建议吗?

【问题讨论】:

    标签: php laravel validation


    【解决方案1】:

    $physicalAddress = $request['physical_address']->validate() 这不起作用,因为validate() 是一个请求方法,而$request['physical_address'] 是一个数组。

    使用验证有什么问题?

    $validated = $request->validate([
        'street' => 
        'number' => 
        'addition' => 
        'zip' => 
        'city' => 
        'country' =>
    
        'physical_address.street' =>
        'physical_address.number' => 
        'physical_address.addition' =>
        'physical_address.zip' => 
        'physical_address.city' => 
        'physical_address.country' => 
    
        'billing_address.street' => 
        'billing_address.number' => 
        'billing_address.addition' => 
        'billing_address.zip' => 
        'billing_address.city' => 
        'billing_address.country' => 
    
        'contacts.*.function' => 
        'contacts.*.first_name' => 
        'contacts.*.last_name' => 
        'contacts.*.phone' => 
        'contacts.*.email' => 
    ]);
    

    【讨论】:

    • 地址/联系方式部分不能用这种方法重用
    • 什么意思? 'contacts.*.function' 应该匹配联系人数组的所有元素。或者你说的是physical_address和billing_address是如何重复的?
    • 我有其他控制器功能,其中请求包含地址或联系人。我想分离验证部分,以便可以在整个项目中重复使用它。
    猜你喜欢
    • 2017-06-14
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    相关资源
    最近更新 更多