Example #1
0
 /**
  * Class constructor.
  *
  * Initialize class properties.
  *
  * @param  string $title          Title of the report
  * @param  string $view           View file for showing the results
  * @param  array  $fields         Form fields labels and validation rules
  * @param  array  $input          Default form input
  * @param  string $offCanvasClass Class to apply to the off-canvas
  *
  * @return void
  */
 public function __construct($title, $view, array $fields, array $input, $offCanvasClass = null)
 {
     // Setup layout
     parent::__construct();
     $this->title = $title;
     $this->view = $view;
     list($this->labels, $this->rules) = \App\Validation\Validator::parseRules($fields);
     $this->defaultInput = $input;
     $this->offCanvasClass = $offCanvasClass;
 }
 /**
  * Validate data
  *
  * @param array $data
  * @return array
  */
 public function validate(array $data)
 {
     $this->validator->validate($data);
     return $this->validator->errors;
 }
Example #3
0
 /**
  * Set validation rules and labels.
  *
  * @param  array
  *
  * @return Model
  */
 protected function setRules(array $rules)
 {
     list($this->labels, $this->rules) = \App\Validation\Validator::parseRules($rules);
     return $this;
 }
Example #4
0
 /**
  * Save resource to the data base using transactions.
  *
  * @param  string $action
  *
  * @return Response
  */
 protected function persist($action)
 {
     try {
         DB::beginTransaction();
         // Validate resource relationships
         if ($this->relationships) {
             list($labels, $rules) = \App\Validation\Validator::parseRules($this->relationships);
             $validator = Validator::make(Input::only(array_keys($rules)), $rules)->setAttributeNames($labels);
             if ($validator->fails()) {
                 $this->resource->validate();
                 $this->resource->getErrors()->merge($validator->messages()->toArray());
                 throw new ModelValidationException(_('Wrong relationships data'));
             }
         }
         // Validate and save resource
         if ($this->resource->save() === false and $this->resource->getErrors()->count()) {
             throw new ModelValidationException(_('Wrong data'));
         }
         // Save resource relationships
         foreach ($this->relationships as $relationship => $notUsed) {
             $this->resource->{$relationship}()->sync(Input::get($relationship, []));
         }
         // Success :)
         DB::commit();
         if ($action === 'update') {
             $successMesssage = _('%s successfully updated');
         } elseif ($action === 'store') {
             $successMesssage = _('%s successfully created');
         } else {
             $successMesssage = _('%s successfully saved');
         }
         Session::flash('success', sprintf($successMesssage, $this->resource));
         return redirect()->route("{$this->prefix}.show", [$this->resource->getKey()]);
     } catch (\Exception $e) {
         DB::rollBack();
         if ($e instanceof ModelValidationException) {
             Session::flash('error', $e->getMessage());
         } else {
             throw $e;
         }
         // Unexpected exception, re-throw it to be able to debug it.
         return redirect()->back()->withInput()->withErrors($this->resource->getErrors());
     }
 }
 public function registerPost(Request $request, Response $response, $args)
 {
     $email = Input::post('email');
     $username = Input::post('username');
     $password = Input::post('password');
     $passwordConfirm = Input::post('password_confirm');
     $v = new Validator(new User());
     $v->validate(['email' => [$email, 'required|email|uniqueEmail'], 'username' => [$username, 'required|alnumDash|max(20)|uniqueUsername'], 'password' => [$password, 'required|min(6)'], 'password_confirm' => [$passwordConfirm, 'required|matches(password)']]);
     if ($v->passes()) {
         $user = new User();
         $user->email = $email;
         $user->username = $username;
         $user->password = $this->hash->password($password);
         $user->group_id = 3;
         $user->save();
         $flash = "You have been registered.";
     } else {
         $flash = "registration failed.";
     }
     $this->view->render($response, 'register.twig', ['errors' => $v->errors(), 'flash' => $flash, 'request' => $request]);
     return $response;
 }