Laravel - 每次迭代为数组添加输入值

时间:2016-12-16 02:46:56

标签: php arrays laravel laravel-5 array-push

我在这里尝试将所有输入值推送到数组并稍后通过邮件功能传递。我设置了我的数组$ stack,通过每个表单POST尝试了一个array_push(),甚至之后它似乎没有存储在上一步中提交的值。如果有帮助,这是一个多步形式。所以我的问题是,如何存储输入值并正确推送到数组末尾?

public function submitApplication(Request $request){
        $input = $request->all();
        $formStep = $input['formStep'];
        $stack = [];
        $rules = [];
        switch ($formStep) {
            case 'step1':
                $rules = [
                    'name'               => 'required|regex:/^[a-zA-Z\s]*$/',
                    'email'              => 'required|email',
                    'phone'              => 'required|regex:/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/',
                    'age_above_18_yn'    => 'required',
                    // 'age_above_18_yes' => 'required'
                ];
                array_push($stack, $input);
                break;
            case 'step2':
                // dd('step2');
                $rules = [
                    // 'emp_1_start'        => 'required',
                    // 'emp_1_end'          => 'required',
                    // 'emp_1_title'        => 'required',
                    // 'emp_1_salary'       => 'required',
                    // 'emp_1_sup_name'     => 'required|regex:/^[a-zA-Z\s]*$/',
                    // 'emp_1_sup_phone'    => 'required|regex:/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/',
                    // 'emp_1_duties'       => 'required',
                    // 'emp_2_start'        => 'required',
                    // 'emp_2_end'          => 'required',
                    // 'emp_2_title'        => 'required',
                    // 'emp_2_salary'       => 'required',
                    // 'emp_2_sup_name'     => 'required|regex:/^[a-zA-Z\s]*$/',
                    // 'emp_2_sup_phone'    => 'required|regex:/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/',
                    // 'emp_2_duties'       => 'required',
                    // 'emp_3_start'        => 'required',
                    // 'emp_3_end'          => 'required',
                    // 'emp_3_title'        => 'required',
                    // 'emp_3_salary'       => 'required',
                    // 'emp_3_sup_name'     => 'required|regex:/^[a-zA-Z\s]*$/',
                    // 'emp_3_sup_phone'    => 'required|regex:/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/',
                    // 'emp_3_duties'       => 'required'
                ];
                array_push($stack, $input);
                break;
            case 'step3':
                $rules = [
                    'ref_1_name'         => 'required|regex:/^[a-zA-Z\s]*$/',
                    'ref_1_phone'        => 'required|regex:/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/',
                    'ref_1_relationship' => 'required',
                    'ref_2_name'         => 'required|regex:/^[a-zA-Z\s]*$/',
                    'ref_2_phone'        => 'required|regex:/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/',
                    'ref_2_relationship' => 'required',
                    'ref_3_name'         => 'required|regex:/^[a-zA-Z\s]*$/',
                    'ref_3_phone'        => 'required|regex:/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/',
                    'ref_3_relationship' => 'required'
                ];
                array_push($stack, $input);
                break;
            case 'step4':
                $rules = [
                    'availability'       => 'required',
                    'commitments'        => 'required',
                    'hear_about_us'      => 'required',
                    // 'comments'           => 'required'
                ];
                array_push($stack, $input);
                break;
            default:
                # code...
                break;
        }

        $messages = [
            'name.required'               => 'The Name Field is Required',
            'email.required'              => 'The Email Field is Required',
            'phone.required'              => 'The Phone Field is Required',
            'age_above_18_yn.required'    => 'The Are You Above 18 Field is Required',
            // 'age_above_18_yes.required'   => 'The Optional',
            'emp_1_start.required'        => 'Employer 1 Start Date Field is Required',
            'emp_1_end.required'          => 'Employer 1 End Date Field is Required',
            'emp_1_title.required'        => 'Employer 1 End Date Field is Required',
            'emp_1_salary.required'       => 'Employer 1 Salary Field is Required',
            'emp_1_sup_name.required'     => 'Employer 1 Supervisor Name Field is Required',
            'emp_1_sup_phone.required'    => 'The Employer 1 Supervisor Phone Number Field is Required',
            'emp_1_duties.required'       => 'Employer 1 List of Duties Field is Required',
            'emp_2_start.required'        => 'Employer 2 Start Date Field is Required',
            'emp_2_end.required'          => 'Employer 2 End Date Field is Required',
            'emp_2_title.required'        => 'Employer 2 End Date Field is Required',
            'emp_2_salary.required'       => 'Employer 2 Salary Field is Required',
            'emp_2_sup_name.required'     => 'Employer 2 Supervisor Name Field is Required',
            'emp_2_sup_phone.required'    => 'The Employer 2 Supervisor Phone Number Field is Required',
            'emp_2_duties.required'       => 'Employer 2 List of Duties Field is Required',
            'emp_3_start.required'        => 'Employer 3 Start Date Field is Required',
            'emp_3_end.required'          => 'Employer 3 End Date Field is Required',
            'emp_3_title.required'        => 'Employer 3 End Date Field is Required',
            'emp_3_salary.required'       => 'Employer 3 Salary Field is Required',
            'emp_3_sup_name.required'     => 'Employer 3 Supervisor Name Field is Required',
            'emp_3_sup_phone.required'    => 'The Employer 3 Supervisor Phone Number Field is Required',
            'emp_3_duties.required'       => 'Employer 3 List of Duties Field is Required',
            'ref_1_name.required'         => 'The Reference 1 Name Field is Required',
            'ref_1_phone.required'        => 'The Reference 1 Phone Number Field is Required',
            'ref_1_relationship.required' => 'The Reference 1 Relationship Field is Required',
            'ref_1_name.required'         => 'The Reference 2 Name Field is Required',
            'ref_1_phone.required'        => 'The Reference 2 Phone Number Field is Required',
            'ref_1_relationship.required' => 'The Reference 2 Relationship Field is Required',
            'ref_3_name.required'         => 'The Reference 3 Name Field is Required',
            'ref_3_phone.required'        => 'The Reference 3 Phone Number Field is Required',
            'ref_3_relationship.required' => 'The Reference 3 Relationship Field is Required',
            'availability.required'       => 'The Your Availability Field is Required',
            'commitments.required'        => 'The Your Prior Commitments Field is Required',
            'hear_about_us.required'      => 'The How Did You Hear About Us Field is Required',
            'comments.required'           => 'The Comments Field is Required'
        ];
        array_push($stack, $input);
        $validator = Validator::make($input, $rules, $messages);
        $ivInputs = [];
        var_dump($stack);

        if ($validator->fails()) {
            foreach($validator->invalid() as $key => $value){
                $ivInputs[] = $key;
            }
            return json_encode(array("pass" => false, "message" => $validator->messages()->all(), 'ivInputs' => $ivInputs));
        }
        else {
            if($formStep == 'step4'){
                // dd($stack);
                Mail::send('emails.submitApplication', $ivInputs, function($message) use ($ivInputs) {
                    $message->from("noreply@igloofrozencustard.com", "New Employment Application");
                    $message->to('justin@sfp.net')->subject('New Employment Application');
                });
            }
            else {
                return json_encode(array("pass" => true, "message" => "You Have a New Employment Application"));
                var_dump($ivInputs);
            }

        }
    }

0 个答案:

没有答案