class InterJinnModulesExamplesFormExample extends JinnBaseComponent
{
var $filename = __FILE__;
//
// Set the default property path.
//
var $propertyPath = 'InterJinn/examples';
function execute()
{
//
// See comment for the function at the bottom of page.
//
$this->checkFormSubmitted( 'userInfo' );
}
function render( $params=null )
{
//
// Get the current URL so demo submits back to itself.
//
$urlManager = &$this->getService( 'urlManager' );
$url = &$urlManager->do->getCurrent();
//
// Obtain the FormJinn service.
//
$formJinn = $this->getService( 'formJinn' );
//
// Create a form from the FormJinn service and set
// the actionURL.
//
$userInfoForm =
$formJinn->do->createForm( 'userInfo' );
$userInfoForm->do->setData(
'actionURL', $url->do->getNice() );
//
// Create some fields: name, phone, email, and submit.
//
$nameField =
$userInfoForm->do->createField( 'name', 'input' );
$phoneField =
$userInfoForm->do->createField( 'phone', 'input' );
$emailField =
$userInfoForm->do->createField( 'email', 'input' );
$submitField =
$userInfoForm->do->createField( 'submit', 'submit' );
//
// Set a default value for the phone field.
//
$phoneField->do->setValue( '(xxx) xxx-xxxx' );
//
// Obtain the WidgetJinn service for creating field widgets
// for displaying form fields.
//
$widgetJinn = $this->getService( 'widgetJinn' );
//
// Create a widget for each field.
//
$nameWidget =
$widgetJinn->do->getWidget( $nameField );
$phoneWidget =
$widgetJinn->do->getWidget( $phoneField );
$emailWidget =
$widgetJinn->do->getWidget( $emailField );
$submitWidget =
$widgetJinn->do->getWidget( $submitField );
//
// Set up some fonts (note that these could have been
// better declared as font properties at a low level in
// the property hierarchy for global usage.
//
$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' );
//
// Configure each of the widgets.
//
$nameWidget->do->setAttribute( 'size', 20 );
$nameWidget->do->setFont( $normalFont );
$nameWidget->do->setErrorFont( $errorFont );
$phoneWidget->do->setAttribute( 'size', 20 );
$phoneWidget->do->setFont( $normalFont );
$phoneWidget->do->setErrorFont( $errorFont );
$emailWidget->do->setAttribute( 'size', 20 );
$emailWidget->do->setFont( $normalFont );
$emailWidget->do->setErrorFont( $errorFont );
$submitWidget->do->setAttribute( 'value', 'Submit Form' );
$submitWidget->do->setFont( $normalFont );
//
// Display the form.
//
echo '<a name="formJinnAnchor_'
.$userInfoForm->do->getData( 'name' ).'"></a>';
$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 />';
$submitWidget->do->paint();
$userInfoForm->do->close();
//
// Add processors for validation.
//
$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 );
$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 );
}
function checkFormSubmitted( $name )
{
//
// Little bit of code for making the page jump to the example
// form when it has been submitted. Note that this makes use
// of accumulators which are extremely handy for adding content
// in arbitrary locations of a page's content -- also note
// that this function MUST be called from the execute method
// since it needs to place its content in the accumulator
// before the page begins rendering.
//
$formJinn = &$this->getService( 'formJinn' );
$aForm = $formJinn->do->getForm( $name );
if( is_null( $aForm )
||
!$aForm->do->getData( 'submitted' ) )
{
return;
}
$accManager = &$this->getService( 'accManager' );
$acc = $accManager->do->getAcc( 'javascriptOnLoad' );
$acc->do->append
(
' document.location = "#'
.'formJinnAnchor_'
.$aForm->do->getData( 'name' ).'";'."\n"
);
}
}