Laravel FormRequest:客户端发送带有点的属性

时间:2020-03-31 06:13:54

标签: laravel laravel-validation

客户端发送如下内容:

const data1 = [{
    city: 'LAKE GENEVA',
    state: 'WISCONSIN',
    theatreId: '000080',
    theatreDescription: 'GENEVA 4'
  },
  {
    city: 'BURLINGTON',
    state: 'WISCONSIN',
    theatreId: 'c05364',
    theatreDescription: 'PLAZA THEATRE 4'
  }
];

const data2 = [{
  city: 'MIAMI',
  state: 'FLORIDA',
  theatreId: 'c05170',
  theatreDescription: 'DOLPHIN 24'
}, {
  city: 'MIAMI',
  state: 'FLORIDA',
  theatreId: '000306',
  theatreDescription: 'CMX BRICKELL CITY CENTRE 10'
}];

const reduceCityTheaters = (arr) => Object.values(arr.reduce((acc, curr) => {
  // Deconstruct needed properties
  const { state, city, theatreId, theatreDescription } = curr;
  
  // Check if state not present
  if (!acc[state]) {
    let obj = {};
    obj.state = state;
    obj.city = [{
      desc: city,
      theatres: [{
        id: theatreId,
        desc: theatreDescription
      }]
    }];

    acc[state] = obj;
  } else { // Check if state is present
    acc[state].city = acc[state].city || [];
    const foundCity = acc[state].city.find(x => x.desc === city);

    // Check if city exists or not if not push it
    if (!foundCity) {
      acc[state].city.push({
        desc: city,
        theatres: [{
          id: theatreId,
          desc: theatreDescription
        }]
      });
    } else {
      const foundTheater = foundCity.theatres.find(x => x.id === theatreId);

      // Check if theatre exists or not if not push it
      if (!foundTheater) {
        foundCity.theatres.push({
          id: theatreId,
          desc: theatreDescription
        });
      } else {
        foundTheater.id = theatreId;
        foundTheater.desc = theatreDescription;
      }
    }
  }

  return acc;
}, {}));


const res1 = reduceCityTheaters(data1);
const res2 = reduceCityTheaters(data2);

console.log('1', res1);
console.log('2', res2);

在我尝试过的FormRequest类中:

foo.1.bar=hello

有人知道如何处理吗?

1 个答案:

答案 0 :(得分:0)

尝试手动获取参数的值,然后对其进行验证:

class YourRequest extends FormRequest
{
    public function rules(): array
    {
        return [...];
    }

    public function withValidator(Validator $validator): void
    {
        $value = $this->get('foo.1.bar') ?: $this->get('foo_1_bar');

        $validator->after(function (Validator $validator) use ($value) {
            if ($value == null) {
                $validator->errors()->add('foo.1.bar', 'Error Message.');
                $validator->errors()->add('foo_1_bar', 'Error Message.');
            }
        });
    }
}