Are you using a release version or the svn trunk or a svn branch ?
I've tested on SVN trunk r14554 and the related lines of code look similar
to the actual branch 1.7.
Greetings
Thomas Weidner, I18N Team Leader, Zend Framework
http://www.thomasweidner.com
----- Original Message -----
From: "Jacky Chen" <[email protected]>
To: <[email protected]>
Sent: Tuesday, March 31, 2009 4:44 PM
Subject: Re: [fw-general] about zend_form_element_file error messages
Sorry,Thomas,but it exactly not worked for me.I just copy and run the
example that you provided to me. But it just print the empty array when
uploaded an file that is not gif extension. Maybe the Zend Framework
version
you used not the same with i used. But i use the newest ZF that update
from
the svn.
just try the example that you provided,upload an file that not gif
extension,and see that if any error messages were displayed?
Forgive me the poor english. Thank you very much.
Best Regards,
Jacky
2009/3/31 Thomas Weidner <[email protected]>
Jacky,
2 things before:
Stop writing me privatly when we have a conversation over the mailing
list.
It's really annoying always to go into the spam filter to get your mail.
Future mails not coming through the mailing list will be ignored by me.
Second note... please stop just to say "doesn't work" without giving the
example code you've tried.
This is wether usefull nor shows it that you really want to be helped.
Now the answer:
What I've said before is true. It works like I've said. See the code with
which I've tested it:
-------------------------------------
<?php
error_reporting(E_ALL|E_STRICT);
ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.'../library');
require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();
$request = new Zend_Controller_Request_Http();
// setup the form
$form = new Zend_Form();
$form->setMethod(Zend_Form::METHOD_POST)
->setAttrib('enctype', Zend_Form::ENCTYPE_MULTIPART);
$form = new Zend_Form();
$element = new Zend_Form_Element_File('file');
$element->setRequired(true);
$element->addValidator('upload', false, array('messages' =>
array(Zend_Validate_File_Upload::FILE_NOT_FOUND => 'no file given')));
$element->addValidator('extension', true, array('gif', 'messages' =>
array(Zend_Validate_File_Extension::FALSE_EXTENSION => 'not a gif
image')));
$form->addElement($element);
$form->setAttrib('enctype', 'multipart/form-data');
$form->addElement('submit', 'Submit');
// check the form
if($request->isPost()) {
$formData = $request->getPost();
if($form->isValid($formData)) {
$form->file->receive();
} else {
print "\nVALIDATION FAILURES:";
print_r($form->file->getMessages());
}
}
print "</pre>";
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<?php echo $form->render(new Zend_View());?>
</body>
</html>
-----------------------------
and the output I am getting is
Array ( [fileUploadErrorFileNotFound] => no file given )
which clearly shows that the set message is really thrown out as
expected.
Once again... when you set the false constants nothing will be outputted
as
the validator accesses a different error message than you gave.
Greetings
Thomas Weidner, I18N Team Leader, Zend Framework
http://www.thomasweidner.com
----- Original Message ----- From: "Jacky Chen" <[email protected]>
To: "Thomas Weidner" <[email protected]>
Sent: Tuesday, March 31, 2009 9:10 AM
Subject: Re: [fw-general] about zend_form_element_file error messages
No,it not worked as you said, i got an empty array() from
$form->getMessages() when no file was uploaded. And the Zend Framework
is
the newest from the svn.
Best Regards,
Jacky
2009/3/31 Thomas Weidner <[email protected]>
Well, when you want to display a own error message, then you have to
set
the correct message constant.
NOT_FOUND is, as it's text says, for "file not found"...
FALSE_EXTENSION is as it's text says for "file has the false
extension...
So why should your text be displayed when you set the false constant ?
Giving "NOT_FOUND" will not be displayed when the extension is false.
I would simply set the correct message constant to get the wished
output.
Running your code I get eighter
"[fileUploadErrorNoFile] => The file 'file' was not uploaded " when no
file
was uploaded, or
"[fileUploadErrorNoFile] => The file 'file' was not uploaded " when a
false
extension was used.
And when I change your code from NOT_FOUND to FALSE_EXTENSION then I
get
"[fileExtensionFalse] => not a gif image " displayed when I do print_r
on
getMessages() from the form.
Greetings
Thomas Weidner, I18N Team Leader, Zend Framework
http://www.thomasweidner.com
----- Original Message ----- From: "Jacky Chen" <[email protected]>
To: "Thomas Weidner" <[email protected]>
Sent: Tuesday, March 31, 2009 4:42 AM
Subject: Re: [fw-general] about zend_form_element_file error messages
OK,i want to validate a form file element if an image with gif
extension,but
no error messages be displayed after the form validate failed.
just try this:
<?php
required_once 'Zend/Controller/Action.php';
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$form = new Zend_Form();
$form->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
$image = new Zend_Form_Element_File('image');
$image->setLabel('upload a gif image');
$image->setRequired(true);
$image->addValidator('extension',true,array('gif','messages'=>array(Zend_Validate_File_Extension::NOT_FOUND=>'not
a gif image')));
$form->addElement($image);
$form->addElement('submit','submit');
if ($this->_request->isPost() &&
$form->isValid($this->_request->getPost()) {
echo 'validate success';
}
$this->view->form = $form;
}
}
?>
views/script/index/index.phtml
<?php
echo $this->form;
?>
run this page,then upload a file that is not gif extension,the form
would
validate failed.But the error message of 'not a gif image' not
displayed
bellow to the form file element.
Best Regards,
Jacky
2009/3/31 Thomas Weidner <[email protected]>
And why should they be assigned by isValid() ? No one said that.
Instead of thinking that the implementation does not work it would be
better if you say where you have a problem.
It makes no sense to discuss ZF internal code which works and is
tested
;-).
Greetings
Thomas Weidner, I18N Team Leader, Zend Framework
http://www.thomasweidner.com
----- Original Message ----- From: "Jacky Chen" <[email protected]>
To: "Thomas Weidner" <[email protected]>
Sent: Monday, March 30, 2009 7:32 PM
Subject: Re: [fw-general] about zend_form_element_file error messages
but in the isValid() method of the form file element,no error
messages
were
assigned to the form by file adapter.
Best Regards,
Jacky
2009/3/30 Thomas Weidner <[email protected]>
Error messages from the form are not assigned to the file adapter.
It is the other way round... error messages from the file adapter
are
assigned to the form.
Greetings
Thomas Weidner, I18N Team Leader, Zend Framework
http://www.thomasweidner.com
----- Original Message ----- From: "Jacky Chen"
<[email protected]>
To: <[email protected]>
Sent: Monday, March 30, 2009 12:36 PM
Subject: [fw-general] about zend_form_element_file error messages
Hi,
is it not implement the error messages process for the form file
element
just now,or missing that? no error messages are assign to the form
file
element after the file adapter validated the file element.
Best Regards,
Jacky