Thursday, September 19, 2013

Form Validation

The validation of the forms in our site is done using the form validation of codeigniter.

<html>
<head>
<title>My Form</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?php echo form_open('form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" value="Submit" /></div>

</form>

</body>
</html>

The form can be accessed using the name tags of elements in the controller class.

<?php

class Form extends CI_Controller {

function index()
{
$this->load->helper(array('form', 'url'));

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

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');

if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}
?>

Codeigniter form validation and file upload

I had an issue in implementing form validation and file uploading for the paper submission, because once the file is uploaded, the page reloads and all data is lost. To overcome this I implemented form validation along with file uploading.
Whole page should consist a single form and form validation can be used to validate the data. Then the submit button of the form can be used as the file upload button. This prevents page from reloading.

Study more on this using the following links.
Form Validation
File Uploading

The following code contains a sample of implementing the above theory.

//form in view

<?php echo $formerror;?>
       <?php echo form_open_multipart(base_url('index.php/ongoing_conference/do_upload'));?>
             <input type="file" name="userfile" size="20" />
             <input type="text" id="TitleOfFile" name="TitleOfFile" value="" size="70">
             <input type="submit" value="upload" />
             <?php echo  $uploaderror;?>
       </form>

//controller method

controller


    function do_upload()
    {
        //echo 'true';

            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
            $this->form_validation->set_rules('TitleOfFile', 'File title', 'trim|required|min_length[5]|xss_clean');       
                   
            if ($this->form_validation->run() == FALSE)
            {
                //echo 'form validation false';
                $formerror="form validation false";

                $navtab='includes/navigation';
                $footer='footer';
                $home='Ongoing_conference';
                $login_view='includes/login_view';
                $registration_view='includes/registration_view';
                $paras = array('site_navigations' => $navtab, 'footer' => $footer, 'content' => $home, 'login_view'=>$login_view, 'registration_view'=>$registration_view, 'formerror' => $formerror, 'uploaderror' => $uploaderror);
                $this->load->view('includes/main',$paras);
            }
            else if($this->form_validation->run() == TRUE)
            {
                //echo 'form validation true';

                $config['upload_path'] = './assets/upload/materials/';
                $config['allowed_types'] = 'docx|pdf';
                $config['max_size'] = '2000';
                $config['file_name']  = "yas";
                $config['overwrite'] = TRUE;
                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload())
                {
                    $uploaderror = $this->upload->display_errors();

                    $navtab='includes/navigation';
                    $footer='footer';
                    $home='Ongoing_conference';
                    $login_view='includes/login_view';
                    $registration_view='includes/registration_view';
                    $paras = array('site_navigations' => $navtab, 'footer' => $footer, 'content' => $home, 'login_view'=>$login_view, 'registration_view'=>$registration_view, 'formerror' => $formerror, 'uploaderror' => $uploaderror);
                    $this->load->view('includes/main',$paras);
                    //echo $uploaderror;
                }
                else
                {
                   
                    $uploaderror='file is uploaded now with correct form validation !';
                    $navtab='includes/navigation';
                    $footer='footer';
                    $home='Ongoing_conference';
                    $login_view='includes/login_view';
                    $registration_view='includes/registration_view';
                    $paras = array('site_navigations' => $navtab, 'footer' => $footer, 'content' => $home, 'login_view'=>$login_view, 'registration_view'=>$registration_view, 'formerror' => $formerror, 'uploaderror' => $uploaderror);
                    $this->load->view('includes/main',$paras);
                }

            }
    }