OK I want to simply validate my form. The problem is with Notexists validator. I write special filter which return correct value of filtered file name (I checked), so if filters work before validators why my form success validation in the example case.
Example:
I upload file Audyt dla strony http.doc and in my uploads is file audyt-dla-strony-http.doc My_Filters_Uploader returns: audyt-dla-strony-http.doc, so it seems that my form should fail validation. In fact it shouldnt. Please guys help me.
//my form file upload element
$kontrolka = new Zend_Form_Element_File('file_name', array(
'label' => 'Dodaj plik:',
'destination' => realpath(APPLICATION_PATH . '/../public/uploads'),
'required' => true,
'filters' => array(new My_Filters_Uploader()),
'validators' => array(
array('Count', true, 1),
array('Size', true, 102400),
array('Db_NoRecordExists', false, array('table' => 'e_exercise','field' => 'file_name')),
array('NotExists', false, realpath(APPLICATION_PATH . '/../public/uploads')),
array('Extension', false, array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'odt', 'pdf'))
),
));
//Filtr My_Filters_Uploader
public function filter($value)
{
$file = pathinfo($value);
$valueFiltered = My_Slugs::string2slug($file['filename']);
return $valueFiltered . '.' . $file['extension'];
}
//action in controller
if ($form->isValid($this->getRequest()->getPost())) {
//usuwanie poprzedniego pliku
$fld = realpath(APPLICATION_PATH . '/../public/uploads');
$plik = $DbTable->getFile($eid);
$np = realpath($fld . '/' . $plik);
if (file_exists($np) && is_file($np))
{
chmod($np, 0777);
unlink($np);
}
//usuwanie poprzedniego pliku
$data = $form->getValues();
$data = array_merge($data, array('edit_date' => date('Y-m-d')));
$obj->setFromArray($data);
$obj->save();
return $this->_helper->redirector(
'editexercise',
$this->getRequest()->getcontrollerName(),
$this->getRequest()->getModuleName(),
array('eid' => $eid,
'id' => $id)
);
}
//helpul string2slug function
public static function string2slug($string = '', $options = array('encoding' => 'windows-1250'))
{
if (!isset($options['separator'])) {
$options['separator'] = '-';
}
if (!isset($options['default'])) {
$options['default'] = 'undefined';
}
if (!isset($options['encoding'])) {
$options['encoding'] = 'utf-8';
}
if (!isset($options['case'])) {
$options['case'] = 'lower';
}
switch ($options['encoding']) {
case 'utf-8':
$string = My_Pl::utf82ascii($string);
break;
case 'iso-8859-2':
$string = My_Pl::iso2ascii($string);
break;
case 'windows-1250':
$string = My_Pl::win2ascii($string);
break;
}
$string = preg_replace('/[^A-Za-z0-9]/', $options['separator'], $string);
if (isset($options['case'])) {
if ($options['case'] == 'lower') {
$string = strtolower($string);
} else if ($options['case'] == 'upper') {
$string = strtoupper($string);
}
}
$string = preg_replace('/' . preg_quote($options['separator'], '/') . '{2,}/', $options['separator'], $string);
$string = trim($string, $options['separator']);
if (isset($options['maxlength'])) {
$string = self::abbr($string, $options);
}
if ($string === '') {
return $options['default'];
} else {
return $string;
}
}