0

I have created an Add User form in codeignitor which has the link http://example.com/users/add. And in the controller I have the following code for form validation:

$this->form_validation->set_rules('firstName', 'First Name', 'required');
$this->form_validation->set_rules('lastName', 'Last Name', '');
$this->form_validation->set_rules('guardian1', 'Guardian First', 'required');
$this->form_validation->set_rules('guardian2', 'Guardian Second', '');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required');
$this->form_validation->set_rules('phone', 'Phone Number', '');
$this->form_validation->set_rules('dob', 'Date Of Birth', 'required');
$this->form_validation->set_rules('status', 'Account Status', 'required');
$this->form_validation->set_rules('confirmation', 'Confirm Creation', 'required');

if ($this->form_validation->run == FALSE)
{
  redirect('news');
}
else
{
  redirect('users/view');
}

When I try to access the url to add user, it directly redirects me to http://example.com/news without opening the form page where I can fill the form. This is my form code for Add User form:

<form id="form-project" role="form" method="post" action="<?php echo base_url(); ?>users/add" autocomplete="off" novalidate>
    <div class="form-group-attached">
    <div class="row clearfix">
        <div class="col-md-6">
          <div class="form-group form-group-default required">
            <label>First name</label>
            <input type="text" class="form-control" name="firstName" required>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group form-group-default">
            <label>Last name</label>
            <input type="text" class="form-control" name="lastName">
          </div>
        </div>
      </div>
      <div class="row clearfix">
        <div class="col-md-6">
          <div class="form-group form-group-default required">
            <label>Parent I / Guardian</label>
            <input type="text" class="form-control" name="guardian1" required>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group form-group-default">
            <label>Parent II / Guardian</label>
            <input type="text" class="form-control" name="guardian2">
          </div>
        </div>
      </div>
      <div class="row clearfix">
        <div class="col-md-6">
          <div class="form-group form-group-default required">
            <label>Mobile</label>
            <input type="text" class="form-control" name="mobile" required>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group form-group-default">
            <label>Phone</label>
            <input type="text" class="form-control" name="phone">
          </div>
        </div>
      </div>
      <div class="row clearfix">
        <div class="col-md-6">
          <div class="form-group form-group-default input-group required">
            <div class="form-input-group">
              <label>Date Of Birth</label>
              <input id="start-date" type="text" class="form-control date" name="dob" required>
            </div>
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group form-group-default input-group">
            <div class="form-input-group">
              <label class="inline">Account Status</label>
            </div>
            <div class="input-group-addon bg-transparent h-c-50">
              <input type="checkbox" name="status" data-init-plugin="switchery" data-size="small" data-color="primary" checked="checked" required />
            </div>
          </div>
        </div>
      </div>
    </div>
    <br>
    <div class="pull-left">
      <div class="checkbox check-primary">
        <input type="checkbox" checked="unchecked" value="1" id="checkbox-agree" name="confirmation">
        <label for="checkbox-agree">I hereby certify that the information above is true and accurate
        </label>
      </div>
    </div>
    <br>
    <button class="btn btn-primary" type="submit">Submit</button>
    <button class="btn btn-default" type="reset"><i class="pg-close"></i>Clear</button>
  </form>

P.s I have the form validation library and url helper loaded.

Tauseef Shah
  • 115
  • 1
  • 1
  • 11

2 Answers2

0

You need to change you if condition:

if ($this->form_validation->run == FALSE)

to

if ($this->form_validation->run != FALSE)

Then, If validation is failed, user will be redirect back to "users/view" and redirect to "news" if passed.

Van Tho
  • 618
  • 7
  • 20
  • I want it to load "users/add" first where I can enter that data in the form before redirecting. It is redirecting automatically to "news" whenever I try to access "users/add" without letting me insert data in the form. – Tauseef Shah Nov 15 '18 at 07:02
  • If so, you need two functions, not just one. First function will only show the form, and the second function will handle the data from html form (you need to put your validator in the second function). – Van Tho Nov 15 '18 at 07:14
  • Thanks!! It worked but is there any way to use a single function for this? (I have seen some applications use single function for handling this) – Tauseef Shah Nov 15 '18 at 07:40
  • How about checking request method? [check this link](https://stackoverflow.com/questions/11189969/how-to-detect-http-method-in-codeigniter). If validation is FALSE or requested method is GET, show the form. If validation is FALSE and method is POST, redirect to `news` – Van Tho Nov 15 '18 at 07:54
  • I switched to using post request method and now I can use form_validation along with route in a single function. Thanks!!! – Tauseef Shah Nov 15 '18 at 08:20
0

If you want to redirect back to your form then, your redirection statement should be in if block body like this

if($this->form_validation->run == FALSE)
{
  redirect('users/view');
}
else
{
  redirect('news');
}