On Tue, 2009-01-13 at 13:28 -0500, Jason Pruim wrote: > > > > > > Wow, that's really low level... I haven't written a <form> tag in > > years. > > So then the question is begged... How do you write your forms? :) If > there's a better way to do it I'm all ears! Other then using CSS... My > HTML knowledge came from the 90's! :P I use my FormJinn engine... here's a login form as an example: <?php function createForm() { $formJinn = &$this->getServiceRef( 'formJinn' ); $trans = &$this->getServiceRef( 'translation' ); ////////////////////////////////////////////////////////////////////// // // Some pre-handling. // ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// // // Set up data for options. // ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// // // Setup form criteria. // ////////////////////////////////////////////////////////////////////// $fields = array(); $values = array(); $preProcessors = array(); $validators = array(); $postProcessors = array(); $fields['username'] = 'text'; $fields['password'] = 'password'; $fields['redirect'] = 'hidden'; $fields['continue'] = 'submit'; ////////////////////////////////////////////////////////////////////// // // Preset values. // ////////////////////////////////////////////////////////////////////// $redirect = trim( (string)$this->getHttpGetVar( 'redirect' ) ); if( $redirect ) { $values['redirect'] = $redirect; } ////////////////////////////////////////////////////////////////////// // // Field widget attributes. // ////////////////////////////////////////////////////////////////////// $widgetAtts = array ( 'username' => array ( ), 'password' => array ( 'secure' => true, ), 'continue' => array ( 'label' => $trans->get( 'Login' ), ), ); ////////////////////////////////////////////////////////////////////// // // Widgets for which a breakdown of elements is desired. // ////////////////////////////////////////////////////////////////////// $widgetElements = array(); ////////////////////////////////////////////////////////////////////// // // Field value validators. // ////////////////////////////////////////////////////////////////////// $validators = array ( 'username' => array ( array ( 'type' => 'mandatory', 'errorMessage' => $trans->expand( '[!Required][!:] [!email address]' ), ), ), 'password' => array ( array ( 'type' => 'mandatory', 'errorMessage' => $trans->expand( '[!Required][!:] [!password]' ), ), ), ); ////////////////////////////////////////////////////////////////////// // // Form handlers. // ////////////////////////////////////////////////////////////////////// $processHandler = array ( array ( 'type' => 'custom', 'source' => 'MyProject/modules/auth/controller.php', 'method' => 'processLoginSubmission', ), ); ////////////////////////////////////////////////////////////////////// // // Generate the form using formJinn. // ////////////////////////////////////////////////////////////////////// $criteria = array ( 'formInfo' => array ( 'name' => $this->formName, 'actionUrl' => $this->actionUrl, 'finalize' => $processHandler, 'aggregateFields' => 'redirect', ), 'fieldInfo' => array ( 'fields' => $fields, 'values' => $values, 'clearValues' => true, 'preProcessors' => $preProcessors, 'validators' => $validators, 'postProcessors' => $postProcessors, 'widgetAtts' => $widgetAtts, 'widgetElements' => $widgetElements, ), ); $this->slices = $formJinn->generateFormSlices( $criteria ); if( !$formJinn->getFormData( $this->formName, 'valid' ) ) { $this->hasErrors = true; } } ?> Basically all my forms follow the same form creation pattern with just the fields and field related settings changing. Makes it very easy and the form generator creates appropriate CSS classes and field IDs for easy styling and scripting respectively. I'm not limited to standard HTML field types either. I can create custom form field types such as date, time, datetime, rich text (a la fckeditor), double pane selection, image upload that remembers if an image has been uploaded between submissions and shows preview, file upload, etc. Also, all the defaults for widget creation can be overriden. Some clients like DHTML date widgets instead of the plain vanilla drop down lists of days and months... in which case I override the date widget that's built into InterJinn and link it up to a 3rd party DHTML calendar widget. In my actuall template file I'll have something like follows: <jinn:module name="loginForm" render="false"> <jinn:component type="view" name="view" source="SecuritiesExpertPanel/modules/auth/loginForm.php"/> </jinn:module> {comment: ------------------------------------------------------------ // // Login form. // //--------------------------------------------------------------------} <jinn:render name="loginForm" selector="formOpen"/> <div class="formLayout"> <jinn:if test="{jinn:value xpath=module://loginForm/view/hasErrors}"> <div class="formLayout-errors"> <p> <project:trans phrase="Your submission had the following errors"/><project:trans phrase=":"/> </p> <jinn:render name="loginForm" selector="formError"/> <jinn:render name="loginForm" selector="username_error"/> <jinn:render name="loginForm" selector="password_error"/> </div> </jinn:if> <div class="formLayout-row"> <div class="formLayout-label"> <jinn:render name="loginForm" selector="username_textOpen"/> <project:trans phrase="Email"/><project:trans phrase=":"/> <project:mandatory/> <jinn:render name="loginForm" selector="username_textClose"/> </div> <div class="formLayout-input"> <jinn:render name="loginForm" selector="username_field"/> </div> </div> <div class="formLayout-row"> <div class="formLayout-label"> <jinn:render name="loginForm" selector="password_textOpen"/> <project:trans phrase="Password"/><project:trans phrase=":"/> <project:mandatory/> <jinn:render name="loginForm" selector="password_textClose"/> </div> <div class="formLayout-input"> <jinn:render name="loginForm" selector="password_field"/> </div> </div> <div class="formLayout-row"> <div class="formLayout-label"> </div> <div class="formLayout-input"> <jinn:render name="loginForm" selector="continue_field"/> </div> </div> </div> <jinn:render name="loginForm" selector="formClose"/> Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php