public function register_post() { $gump = new GUMP(); $form = $gump->sanitize($_POST); $gump->validation_rules(array("firstname" => "required|valid_name", "lastname" => "required|valid_name", "street" => "required|street_address", "zip" => "required|numeric,min_len=4", "city" => "required", "country" => "required", "email" => "required|valid_email", "password" => "required", "password_verify" => "required")); $validation = $gump->run($form); if ($validation === false) { $errors = $gump->errors(); for ($i = 0; $i < count($errors); $i++) { $this->form[$errors[$i]["field"]]["error"] = true; } } else { if ($user = (new Login())->createLogin($form["email"], $form["password"], $form["company"], $form["firstname"], $form["lastname"], $form["street"], $form["zip"], $form["city"], $form["country"])) { $session = new \Base\Session(); $session->set("user_id", $user->getId()); (new Request())->redirect("dashboard"); } } $this->assign("error_message", "E-Mail oder Passwort falsch."); $this->register(); }
/** * Check if data as posted and validate * fields with rules specified in rules.yml * @param string $rule * @param array $unset * @return array */ public function posts($rule = '', array $unset = []) { if (\Request::isPost()) { $results = ['valid' => false]; /** * get all posts */ $posts = \Request::post(); /** * unset unused fields if * needed */ if (sizeof($unset)) { foreach ($unset as $fields) { unset($posts[$fields]); } } /** * get rules */ $rules = $rule ? Config::get('rules.' . $rule) : []; /** * use GUMP library to validate * and sanitize fields */ $validator = new \GUMP(); $posts = $validator->sanitize($posts); $validator->validation_rules($rules); $validated = $validator->run($posts); /** * check validations result */ if (!$validated) { $results['error'] = $validator->errors(); $results['data'] = $posts; } else { $results['valid'] = true; $results['data'] = $posts; } return $results; } return []; }
function processForm($data, $user) { $gump = new GUMP(); $data = $gump->sanitize($data); $gump->validation_rules(array('user_target_name' => 'required', 'repair_post_id' => 'required|integer', 'repair_type_id' => 'required|integer', 'user_target_id' => 'required|integer', 'startdatetime' => 'required', 'enddatetime' => 'required', 'customer_car_gv_number' => 'required', 'customer_car_mileage' => 'integer', 'customer_car_name' => 'required', 'customer_car_vin' => 'required', 'customer_name' => 'required', 'customer_phone' => 'required', 'customer_id' => 'integer', 'customer_car_id' => 'integer', 'id' => 'integer', 'state' => 'required|integer')); $gump->filter_rules(array('user_target_name' => 'trim|sanitize_string', 'customer_car_gv_number' => 'trim|sanitize_string', 'customer_car_name' => 'trim|sanitize_string', 'customer_car_vin' => 'trim|sanitize_string', 'customer_name' => 'trim|sanitize_string', 'customer_phone' => 'trim|sanitize_string')); $customer_car_id = null; $customer_id = null; $validated_data = $gump->run($data); if ($validated_data) { $customer_car = null; $customer = null; // добавляем авто if (!isset($validated_data['customer_car_id'])) { $customer_car = new CustomerCar(); } else { $customer_car = CustomerCar::retrieveByPK($validated_data['customer_car_id']); } $customer_car->gv_number = $validated_data["customer_car_gv_number"]; $customer_car->mileage = $validated_data["customer_car_mileage"]; $customer_car->name = $validated_data["customer_car_name"]; $customer_car->vin = $validated_data["customer_car_vin"]; try { $customer_car->save(); $customer_car_id = $customer_car->id; Log::toDebug(["Save CustomerCar", $customer_car_id]); } catch (Exception $ex) { Log::toDebug("ERROR_SAVE_TO_DATABASE"); return ["err" => "ERROR_SAVE_TO_DATABASE"]; } // добавляем заказчика if (!isset($validated_data['customer_id'])) { $customer = new Customer(); } else { $customer = Customer::retrieveByPK($validated_data['customer_id']); } $customer->name = $validated_data["customer_name"]; $customer->phone = $validated_data["customer_phone"]; try { $customer->save(); $customer_id = $customer->id; Log::toDebug(["Save CustomerCar", $customer_id]); } catch (Exception $ex) { return ["err" => "ERROR_SAVE_TO_DATABASE"]; } try { if (!isset($validated_data['id'])) { $new_event = new GreaseRatEvent(); } else { $new_event = GreaseRatEvent::retrieveByPK($validated_data['id']); } $new_event->repair_post_id = $validated_data["repair_post_id"]; $new_event->repair_type_id = $validated_data["repair_type_id"]; if (isset($user)) { $new_event->user_owner_id = $user->id; } $new_event->user_target_id = $validated_data["user_target_id"]; $new_event->state = $validated_data["state"]; $new_event->customer_id = $customer_id; $new_event->customer_car_id = $customer_car_id; $new_event->startdatetime = $validated_data["startdatetime"]; $new_event->enddatetime = $validated_data["enddatetime"]; $new_event->save(); Log::toDebug(["Save rat event", $new_event->id]); return ['event' => $new_event]; } catch (Exception $ex) { return ["err" => "ERROR_SAVE_TO_DATABASE"]; } } else { return ["err" => "VALIDATE_FORM_ERROR", "errors" => $gump->errors()]; } }