Laravel在一页上有多种形式

时间:2018-12-06 10:44:11

标签: php laravel

在我的Laravel应用程序中,我有一个注册过程,在该过程中,用户必须选择所属的类别,每个类别都采用自己的形式。

在每种形式中都有一组复选框,用户必须至少勾选一个复选框,否则验证将失败,我一直在阅读一些内容,发现了两个很好的类似问题:

Handling multiple forms on a single page

Laravel request validation with multiple forms on the same page

此刻,我将所有3种表格都用相同的方法输入:

/**
 * Store a user's selected investor type and progress onto next stage
 *
 * @param Request $request
 * @return void
 */
public function storeInvestorType(Request $request)
{
    $user = auth()->user();

    $user->investor_type = $request->get('investor_type');

    $user->declaration_date = Carbon::now();

    $user->save();

    Log::info("{$user->log_reference} has declared that they are a '{$user->investor_type}' investor.");

    return redirect()->route('user.member-type');
}

字面上只是更新数据库中的列。

拥有3种单独的方法还是只为每种形式命名会更清洁吗?

更新

我为每个提交按钮添加了name="something",以便我可以在控制器中执行以下操作:

/**
 * Store a user's selected investor type and progress onto next stage
 *
 * @param Request $request
 * @return void
 */
public function storeInvestorType(Request $request)
{
    $user = auth()->user();

    if ($request->has('high_net_worth')){
        if(!$request->has('high_net_worth_criteria')){
            return redirect()->back()->withErrors('Please tick at least one criteria that specifies you are a High Net Worth investor');
        } else{
            $investor_type = "High Net Worth";
        }
    } elseif ($request->has('self_certified_sophisticated')) {
        if (!$request->has('self_certified_sophisticated_criteria')) {
            return redirect()->back()->withErrors('Please tick at least one criteria that specifies you are a Self-Certified Sophisticated investor');
        } else {
            $investor_type = "Self-Certified Sophisticated";
        }
    } elseif ($request->has('other')) {
        $investor_type = "Other";
    }

    $user->investor_type = $investor_type;

    $user->declaration_date = Carbon::now();

    $user->save();

    Log::info("{$user->log_reference} has declared that they are a '{$user->investor_type}' investor.");

    return redirect()->route('user.member-type');
}

0 个答案:

没有答案