Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 601107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:40:43+00:00 2026-05-13T16:40:43+00:00

I’ve been working with PHP for about a year, but I do it as

  • 0

I’ve been working with PHP for about a year, but I do it as a hobby. I dont have anybody I can go to as a teacher or a mentor to give me advice on what I may be doing completely wrong, or what I could do better. I’ve done quite a few different things within that year, so I wouldnt consider myself a complete noob.

Anyways, I have just started using a framework (Kohana), and there really arent that many tutorials out there, so I’m not entirely sure if I’m doing things in a good way.

I have a few code snippets that I would like to post to get some feedback pertaining to what I just said.

For Starters

User Controller

class User_Controller extends Template_Controller{

    public function register()
{
    // logged in users cant register
    if($this->logged_in)
    {
        url::redirect('user/profile');
    }

    // initially show an empty form
    $form = $errors = array
    (
        'username'      => '',
        'email'         => '',
        'password'      => '',
        'gender'        => '',
        'dob_month'     => '',
        'dob_day'       => '',
        'dob_year'      => '',
        'date_of_birth' => '',
        'captcha'       => '',
        'registration'  => ''
    );

    // check for a form submission
    if($this->input->post('register'))
    {
        // get the form
        $post = $this->input->post();

        // prepare the data for validation
        $post['date_of_birth'] = "{$post['dob_year']}-{$post['dob_month']}-{$post['dob_day']}";

        // create a new user
        $user = ORM::factory('user');

        // validate and register the user. 
        if($user->register($post, TRUE))
        {
            // SEND EMAIL

            // login using the collected data
            if(Auth::instance()->login($post->username, $post->password, TRUE))
            {
                // redirect the user to the profile page
                //url::redirect("user/profile/{$user->id}");
            }
        }

        // get validation errors and repopulate the form
        $form   = arr::overwrite($form,   $post->as_array());
        $errors = arr::overwrite($errors, $post->errors('registration_errors'));
    }

    // template variables
    $this->template->title = 'Sign Up';
    $this->template->body  = new View('layout_1');

    // layout variables
    $this->template->body->left  = new View('user/registration_form');
    $this->template->body->right = 'Right Side Content';

    // registration form variables
    $this->template->body->left->form    = $form;
    $this->template->body->left->errors  = $errors;
    $this->template->body->left->captcha = new Captcha('register');
}
}

Register Function within User_Model

class User_Model extends ORM{

    public function register(array& $user, $save = FALSE)
{
    $user = new Validation($user);

    // logged in users cant register
    if(Auth::instance()->logged_in())
    {
        $user->add_error('registration', 'logged_in');
        return FALSE;
    }

    // trim everything
    $user->pre_filter('trim')

        // everything is required
        ->add_rules('*', 'required')

        // username must be 5 - 30 alphanumeric characters and available
        ->add_rules('username', 'length[5,30]', 'valid::alpha_numeric', array($this, 'username_available'))

        // email must be valid format and available
        ->add_rules('email', 'valid::email', array($this, 'email_available'))

        // password must be 5 - 15 characters and alpha dash
        ->add_rules('password', 'length[5,15]', 'valid::alpha_dash')

        // gender must be either male or female. capitalize first letter
        ->add_rules('gender', array($this, 'valid_gender'))
        ->post_filter('ucfirst', 'gender')

        // dob must be a valid date, and user must be old enough.
        ->add_callbacks('date_of_birth', array($this, 'check_dob'))

        // captcha must be entered correctly.
        ->add_rules('captcha', 'Captcha::valid');

    // add the registration date
    $this->registration_date = date::unix2mysql();  // helper function transforms the current unix to mysql datetime format

    // validate the information. an ORM function.
    $result = parent::validate($user, $save);

    // was the user info valid?
    if($result === TRUE)
    {
        // was the user saved?
        if($save === TRUE)
        {
            // add a login role
            $this->add(ORM::factory('role', 'login'));
            $this->save();
        }
    }
    else
    {
        $user->add_error('registration', 'failed');
    }

    return $result;
}
}

Mostly all my models follow the same format when validating info.

I have some other things I would appreciate feedback on as well, but I dont want to overwhelm anybody.

Thanks a lot for your time

EDIT: I’m sorry, I should’ve posted both the user controller and model. I’ve been reading alot about how models should be fat, and controllers should be skinny. Thats why I created a register function in the model to validate the info instead of doing so within the controller. The register function takes an array, but turns that array into a validation object so that I can retrieve the user input, and the errors. I’ve seen a few tutorials on Kohana where it was done this way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:40:43+00:00Added an answer on May 13, 2026 at 4:40 pm

    First, I would not put the register() method into the User model. A model should be a representation of the object in the database and generally only contains your “CRUD” methods (create, retrieve, update, delete), getter and setter methods, and maybe some static helper methods related to the model. By putting your register() method into the model, you’re making the model do presentation logic that should really be done by a User controller, since this is a response to a user action. A controller handles user actions, validates those user actions, then updates the model if the validation is successful.

    In your example, the user is attempting to create a new account. He fills out a form and clicks submit. The form’s POST action should point to a controller’s method, like /user/register, and that method will use the Validation library to validate the form data sent by the user. Only if that data validates successfully should you create a User model instance, set the properties of that model to what the user input, and then use the model’s save() method to save to the database. If the validation fails, you report the error to the user and you don’t create a User model at all since you don’t have a valid data set to create a model with yet.

    Next, you are checking to see if the user is logged in. Again, this should be in the controller, not the model. Besides that, the user should not be able to get to this register process in the first place if he is already logged in. The controller method that creates the user registration form view should check to see if the user’s logged in, and if he is, then he should be redirected to another page. Even if the user is playing tricks and manages to submit the form (maybe he logged in via another window while having the form open in an old window), your register method should check for that first and not create a $user Validation object yet.

    I can see in your code that there are some confusing items based on your model set up. For example, you’re passing the $user array into the method, which I presume is the form data. But you’re using the “pass by reference” operator (&) which is unnecessary in PHP5 since all objects are now passed by reference. But after that you’re recasting $user as a Validation object. Are you using the $user Validation object elsewhere and require it to be passed by reference? If so, that’s another flaw in the logic as all of this processing needs to be in the controller and the $_POST values can be used directly in the controller instead of having to pass around a Validation object.

    Later on, you’re validating the user information with parent::validate($user, $save). Why is the validate() method being called on parent as a static method? If this is a model, it should be extending Kohana’s core Model class, and “parent” references the Model class. Is your model extending the Validation class? Also, why are you passing in the $user Validation object to the validation() method? Doing that is required if you need to do recursion (to validate elements again after making changes from previous filters), but it looks like you’re not doing anything to require recursion. You should be calling validate() on the $user Validation object:

    $user->validate();
    

    without any arguments. The validation errors will become part of the $user object, so you can check for errors using

    $user->errors();
    

    Finally, while Kohana allows you to use method chaining, I would not use one long chain to set up the rules and other items for the validation. It’s confusing and may cause debugging to be difficult. Put each of those on its own line and perform each directly on the $user object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.