One of the most often used feature of any interactive web application is the form. Forms allow a visitor to input information and have it sent to the server for processing. There are many different ways to go about this process. You could directly compose the forms yourself via HTML which would provide simplistic functionality and subsequently you would need to carefully validate the information so that your database (where applicable) doesn't become corrupt. Alternatively you could use the FormJinn engine which provides a separation of data, layout, and display (albeit not yet integrated into TemplateJinn). This page will server to walk through the steps necessary to create a form using the FormJinn service.
The first thing to know is how forms are constructed.
there are 5 basic parts to any FormJinn form.
Although there are 5 parts, you will most likely only ever deal with 4 of
them. The InputProcessor engine is implicitly called on the field's data at
submission time. You will just declare types of processors to apply to the
data.
We will work through an example form which will request the user's name, phone number, and email address. The first step is to create a form. Creating a form is quite simple. You just request the Formjinn service and then you use it to create a form object (from within a component or service).
$formJinn = $this->getService( 'formJinn' ); $userInfoForm = $formJinn->do->createForm( 'userInfo' );
actionURL
field.
$userInfoForm->do->setData( 'actionURL', '//thankYou.php' );
Now that we've created a form which will keep track of the user's info fields, it is now time to add our first field -- the name field. Creating a field is almost as simple as creating the form itself. there are actually a couple of ways to go about creating a field and then subsequently the widget -- for now I will just describe the technique I have found simplest from experience.
$nameField = $userInfoForm->do->createField( 'name', 'input' );
$phoneField = $userInfoForm->do->createField( 'phone', 'input' ); $emailField = $userInfoForm->do->createField( 'email', 'input' ); $submitField = $userInfoForm->do->createField( 'submit', 'submit' );
$phoneField->do->setValue( '(xxx) xxx-xxxx' );
Now that you've created a form, and some simple fields, the next question you should be asking is "How do I display these to the user?" the answer to this is fairly simple -- you just create a field widget object which will be used to render the field. We obtain a widget in the following example:
$widgetJinn = $this->getService( 'widgetJinn' ); $nameWidget = $widgetJinn->do->getWidget( $nameField );
// // Alternative 1. // $nameField = $userInfoForm->do->createField( 'name' ); $nameField->do->setType( 'input' );
$widgetJinn = $this->getService( 'widgetJinn' ); $nameWidget = $widgetJinn->do->getWidget( $nameField );
// // Alternative 2. // $nameField = $userInfoForm->do->createField( 'name' ); $nameField->do->setType( 'input' );
$widgetJinn = $this->getService( 'widgetJinn' ); $nameWidget = $widgetJinn->do->getWidget( 'input' ); $nameWidget->do->setField( $nameObject );
$phoneWidget = $widgetJinn->do->getWidget( $phoneField ); $emailWidget = $widgetJinn->do->getWidget( $emailField ); $submitWidget = $widgetJinn->do->getWidget( $submitField );
$normalFont = $this->getService( 'htmlFont' ); $normalFont->do->setAttribute( 'face', 'verdana, arial' ); $normalFont->do->setAttribute( 'size', '-1' ); $normalFont->do->setAttribute( 'color', '#000000' );
$errorFont = $this->getService( 'htmlFont' ); $errorFont->do->setAttribute( 'face', 'verdana, arial' ); $errorFont->do->setAttribute( 'size', '-1' ); $errorFont->do->setAttribute( 'color', '#aa0000' );
$smallErrorFont = $this->getService( 'htmlFont' ); $smallErrorFont->do->setAttribute( 'face', 'verdana, arial' ); $smallErrorFont->do->setAttribute( 'size', '-2' ); $smallErrorFont->do->setAttribute( 'color', '#aa0000' );
$nameWidget->do->setAttribute( 'size', 20 ); $nameWidget->do->setFont( $normalFont ); $nameWidget->do->setErrorFont( $normalFont );
$submitWidget->do->setAttribute( 'value', 'Submit Form' ); $submitWidget->do->setFont( $normalFont );
$userInfoForm->do->open();
$nameWidget->do->paintError( $smallErrorFont, '', '<br />' ); $nameWidget->do->paintText( 'Please input your name: <br />' ); $nameWidget->do->paint();
echo '<br /><br />';
$phoneWidget->do->paintError( $smallErrorFont, '', '<br />' ); $phoneWidget->do->paintText( 'Input your phone number: <br />' ); $phoneWidget->do->paint();
echo '<br /><br />';
$emailWidget->do->paintError( $smallErrorFont, '', '<br />' ); $emailWidget->do->paintText( 'Input your email address: <br />' ); $emailWidget->do->paint();
echo '<br /><br />';
$userInfoForm->do->close();
One of the claims of FormJinn is that it simplifies the task of validating user input. This is done by applying input processors to the field's data. When the field is submitted, its data will be passed to any registered processor. If any of these processors return false then the field will be considered invalid, and subsequently the form will also be considered invalid. To begin we will apply some processors to the name field.
$nameProcesses = array
(
array
(
'type' => 'mandatory',
'errorMessage' => 'You must input your name!',
),
array
(
'type' => 'regEx',
'expression' => '^[[:alpha:] ]*$',
'errorMessage' => 'You may only use letters and spaces!',
),
);
$nameField->do->setData( 'validation', $nameProcesses );
$smallErrorFont and the
input label will be displayed using the $errorFont instead of the $normalFont. Try
inputting data into the name field at the bottom of this page to see how this
works. Similarly we add validation for the phone number and for the email
fields.
$phoneProcesses = array
(
array
(
'type' => 'mandatory',
'errorMessage' => 'You must input your phone number!',
),
array
(
'type' => 'phone',
'errorMessage' => 'The input phone number is invalid!',
),
);
$phoneField->do->setData( 'validation', $phoneProcesses );
$emailProcesses = array
(
array
(
'type' => 'mandatory',
'errorMessage' => 'You must input your email address!',
),
array
(
'type' => 'email',
'errorMessage' => 'The input email address is invalid!',
),
);
$emailField->do->setData( 'validation', $emailProcesses );
By now you should have a fairly good idea of how the FormJinn system works. There's a lot more that hasn't been covered here, such as custom processors, preProcessing, which occurs before validation, and postProcessing, which occurs after validation. Also you can replace the regular submit button with an image submission, there are widgets for drop down select lists, uploading files, and everything else you usually do in forms (unless I've forgotten something :). You'll also notice that if you have cookies enabled, then when you got to other pages and return to the form your values will not have changed. This might pose a problem if you always want to reset the value of a field to some default, and so the following mechanism is possible:
$phoneField->do->setValue( '(xxx) xxx-xxxx', true );
Below is the form you should have been able to produce if you followed and understood the material in this page. If not then you can see the complete listing for the form here.