/** * Get the validation rules that apply to the request. * * @return array */ public function rules() { $rules = ['shirt_size' => 'required', 'spouse_gender' => 'required_with:spouse_first_name', 'spouse_shirt_size' => 'required_with:spouse_first_name', 'minor.*.shirt_size' => 'required_with:minor.*.first_name', 'minor.*.age' => 'required_with:minor.*.age', 'minor.*.gender' => 'required_with:minor.*.gender', 'first_name' => 'required_unless:registering_as_current_user,1|max:32', 'last_name' => 'required_unless:registering_as_current_user,1|max:32', 'email' => 'required_unless:registering_as_current_user,1|email|max:128', 'gender' => 'required_unless:registering_as_current_user,1']; // add address validation rules with a condition $addressRules = array_except(Address::validationRules(), ['name']); foreach ($addressRules as $field => $rule) { $rules[$field] = str_replace('required', 'required_unless:registering_as_current_user,1', $rule); } return $rules; }
/** * @return mixed */ public function postSetup(Request $request, Scrubber $scrubber) { $request->merge(['name' => 'Home', 'phone' => $scrubber->integer($request->get('phone'))]); $userRules = array_only(User::validationRules(), ['gender', 'phone', 'first_name', 'last_name']); $this->validate($request, array_merge(Address::validationRules(), $userRules), Address::validationMessages()); //update the info DB::transaction(function () use($request) { $user = Auth::user(); $user->first_name = $request->get('first_name'); $user->last_name = $request->get('last_name'); $user->phone = $request->get('phone'); $user->gender = $request->get('gender'); $user->save(); // set timezone $settings = $user->settings; $settings->setTimezone($request->input('timezone')); $user->update(['settings' => $settings]); // add user address $address = App::make(Address::class, [$request->except(['first_name', 'last_name', 'phone', 'gender', 'timezone', 'answer', 'other'])]); $user->addresses()->save($address); $user->update(['primary_address_id' => $address->id]); // record survey if ($request->has('answer') && count($request->get('answer')) > 0) { $surveys = []; foreach ($request->get('answer') as $questionId => $answers) { foreach ($answers as $answerId => $true) { $surveys[] = app(RegistrationSurvey::class, [['answer_id' => $answerId]]); } // update that question's "Other" if ($request->has('other.' . $questionId) && strlen($request->get('other')[$questionId]) > 0) { $otherAnswer = RegistrationSurveyAnswer::where('question_id', $questionId)->where('answer', 'Other')->first(); $surveys[] = app(RegistrationSurvey::class, [['answer_id' => $otherAnswer->id, 'other' => $request->get('other')[$questionId]]]); } } $user->surveys()->saveMany($surveys); } }); return redirect('/dashboard'); }
/** * @param AddressOwnerOnlyRequest $request * @param $id * * @return mixed */ public function update(AddressOwnerOnlyRequest $request, $id) { $this->validate($request, Address::validationRules(), Address::validationMessages()); Address::findOrFail($id)->update($request->all()); return redirect('/account/address')->withFlashSuccess('Your changes were saved'); }