Welcome Guest, Not a member yet? Register   Sign In
Validation problem for form_upload
#1

[eluser]cinewbie81[/eluser]
Hi guys,

I have this on my form:
Code:
form_upload('csvFile', 'csvFile')

and here's my form validation
Code:
$rules['csvFile'] = "required";
$this->validation->set_rules($rules);

$fields['csvFile'] = 'CSV file';
$this->validation->set_fields($fields);    

if ($this->validation->run() == FALSE)
{
    // Form Validation Error
} else {
    // Form Validation Success
}

csvFile's rules has been set to 'required' because i want to make sure that users actually browse for a file.

Anyway, the validation error : "The CSV files field must have a value" is always display even though i have already browse for the file. Anyone ? Thanks in advance and Happy New Year 2008.
#2

[eluser]Sawariya[/eluser]
if (!$this->upload->do_upload('csvFile'))
{
return $this->upload->display_errors();
}
#3

[eluser]xwero[/eluser]
As Sawariya points out the upload validation is not present in the validation library. This is because the validation library only searches in the $_POST global where the upload is located in the $_FILES global.
#4

[eluser]cinewbie81[/eluser]
Let's say i have a form_input and a form_upload component . How can I do the verification for both component together ?? My code again:

Code:
form_upload('csvFile', 'csvFile')
form_input('name', 'name')

Code:
$rules['csvFile'] = "required";
$rules['name'] = "required";
$this->validation->set_rules($rules);

$fields['csvFile'] = 'CSV file';
$fields['name'] = "Name";
$this->validation->set_fields($fields);    

if ($this->validation->run() == FALSE)
{
    // Form Validation Error
} else {
    // Form Validation Success
}
#5

[eluser]xwero[/eluser]
Code:
// checking for errors + building error string
$errorstring = false;
if (!$this->upload->do_upload(’csvFile’))
{
$errorstring = $this->upload->display_errors();
}
if ($this->validation->run() === FALSE)
{
    $errorstring .= $this->validation->error_string;
}
// take appropriate action
if($errorsting)
{
   // errors action
}
else
{
  // success action
}
#6

[eluser]cinewbie81[/eluser]
Hi, the code died at this statement:
Code:
if (!$this->upload->do_upload(’csvFile’))

Error:
Severity: Notice
Message: Undefined property: Student::$upload

Any idea ?
#7

[eluser]Sawariya[/eluser]
you have to call this library

$this->load->library('upload', $config);
#8

[eluser]Sawariya[/eluser]
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';

$this->load->library('upload', $config);

if ( ! $this->upload->do_upload())
{
$arr= array('error' => $this->upload->display_errors());

$this->load->view('upload_form', $arr);
}
else
{
$data = array('upload_data' => $this->upload->data());

$this->load->view('upload_success', $data);
}
}




Theme © iAndrew 2016 - Forum software by © MyBB