5

I found CodeIgniter form validation to show error message with load->view method, and will lost field error message if use "redirect".

Currently I use one function to show form page, and another function to deal form post.

class Users extends CI_Controller {
  function __construct() {
      parent::__construct();
  }

  public function sign_up()
  {
    $this->load->view('users/sign_up');
  }

public function do_sign_up(){
      $this->form_validation->set_rules('user_login', 'User Name', 'trim|required|is_unique[users.login]');
      $this->form_validation->set_rules('user_email', 'Email', 'trim|required|valid_email|is_unique[users.email]');

      if ($this->form_validation->run() == FALSE) {
          $this->load->view('users/sign_up');
      }else {
       // save post user data to users table
       redirect_to("users/sign_in");
}


When form validation failed, url in browser will changed to "/users/do_sign_up", I want to keep same url in sign_up page.

Use redirect("users/sign_up") method in form validation failed will keep same url, but validation error message will lost.

in Rails, I cant use routes to config like this:

get "users/sign_up"       => "users#signup"
post "users/sign_up"       => "users#do_signup"

3 Answers 3

6

imho it's not necessary to check the request method because if the user 'GET' to the page you want to show the sign up view... if they user 'POST' to the page and fails validation you ALSO want to show the sign up view. You only won't want to show the sign up view when the user 'POST' to the page and passes validation.

imho here's the most elegant way to do it in CodeIgniter:

public function sign_up()
{
    // Setup form validation
    $this->form_validation->set_rules(array(
        //...do stuff...
    ));

    // Run form validation
    if ($this->form_validation->run()) 
    {
        //...do stuff...
        redirect('');
    }

    // Load view
    $this->load->view('sign_up');
}

Btw this is what im doing inside my config/routes.php to make my CI become RoR-like. Remember that your routes.php is just a normal php file so u can put a switch to generate different routes depending on the request method.

switch ($_SERVER['REQUEST_METHOD'])
{
    case 'GET':
        $route['users/sign_up'] = "users/signup";
    break;
    case 'POST':
        $route['users/sign_up'] = "users/do_signup";
    break;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here is my approach in CodeIgniter 4. I think you only need one method to complete the task.

In your app/Config/Routes.php

/*
 * --------------------------------------------------------------------
 * Route For Sign up page
 * --------------------------------------------------------------------
 */
$routes->match(['get','post'], 'signup', 'Users::Signup');

In your app/Views/signup.php

<?php  print form_open('/signup', ['method' => 'POST']);?> 

 <!--All other inputs go here, for example-->

<input type="text" name="firstname">

<?php  print form_close();?> 

In your app/Controllers/Users.php

namespace App\Controllers

use App\Controllers\BaseController;

class Users extends BaseController
{

public function Signup(){
    helper(['form', 'url']);
//run validations here
if ($this->request->getMethod() === 'post' && $this->validate([

   'firstname' => [
            'label'  => 'Firstname',
            'rules'  => 'required|alpha_space', 
            'errors' => [
             'required' =>'Please enter your <strong>Firstname</strong> e.g.John',
             'alpha_space' => 'Only alphabetic characters or spaces for <strong>Firstname</strong> field'
            ]    
        ],
])){
//do other stuff here such as save data to database
 $first_name=$this->request->getPost('firstname');
//if all go well here you can redirect to a favorite page
//e.g /success page
return redirect()->to('/success');

}
//if is get or post
print view('signup');
}

}

Comments

-2
<button type="submit"class="md-btn btn-sm md-fab m-b-sm indigo" id="filterbtn" formaction="<?php echo base_url(); ?>front/get_filter/<?php echo$device_id;?>"><i class="fa fa-bar-chart"></i></button>
<button type="submit"class="md-btn btn-sm md-fab m-b-sm indigo" id="filterbtn" formaction="<?php echo base_url(); ?>front/get_data/<?php echo$device_id;?>"><i class="fa  fa-th-list"></i></button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.