Re: QuickForm file.php

From: Date: Mon, 29 Apr 2002 10:02:04 +0000
Subject: Re: QuickForm file.php
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-1300@lists.php.net to get a copy of this message
This is a message I received privately. I post it here because it could help others. le 29/04/02 10:49, Iwan Birrer à ibirrer@hsr.ch a écrit : > Hello Bertrand, > > I am a bit late but I hope you can help me on the file Upload problem > again... > > Here is the code I use to display and process the form: > > require_once ("HTML/QuickForm.php"); > $form = new HTML_QuickForm('frmTest'); That's ok. Use single quotes when possible. > if ($REQUEST_METHOD == "POST") { > $form->setDefaults(); > } There is no need for that. You don't want to set default values if the form is posted. You only set default values when the form is displayed for the first time. Also, there is no need to set default values if they are empty. setDefaults() is made to fill a field with default values when it is displayed. > $form->addElement('text', 'date', '','', > 'Date'); Use : $form->addElement('text', 'date', 'Date:'); You are wrong in the parameters order. addElement is supposed to be used this way: addElement( element type, element name, element label ); You can have more parameters. It depends on the type of element you use. I will have to write a documentation for each element one day. For instance, for a textfield, you can have: Use : $form->addElement('text', 'date', 'Date:', 'size=12 maxlength=8'); This will had HTML attributes to your element. > $form->addElement('file', 'fileUpload', '','', > 'File:'); Same here, use: $form->addElement('file', 'fileUpload', 'File:'); > $form->addElement('submit', '', 'Submit'); $form->addElement('submit', 'submit', 'Submit'); is better. ^ type ^ name ^ label > $form->addRule('date', 'Please type in a date', 'required'); This is fine. You could also do: $form->registerRule('dateRule','regex','^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$'); and then, $form->addRule('date','Your date is not correct','dateRule','', 'client'); Adding client at the end create the necessary javascript for client-side validation. Server side validation is always enabled in case users disable javascript on their browsers. registerRule is very helpful if you want to create your own validation function. For instance, you could create a function which check in the database if the username the user chose is already taken and return true if the username is free or false if it's taken. Then QuickForm would call this function at validation time (server-side of course) and display the error message if the function return false. $form->registerRule('checkName','function','checkName'); function checkName($element, $value) { // code for username validation } and then, $form->addRule('username','Name already taken','checkName','function'); In the case of a regular expression you are not forced to register the rule first. You could directly write : $form->addRule('date','Your date is not correct','regex','^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$', 'client'); Remember that addRule takes its parameters this way: addRule($element, $message="", $type="", $format="", $validation="server"); and if type is 'function', the rule will need to be registered first. > if ($form->validate()) { > $form->moveUploadedFile('fileUpload', "c:\\tmp" ); > echo( "Date: " . $form->_submitValues['date'] ); > echo( "File: " . $form->_submitFiles['fileUpload']['name'] ); > $form->process(); > } else { > $form->display(); > } I guess the rest should work fine. Please tell me if it works so I can make sure everything is fine. Thanks. Refer to this example if you want to know more : http://cvs.php.net/co.php/pear/HTML_QuickForm/docs/QuickForm_example.php Bertrand Mansion Mamasam > If I load the php site first I get the following warnings: > > Warning: Undefined offset: 2 in c:\php4\pear\HTML\QuickForm\element.php on > line 334 > Warning: Undefined offset: 3 in c:\php4\pear\HTML\QuickForm\element.php on > line 334 > Warning: Undefined variable: args in c:\php4\pear\HTML\QuickForm\file.php on > line 129 > Warning: Undefined variable: args in c:\php4\pear\HTML\QuickForm\file.php on > line 129 > Warning: Undefined variable: args in c:\php4\pear\HTML\QuickForm\file.php on > line 129 > Warning: Undefined offset: 2 in c:\php4\pear\HTML\QuickForm\element.php on > line 334 > Warning: Undefined offset: 3 in c:\php4\pear\HTML\QuickForm\element.php on > line 334 > Warning: Undefined variable: test in c:\php4\pear\HTML\QuickForm.php on line > 999 > > > after submitting a date and a fiel I get the following output: > > Warning: Undefined offset: 2 in c:\php4\pear\HTML\QuickForm\element.php on > line 334 > Warning: Undefined offset: 3 in c:\php4\pear\HTML\QuickForm\element.php on > line 334 > Warning: Undefined variable: args in c:\php4\pear\HTML\QuickForm\file.php on > line 129 > Warning: Undefined variable: args in c:\php4\pear\HTML\QuickForm\file.php on > line 129 > Warning: Undefined variable: args in c:\php4\pear\HTML\QuickForm\file.php on > line 129 > Warning: Undefined offset: 2 in c:\php4\pear\HTML\QuickForm\element.php on > line 334 > Warning: Undefined offset: 3 in c:\php4\pear\HTML\QuickForm\element.php on > line 334 > Warning: Undefined index: fileUpload in c:\php4\pear\HTML\QuickForm.php on > line 421 > Warning: Unable to open '' for reading: Permission denied in > c:\php4\pear\HTML\QuickForm.php on line 425 > Date: 29.04.2002 > Warning: Undefined index: fileUpload in > c:\webroot\brainfood\web\uploadtest.php on line 17 > File: > > array(2) { > ["date"]=> > string(10) "29.04.2002" > ["MAX_FILE_SIZE"]=> > string(0) "" > } > > array(0) { > } > > > The other error I mentioned is fixed. But I still can't upload a file. > I use PHP Version 4.1.2 on a windows platform. > > Thanks for your answer > > Iwan Birrer > > > > ----- Original Message ----- > From: "Bertrand Mansion" <bmansion@mamasam.com> > To: "Iwan Birrer" <ibirrer@hsr.ch> > Sent: Wednesday, April 24, 2002 7:55 PM > Subject: Re: QuickForm file.php > > > le 24/04/02 18:37, Iwan Birrer à ibirrer@hsr.ch a écrit : > >> Hello again, >> >> Thanks a lot for the fast answer. Unfortunately it isn't working yet (As > far >> as I use it right..). >> The array _submitFiles seems to be empty after submitting. >> >> array(0) { >> } >> > Iwan, > > Show me the code you use for your form. Especially the file part. > Which version of PHP do you use ? > Do you still have the same error : > >> Fatal error: Call to a member function on a non-object in >> c:\php4\pear\HTML\QuickForm\file.php on line 121 > > Or is this fixed ? > > Bertrand >

« previous php.pear.general (#1300) next »