Einfaches Formular

Anwendungsbeispiel: Einfaches Formular mit Validierung

Kontaktformular

Beispiel: Minimales Beispielformular mit Validierung

Quelltext

<?php

$formular
= new wForm(array('decorators' => array('form' => '<dl class="formTable">')));
$formular->resetIfHasBeenCalledWithStatus();

$formular->addElement('input', 'name', 'Name*', array('validators' => array(array('type' => 'required', 'errorMessage' => 'Bitte geben Sie einen Namen an'))));
// wenn das Feld vorbelegt werden soll (Initialwert):
//$formular->addElement('input', 'name', 'Name*', array('value' => 'Ihr Name', 'validators' => array(array('type' => 'required', 'errorMessage' => 'Bitte geben Sie einen Namen an'))));
$formular->addElement('input', 'email', 'E-Mail*', array('validators' => array(array('type' => 'email', 'errorMessage' => 'Bitte geben Sie eine gültige E-Mail Adresse an'))));
$formular->addElement('select', 'type', 'Typ*', array('options' => array('' => 'Bitte auswählen', 'customer' => 'Kunde', 'prospect' => 'Interessent'), 'validators' => array(array('type' => 'required', 'errorMessage' => 'Bitte wählen Sie einen Typ aus.'))));
$formular->addElement('textarea', 'text', 'Text');
$formular->addElement('captcha', 'captcha', 'Bitte geben Sie diesen Text in das Feld ein', array('validators' => array(array('type' => 'required', 'errorMessage' => 'Der Wert stimmt nicht mit dem des Bildes überein'))));
$formular->addElement('input', 'captchaValue', 'Sicherheitsabfrage', array('validators' => array(array('type' => 'captcha', 'errorMessage' => 'Bitte geben Sie den Wert auf dem Bild ein'))));
$formular->addElement('submit', 'submit', 'Absenden');

$formular->addEventServer('onSubmit', '
if(wRequest::getParameter("wFormAction") == "reset"){
$this->reset();
wResponse::redirect($this->url);
}

$this->setFormData(wRequest::getParameters());
if($this->validate()){
$this->initFormData();
$this->setStatus("sent");
wCache::clearCachePath($this->url);
//wResponse::redirect($this->referrer);
$redirectURL = wURL::setParameter($this->url, \'wFormStatus\', \'\');
wOutput::loadWithoutCache($redirectURL);
wResponse::redirect($redirectURL);
}
else{
$this->setStatus("failed");
//wResponse::redirect($this->referrer);
$redirectURL = wURL::setParameter($this->url, \'wFormStatus\', \'failed\');
wOutput::loadWithoutCache($redirectURL);
wResponse::redirect($redirectURL.\'#wFormErrorMessages\');
}
'
);

if(
$formular->getStatus() == 'sent'){
print
'<div style="padding:8px;background-color:#ffffff;border:solid 1px #666666">Vielen Dank für Ihre Nachricht!</div>';
$formular->setStatus("");
}
else{
if(
$formular->getStatus() == 'failed'){
$formular->setStatus("");
}

$formular->show();
}

?>

Ergebnis