Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Admin scaffolding edit method wants to call a static method validate on model
  • Testing out the admin scaffolding through oil, but wondering what I'm supposed to do about the method validate, is there any sample code of what I'm supposed to do for this method


    This is part of the code that the scaffolding generates
     public function action_edit($id = null)
          {
                $user = Model_Checkin::find($id);
                $val = Model_Checkin::validate('edit');

                if ($val->run()) {
                      $user->privacy = Input::post('privacy');
                      $user->current = Input::post('current');
                      $user->message = Input::post('message');
                      $user->photo = Input::post('photo');
                      $user->video = Input::post('video');

                      if ($user->save()) {
                            Session::set_flash('success', e('Updated user #' . $id));

                            Response::redirect('admin/user');
                      }
                      else {
                            Session::set_flash('error', e('Could not update user #' . $id));
                      }
                }
                else {
                      if (Input::method() == 'POST') {
                            $user->privacy = $val->validated('privacy');
                            $user->current = $val->validated('current');
                            $user->message = $val->validated('message');
                            $user->photo = $val->validated('photo');
                            $user->video = $val->validated('video');

                            Session::set_flash('error', $val->error());
                      }

                      $this->template->set_global('user', $user, false);
                }

                $this->template->title = "user";
                $this->template->content = View::forge('admin/user/edit');
          }
  • The validate() method is generated as well, as part of the model. The method gets the name of the function passed so you know if it's a create or an edit, and you can validate accordingly.

    It is done this way because it's a generic solution that works for both Model_Crud and ORM. ORM has it's own validation solution, so if you're using ORM only, it might be better to use that (validation rules are part of the properties).

    The idea is that validation should not be part of your controller (as you do now) but part of the model. So no matter where in your code you add or edit a model object, you have it's validation at hand.

    It is another reason why ORM's built-in validation is better. Solutions like this require you to fetch the data to be validated yourself (like Input::post), but ORM validates internally, so it doesn't matter where the data comes from and how it ended up in the object.

    I have no clue why oil now generates validation code inside the controller, I need to look at the commit history, this is new to me. It is not logical, and it is not how Fuel was designed.
  • The scaffold doesn't generate a new model, at least it didn't for me and I wouldn't want it to overwrite the files anyways. 

    So basically there shouldn't be any input validation in the controller? Just set the model properties to the Input::() variables and then let ORM validate on save ?
  • When you're using ORM and validation rules in the ORM properties, you have two options.

    The easiest is to let save() do it, put that in a try/catch block and capture the validation exception. Downside of this is that you only get one error, since save() stops at the first validation error.

    If you want all errors (for example because you want to mark the fields in error), you need to do run the ORM validation inside the controller.

    In the Auth examples there's an example of such a flow (on the User model), see http://docs.fuelphp.com/packages/auth/examples/auth.html#/registration

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion