结合两个阵列数据进行Laravel验证

wgx48brx  于 2023-03-09  发布在  其他
关注(0)|答案(3)|浏览(153)

我如何从请求文件中验证它?我想检查floor_id和material_id的不同组合,一个floor_id不能有两个或更多相同的material_id
从一个表单数据我得到这种输出

project_id => 5,
date => 12-15-2022,
floor_id => array(
0=> 1,
1=> 5,
2=> 12
),
material_id => array(
0=>15,
1=>2,
2=>20
),
quantity => array(
0=>2500,
1=>3500,
2=>6522
)

我如何从请求文件中验证它

bqujaahr

bqujaahr1#

你可以使用验证distinct
https://laravel.com/docs/9.x/validation#rule-distinct

hl0ma9xz

hl0ma9xz2#

您可以编写自定义验证规则来满足您的需要。
在这里,您将了解一些情况https://laravel.com/docs/9.x/validation#custom-validation-rules

3lxsmp7m

3lxsmp7m3#

我找到了解决办法

class CsRequest extends FormRequest
{
    /**
     * Manupulate the request data before validation
     * @return void
     * 
     */
    public function prepareForValidation()
    {
        $material_id = $this->material_id;
        $brand_id = $this->brand_id;
        $material_brand = [];
        foreach ($material_id as $key => $value) {
            $material_brand[] = $value . '-' . $brand_id[$key];
        }
        $this->merge([
            'material_brand' => $material_brand,
        ]);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'cs_no'                   => ['required', 'string', 'max:255', Rule::unique('cs')->ignore($this->c, 'cs_no')], //$this->c is the id of the cs
            'effective_date'          => ['required', 'date', 'date_format:d-m-Y'],
            'expiry_date'             => ['required', 'date', 'date_format:d-m-Y', 'after:effective_date'],
            'remarks'                 => ['present', 'nullable', 'string'],

            'material_id'             => ['required', 'array'],
            'material_id.*'           => ['required', 'exists:materials,id'],
            'material_name'           => ['required', 'array'],
            'material_name.*'         => ['required', 'string'],
            'material_brand.*'          => ['required', 'distinct'],

            'supplier_id'             => ['required', 'array'],
            'supplier_id.*'           => ['required', 'exists:suppliers,id', 'distinct'],
            'supplier_name'           => ['required', 'array'],
            'supplier_name.*'         => ['required', 'string'],
            'checked_supplier'        => ['required', 'array'],
            'quotation_no'            => ['required', 'array'],
            'vat_tax'                 => ['required', 'array'],
            'vat_tax.*'               => ['required', 'string'],
            'credit_period'           => ['required', 'array'],
            'credit_period.*'         => ['required', 'string'],

            'price'                   => ['required', 'array'],
            'price.*'                 => ['required', 'numeric'],
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     * 
     * @return array
     */
    public function messages()
    {
        return [
            'material_brand.*.distinct'           => 'The material and brand combination must be unique.',
            'cs_no.required'                    => 'Reference No. is required',
            'cs_no.string'                      => 'Reference No. must be a string',
            'cs_no.max'                         => 'Reference No. may not be greater than 255 characters',
            'cs_no.unique'                      => 'Reference No. has already been taken',

            'effective_date.required'           => 'Effective Date is required',
            'effective_date.date'               => 'Effective Date must be a date',
            'effective_date.date_format'        => 'Effective Date must be in the format of d-m-Y',

            'expiry_date.required'              => 'Expiry Date is required',
            'expiry_date.date'                  => 'Expiry Date must be a date',
            'expiry_date.date_format'           => 'Expiry Date must be in the format of d-m-Y',
            'expiry_date.after'                 => 'Expiry Date must be after Effective Date',

            'remarks.present'                   => 'Remarks must be present',
            'remarks.nullable'                  => 'Remarks may be null',
            'remarks.string'                    => 'Remarks must be a string',

            'material_id.required'              => 'Material is required',
            'material_id.array'                 => 'Material must be an array',
            'material_id.*.required'            => 'Material is required',
            'material_id.*.exists'              => 'Material does not exist',
            'material_id.*.distinct'            => 'Material must be distinct',

            'material_name.required'            => 'Material Name is required',
            'material_name.array'               => 'Material Name must be an array',
            'material_name.*.required'          => 'Material Name is required',
            'material_name.*.string'            => 'Material Name must be a string',

            'supplier_id.required'              => 'Supplier is required',
            'supplier_id.array'                 => 'Supplier must be an array',
            'supplier_id.*.required'            => 'Supplier is required',
            'supplier_id.*.exists'              => 'Supplier does not exist',
            'supplier_id.*.distinct'            => 'Supplier must be distinct',

            'supplier_name.required'            => 'Supplier Name is required',
            'supplier_name.array'               => 'Supplier Name must be an array',
            'supplier_name.*.required'          => 'Supplier Name is required',
            'supplier_name.*.string'            => 'Supplier Name must be a string',

            'checked_supplier.required'         => 'Checked Supplier is required',
            'checked_supplier.array'            => 'Checked Supplier must be an array',

            'vat_tax.required'                  => 'VAT/Tax is required',
            'vat_tax.array'                     => 'VAT/Tax must be an array',
            'vat_tax.*.required'                => 'VAT/Tax is required',
            'vat_tax.*.string'                  => 'VAT/Tax must be a string',

            'quotation_no.required'             => 'Quotation No. is required',
            'quotation_no.array'                => 'Quotation No. must be an array',
            'quotation_no.*.required'           => 'Quotation No. is required',
            'quotation_no.*.string'             => 'Quotation No. must be a string',

            'credit_period.required'            => 'Credit Period is required',
            'credit_period.array'               => 'Credit Period must be an array',
            'credit_period.*.required'          => 'Credit Period is required',
            'credit_period.*.string'            => 'Credit Period must be a string',

            'price.required'                    => 'Price is required',
            'price.array'                       => 'Price must be an array',
            'price.*.required'                  => 'Price is required',
            'price.*.numeric'                   => 'Price must be a number',
        ];
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
}

通过提出这样的要求,我解决了同样的问题

相关问题