karuna
karuna

Reputation: 59

form validation error is not displaying in codeigniter while redirecting to same page

My website have multiple pages and footer page is included to every page. I have a contact us form in footer of my website. When user fills that form with valid value it should send the mail and returns to the same page, but if user submit the form without filling any field the current page should be load with the error messages. I have auto load the form_validation, session library and form, url helper also.

This is my View:-

      <form action="<?php echo site_url('main/contactMail')?>"  method="post" class="wpcf7-form"  novalidate="novalidate">

          <input type="hidden" value="<?php echo current_url();?>" name="url">
           <label for="your-name"> Full Name</label> <input type="text" name="your-name" />
           <label for="your-email"> Email*</label> <input type="email" name="your-email" />
            <label for="phonenumber">Phone Number *</label> <input type="tel" name="phonenumber" />
            <label for="textarea"> Message </label><textarea name="textarea" cols="40" rows="2"> 
            </textarea></div>
          <input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit square-button material-btn" />


        <?php echo  validation_errors();?>
                 </form>

This is my controller:-

       $url = $this->input->post('url');

    $this->form_validation->set_rules('your-name','Name','required|alpha_numeric_spaces');
    $this->form_validation->set_rules('your-email','Email','required|valid_email');
    $this->form_validation->set_rules('phonenumber','Mobile','required|numeric');
    $this->form_validation->set_rules('textarea','Text', 'required');
    if($this->form_validation->run() == FALSE)
    {
       redirect($url);
    }

when user submit the form without filling the form current page is loading but errors are not displaying.

Upvotes: 0

Views: 756

Answers (2)

Ramki
Ramki

Reputation: 472

You need to Load library in your constructor :

 $this->load->library('form_validation');



$url = $this->input->post('url');
          $this->form_validation->set_rules('your-name','Name','required|alpha_numeric_spaces');
          $this->form_validation->set_rules('your-email','Email','required|valid_email');
          $this->form_validation->set_rules('phonenumber','Mobile','required|numeric');
          $this->form_validation->set_rules('textarea','Text', 'required');
          if($this->form_validation->run() == FALSE)
          {
             $this->session->set_flashdata('error', validation_errors());
             redirect($url);
          }

In your controller functions where you would like your errors or messages to be received you can then set them to the $data array like so:

if (!empty($this->session->flashdata('message'))) {

              $data['message'] = $this->session->flashdata('message');
          } elseif (!empty($this->session->flashdata('error'))) {

              $data['error'] = $this->session->flashdata('error');
          }

        <?php 
          if (isset($message)) {
              echo '<p class="alert alert-info">'.$message.'</p>';
          } elseif (isset($error)) {
              echo '<p class="alert alert-danger"><strong>Error: </strong>'.$error.'</p>';
          }
        ?>

Upvotes: 2

Frank Lexer
Frank Lexer

Reputation: 310

Put this on the constructor of your controller:

$this->load->library('form_validation');

On your view, write this at the top of your DOM or HTML elements:

<?php echo validation_errors(); ?>

Upvotes: 0

Related Questions