Form with validation

A simple form with validation checks.

<pinp>
$formFields = array(
'Name',
'Email' => array(
'required' => true,
'checks' => 'email'
),
'Password' => array(
'type' => 'password',
'required' => true
),
'agree' => array(
'type' => 'checkbox',
'required' => true,
'checkedValue' => 1,
'label' => 'I agree to the terms of service.'
)
);

$formButtons = array( 'Register', 'cancel' => 'Another time' );

$form = ar('html')->form( $formFields, $formButtons );

if ( $form->isSubmitted('button_0') )
{
if ( $form->isValid() )
{
echo 'Registration complete, thank you.';
}
else
{
$invalidFields = $form->validate();
$required = false;
$errors = array();
$formHTML = $form->getHTML();
foreach ( $invalidFields as $name => $error )
{
$code = $error->getCode();
switch ( $code )
{
case 'required' :
if ( !$required )
{
$required = true;
$errors[] = ar('html')->el( 'div',
'You must fill in the required fields.' );
}
break;

default :
$errors[] = ar('html')
->el( 'div', $error->getMessage() );
break;
}
$field = $formHTML->getElementById( $name );
if ($field)
{
$field->parentNode->setAttribute( 'class',
array( 'validation' => ' invalid' ) );
}
}
if ( count($errors) )
{
echo ar('html')
->el( 'div', array( 'class' => 'formError'),
  ar('html')->nodes( $errors ) );
}
echo $formHTML;
}
}
else if ($form->isSubmitted('cancel'))
{
echo 'Registration cancelled, see you later.';
}
 else
{
echo $form->getHTML();
}
</pinp>