自定义验证规则无法正常工作(规则通过但不应通过)

时间:2018-07-31 16:36:02

标签: php laravel

我有下面的表格供用户选择他想要的每种注册类型的数量。

例如,如果会议具有两种注册类型(例如:普通和加号),则用户可以选择他要注册类型“通用”的数量“ 1”和注册类型“加号”的数量“ 1”。如果用户进行了此选择,则他会转到注册页面,它可以正常工作。

但是,如果用户没有为任何注册类型选择任何数量,而不是出现验证错误,则会显示“未定义的变量:selectedRtypes “。

您知道RegistrationTypeQuantity自定义规则中的错误在哪里吗?

用户为每种注册类型存储所选数量的方法

public function storeQuantities(Request $request, $id, $slug = null)
{

    $request->validate([
        'rtypes' => ['required', 'array', new RegistrationTypeQuantity],
    ]);

    $rtypeQuantities = $request->get('rtypes');

    $total = 0;
    foreach ($rtypeQuantities as $rtypeName => $quantity) {
        if ($quantity) {

            $rtype = RegistrationType::where('name', $rtypeName)->firstOrFail();
            $price = $rtype->price;

            $selectedRtypes[$rtype->name]['quantity'] = $quantity;
            $selectedRtypes[$rtype->name]['price'] = $price;
            $selectedRtypes[$rtype->name]['subtotal'] = $price * $quantity;
            $total += $selectedRtypes[$rtype->name]['subtotal'];
            $selectedRtypes[$rtype->name]['total'] = $total;

            $selectedRtypes[$rtype->name]['questions'] = $rtype->questions;
            $selectedRtypes[$rtype->name]['id'] = $rtype->id;
            //dd($selectedRtypes);
        }
    }

    Session::put('selectedRtypes', $selectedRtypes);
    Session::put('customQuestions', $selectedRtypes[$rtype->name]['questions']);
    Session::put('total', $total);

    return redirect(route('conferences.registration', 
    ['id' => $id, 'slug' => $slug]));
}

RegistrationTypeQuantity规则:

public function passes($attribute, $value)
    {

        foreach($value as $key=>$v) {

            // if $v is null 
            if ( is_null($v)) return false;

            $rtype = RegistrationType::where('name',$key)->first();
            // if there is no $rtype 
            if ( ! $rtype) return false;

            if($v == 0)
                return true;

            // $rtype was found
            if ( ($v < $rtype->min_participants || $v > $rtype->max_participants) )
                return false;
        }
        return true;
    }

如果用户未选择任何数量

$rtypeQuantities = $request->get('rtypes');
dd($rtypeQuantities);

显示:

array:2 [▼
  "general" => "0"
  "plus" => "0"
]

在用户的选择菜单中,为每种注册类型选择数量,只应允许用户在min_participant和max_participant之间选择一个值。

min_participants是registration_types表的一列,表示这是用户可以为注册选择的最小数量。

max_participants是registration_types表的一列,表示这是用户可以为注册类型选择的最大数量。

因此,例如,如果注册类型“常规”的min_participants为“ 0”且max_participants为“ 2”,则用户只能为注册类型“常规”选择0到2之间的数量。

 <select class="custom-select form-control rtype_name" id="rtype_{{ $rtype->id }}" 
                        data-price="{{ $rtype->price }}"
                        name="rtypes[{{ $rtype->name }}]">
                    <option value="0">0</option>
                    @for ($i = $rtype->min_participants; $i <= $rtype-> max_participants; $i++)
                        <option value="{{ $i }}">{{ $i }}</option>
                    @endfor
                </select>

因此,自定义规则应验证用户是否填写选择菜单字段,因为这是必需的,并且用户引入的值介于min_participants和max_participants之间。

2 个答案:

答案 0 :(得分:0)

仅当$selectedRtypes为真时,才在foreach循环内定义$quantity。因此,只要您没有有效的$selectedRtypes$quantity都将是未定义的。

由于依赖于定义而在循环之外定义$selectedRtypes,或验证输入数组以确保提供了有效数量。

后者似乎是必需的,因为您还依赖于$rtype,该if ($quantity)也在循环内定义并且依赖于有效的数量和类型。数量0似乎通过了您的验证,但未通过您的if i == "Property Address" and df.loc[i+1, :] != "Property Address": TypeError: must be str, not int 检查。

答案 1 :(得分:0)

您需要在foreach之前添加

$selectedRtypes = [];

,您可能在Session::put('customQuestions', $selectedRtypes[$rtype->name]['questions']);中出错。循环总是有最后一个结果作为键。