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 6606079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:21:28+00:00 2026-05-25T19:21:28+00:00

I just started using a MVC framework, especially Codeigniter and i am having some

  • 0

I just started using a MVC framework, especially Codeigniter and i am having some trouble maintaining my code and where to place my functions(controller or model).

For now i am building a sign up system and i have a controller with the name signup.php

This is my code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class Signup extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->form_validation->set_rules('username', 'Username', 'trim|required|callback_check_valid_username|min_length[6]|max_length[20]|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|max_length[32]');

        if ($this->form_validation->run() == false){
            $this->load->view("register/index");
        }else{
            $this->submitRegistration();
        }
    }

    public function ajaxup(){
        if ($this->input->isAjaxRequest()){
            header('Content-type: application/json');

            $error = false;
            $message = '';

            $this->form_validation->set_rules('username', 'Username', 'trim|required|callback_check_valid_username|min_length[6]|max_length[20]|xss_clean');
            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|max_length[32]');

            if ($this->form_validation->run() == false){
                $message = validation_errors();
                $error = true;
            }else{
                $this->_submitRegistration();
                $message = 'Successfully registered.';
            }

            $return = array(
                'error' => $error,
                'message' => $message
            );

            $return = json_encode($return);

            echo $return;

        }
    }

    public function _submitRegistration(){
        $username = $this->input->post('username');
        $email = $this->input->post('email');
        $password = $this->input->post('password');

        $data = array(
            'username' => $username,
            'email'    => $email,
            'password' => $password
        );

        $this->load->model('users_model');

        $this->users_model->register_user($data);
    }

    public function check_valid_username($username){

        $this->load->model('users_model');

        if (!$this->users_model->is_valid_username($username)){
            $this->form_validation->set_message('check_valid_username', 'The %s field should contain only letters, numbers or periods');
            return false;
        }

        return true;

    }

}

Is there anything i could write better to maintain my code and be readable?

*NOTE:*the function ajaxup is used when a user clicks the button and does an ajax call.

Thanks

  • 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-25T19:21:29+00:00Added an answer on May 25, 2026 at 7:21 pm

    Looks pretty good to me. Here are few ideas/suggestions for future improvements:

    • In index() you are calling $this->submitRegistration() but I think you want to be calling $this->_submitRegistration().

    • Since you are using the same validation rules in both the index() and ajaxup() methods you could pull pull them out into an array and either make them a property of your controller or put them into a config file.

    For documentation see here and here.

    $validation_rules = array(
        array(
            'field'   => 'username',
            'label'   => 'Username',
            'rules'   => 'trim|required|callback_check_valid_username|min_length[6]|max_length[20]|xss_clean'
        ),
        array(
            'field'   => 'email',
            'label'   => 'Email',
            'rules'   => 'trim|required|valid_email'
        ),
        array(
            'field'   => 'password',
            'label'   => 'Password',
            'rules'   => 'trim|required|min_length[6]|max_length[32]'
        ),
    );
    

    Then in your methods you would do something similar to $this->form_validation->set_rules($validation_rules).

    • Think about reordering your validation rules. For example, let’s take a look at the rules for the username field. If check_valid_username() is making a call to the database (through the user model) then it would probably be better to validate the length requirements before. There’s no use making an expensive call to the database if we can determine if the username is invalid.

    • Make your callback methods private. Right now check_valid_username() is a public method and could potentially be accessed through the URL. Prefix it with an underscore (_check_valid_username()) and then in your validation rules use callback__check_valid_username. Note the two underscores.

    • If you find yourself needing to use check_valid_username() in multiple controllers you could extend the native form validation library and put it there.

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

Sidebar

Related Questions

I am just getting started with Microsoft's Entity Framework, using it for an MVC
I've just started using the mvc-mini-profiler ( http://code.google.com/p/mvc-mini-profiler/ ) and I think it's awesome.
Just getting started using MVC in ASP.NET, I'm going to have it so users
I’ve only just started using ASP.NET MVC, and I have a somewhat trivial question:
I'm just getting started with ASP.NET MVC. I'm going to be using JQuery on
i am just getting started with ASP.NET MVC and i'm using NHibernate for my
I just started with ASP.NET MVC 1.0. I've read through some tutorials but I
I have just started using MVC 2 , I have got a couple of
I've just started out using ASP.NET MVC and TDD. I've read that while unit
I'm using a framework which uses the MVC paradigm. It's CodeIgniter, but my question

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.