Jenz
Jenz

Reputation: 8369

Setting uploaded video's value in form in codeigniter

I am trying to upload a video file in my project. I have included form validation in my form and video field have required validation. The problem is, even if I have given all values, the form is not getting submitted. It is showing error as The Video field is required (form validation error).

Controller function - index():

function index()
    {           
        $this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean|max_length[255]');           
        $this->form_validation->set_rules('video_type', 'Video genre', 'required');         
        $this->form_validation->set_rules('language', 'Language', 'required');      
        $this->form_validation->set_rules('length', 'Length', 'required|is_numeric');           
        $this->form_validation->set_rules('tralier', 'Tralier', 'is_numeric');          
        $this->form_validation->set_rules('link', 'Link', 'max_length[255]');           
        $this->form_validation->set_rules('thumb_image', 'Thumb Image', 'required|max_length[255]');            
        $this->form_validation->set_rules('video', 'Video', 'required');            
        $this->form_validation->set_rules('description', 'Description', 'required|xss_clean|max_length[500]');          
        $this->form_validation->set_rules('keywords', 'Keywords', '');
        $this->form_validation->set_rules('accept_terms', '...', 'callback_accept_terms');
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');



        $genre=$this->db->query("select id,genre from genre where status=1 order by genre asc");
        $data['genre']=$genre->result();

        $language=$this->db->query("select id,language from movie_languages where status=1 order by language asc");
        $data['language']=$language->result();

        if ($this->form_validation->run() == FALSE) // validation hasn't been passed
        {
            $this->load->view('video/upload',$data);
        }
        else // passed validation proceed to post success logic
        {
            $form_data = array(
                            'title' => set_value('name'),
                            'genre' => set_value('video_type'),
                            'language' => set_value('language'),
                            'length' => set_value('length'),
                            'associatedvideo' => set_value('trailer'),
                            'videolink' => set_value('video'),
                            'videothumbnail' => set_value('thumb_image'),
                            'uploaderid' => set_value('1'),
                            'description' => set_value('description'),
                            'keywords' => set_value('keywords')
                        );

            // run insert model to write data to db
        echo $this->Video_model->SaveForm($form_data);
            if ($this->Video_model->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
            {
                redirect('video/success');   // or whatever logic needs to occur
            }
            else
            {
                echo 'An error occurred saving your information. Please try again later';
            // Or whatever error handling is necessary
            }
        }
    }

View file - upload.php

<?php // Change the css classes to suit your needs    
$this->load->view('layout/header');

$attributes = array('class' => '', 'id' => '');
echo form_open_multipart('video', $attributes); ?>
<table class="uploadtab">
    <tr>
        <td>
            <label for="name">Name <span class="required">*</span></label>

        </td>
        <td>
            <input id="name" type="text" name="name" maxlength="255" class="input" value="<?php echo set_value('name'); ?>"  />
        </td>
        <td>
        <?php echo form_error('name'); ?>
        </td>
    </tr>
    <tr>
        <td>
            <label for="video_type">Video Genre <span class="required">*</span></label>

        </td>
        <td>
            <?php 
                $array['']="Select";
                foreach($genre as $row )
                {
                    $array[$row->id] = $row->genre;
                }
                echo form_dropdown('video_type',$array, set_value('video_type'));
            ?>
        </td>
        <td>
            <?php echo form_error('video_type'); ?>
        </td>
    </tr>
    <tr>                                                             
        <td>
            <label for="category">Video Language <span class="required">*</span></label>
        </td>
        <td>
            <?php 
                $langarray['']="Select";
                foreach($language as $row )
                {
                    $langarray[$row->id] = $row->language;
                }
                echo form_dropdown('language',$langarray, set_value('language'));
            ?>
        </td>
        <td>
            <?php echo form_error('category'); ?>
        </td>
    </tr>
    <tr>
        <td>                                            
            <label for="length">Movie Length (in minutes)<span class="required">*</span></label>
        </td>
        <td>
            <input id="length" type="text" name="length"  class="input"  value="<?php echo set_value('length'); ?>"  />
        </td>
        <td>
            <?php echo form_error('length'); ?>
        </td>
    </tr>
    <tr>
        <td>                                            
            <label for="tralier">Tralier</label>
        </td>
        <td>
        <?php echo form_radio('trailer', '1', TRUE);?>Yes
     <?php    echo form_radio('trailer', '0');?>No

        </td>
        <td>
            <?php echo form_error('trailer'); ?>
        </td>
    </tr>

    <tr>
        <td>
            <label for="uploader_id">Upload Video <span class="required">*</span></label>
       </td>
       <td>
            <input type="file" name="video"  class="input" value="<?php echo set_value('video'); ?>" />
        </td>
        <td>
            <?php echo form_error('video'); ?>
        </td>
    </tr>


    <tr>
        <td>
            <label for="link">Link</label>
        </td>
        <td>
            <input id="link" type="text" name="link" maxlength="255" class="input"  value="<?php echo set_value('link'); ?>"  />
        </td>
        <td>
            <?php echo form_error('link'); ?>
        </td>
    </tr>
    <tr>
        <td>
            <label for="thumb_image">Thumb Image <span class="required">*</span></label>
        </td>
        <td>
            <input id="thumb_image" type="text" name="thumb_image" class="input"  maxlength="255" value="<?php echo set_value('thumb_image'); ?>"  />
        </td>
        <td>
            <?php echo form_error('thumb_image'); ?>
        </td>
    </tr>

    <tr>
        <td>
            <label for="description">Description <span class="required">*</span></label>
        </td>
        <td>                    
            <?php echo form_textarea( array( 'name' => 'description', 'rows' => '8', 'cols' => '30', 'class'=>'textarea','value' => set_value('description') ) )?>
        </td>
        <td>
            <?php echo form_error('description'); ?>
        </td>
    </tr>
    <tr>
        <td>
            <label for="keywords">Keywords</label>
       </td>
       <td>
            <input id="keywords" type="text" name="keywords"  class="input"  value="<?php echo set_value('keywords'); ?>"  />
        </td>
        <td>
            <?php echo form_error('keywords'); ?>
        </td>
    </tr>

<tr>
<td colspan="3">
<?php echo form_error('accept_terms'); ?>
    <label class="checkbox"><input type="checkbox" name="accept_terms"><i></i>I agree to the Terms of Service</label>
</td>
</tr>

<tr>
<td>
        <?php echo form_submit( 'submit', 'Submit'); ?>
</td>
</tr>
</table>
<?php echo form_close();

$this->load->view('layout/footer');?>

Can anybody help me to find the problem why the form is not getting submitted. I am not getting any filetype/size error.

Thanks in advance.

Upvotes: 1

Views: 691

Answers (1)

Kumar V
Kumar V

Reputation: 8830

Change

 $this->form_validation->set_rules('video', 'Video', 'required');    

to

if (empty($_FILES['video']['name']))
{
    $this->form_validation->set_rules('video', 'Video', 'required');
}

Upvotes: 1

Related Questions