Ik heb een formulier gemaakt volgens OOP principe. Ik wil dat de Error message in het formulier getoond wordt op de plek waar het veld fout is. Hoe kan ik dit regelen?
In het vogende stuk code (Ik weet niet hoe ik de stukken code van elkaar kan schreiden:
Code Form class:
Code Element class
Code Text Element
Code RegistratieForm
Code formulier.php
[code]
<?php
abstract class Form implements IForm {
private $elements;
private $id;
private $action;
private $method;
private $attribs;
private $isGeldig;
private $errorMessages;
private $values;
const METH_POST = 'post';
const METH_GET = 'get';
public function __construct($options) {
$this->isGeldig = true;
$this->errorMessages = array();
$this->values = array();
$this->id = isset($options['id']) ? $options['id'] : 'Formulier';
$this->action = isset($options['action']) ? $options['action'] : '#';
$this->method = isset($options['method']) ? $options['method'] : self::METH_POST;
//attribs zijn alle overige HTML-attributen die je aan een formulier zou willen toekennen: style, class etc.
$this->attribs = isset($options['attribs']) ? $options['attribs'] : '';
}
public function add(IElement $elm) {
$this->elements[$elm->getName()] = $elm;
}
public function render() {
//PHP_EOL is een ingebouwde constante in PHP die een nieuwe regel maakt (zie je in broncode van de HTML)
$html = "<form method='{$this->method}' action='{$this->action}' id='{$this->id}' {$this->attribs}>" . PHP_EOL;
foreach ($this->elements as $element) {
$html .= $element->render() . '<br />' . PHP_EOL;
}
$html .= "</form>" . PHP_EOL;
return $html;
}
public function isValid() {
$this->clean();
foreach ($this->elements as $element) {
if (!$element->isValid()) {
$this->isGeldig = false;
$this->errorMessages[$element->getName()] = $element->getMessages();
}
}
return $this->isGeldig;
}
public function getMessages() {
return $this->errorMessages;
}
public function clean() {
foreach ($this->elements as $element) {
$element->clean();
}
}
public function getValues() {
foreach ($this->elements as $key => $element) {
$this->values[$key] = $element->getValue();
}
return $this->values;
}
}
?>
code class Element
<?php
abstract class Element implements IElement {
protected $filters;
protected $validators;
protected $options;
protected $value;
protected $name;
protected $label;
protected $attribs;
protected $isGeldig;
protected $errorMessages;
public function __construct(array $options = array())
{
$this->isGeldig = true;
$this->filters = array();
$this->validators = array();
$this->value = '';
$this->setOptions($options);
}
public function setOptions(array $options)
{
$this->name = isset($options['name']) ? $options['name'] : 'standaardnaam'; //eigenlijk moet de naam worden getest; niet alle namen zijn volgens de HTML-specs toegestaan!
$this->label = isset($options['label']) ? $options['label'] : 'label';
$this->value = isset($options['value']) ? $options['value'] : '';
$this->attribs = isset($options['attribs']) ? $options['attribs'] : '';
if (isset($options['validators']) && is_array($options['validators'])) {
foreach ($options['validators'] as $validator) {
$this->addValidator($validator);
}
}
if (isset($options['filters']) && is_array($options['filters'])) {
foreach ($options['filters'] as $filter) {
$this->addFilter($filter);
}
}
}
public function addValidator(Validator $validator)
{
$this->validators[] = $validator;
}
abstract public function render();
public function addFilter(Filter $filter)
{
$this->filters[] = $filter;
}
public function isValid()
{
foreach ($this->validators as $validator) {
if (!$validator->isValid($this->value)) {
$this->isGeldig = false;
$this->errorMessages[] = $validator->getMessages();
}
}
return $this->isGeldig;
}
public function clean()
{
$localValue = $this->value;
foreach ($this->filters as $filter) {
$localValue = $filter->clean($localValue);
}
$this->value = $localValue;
}
public function getValue()
{
return $this->value;
}
public function getMessages()
{
return $this->errorMessages;
}
public function getName()
{
return $this->name;
}
}
?>
Code Text element
<?php
/**
* User: GJ
* Name: TextElement.php
* Date: 14-8-13
* Time: 12:10
*/
class TextElement extends Element {
public function render()
{
$html = '';
$html .= '<label>' . $this->label;
$html .= "<input type='text' name='{$this->name}' value='{$this->value}' $this->attribs>";
$html .= '</label>';
$html .= $this->getMessages();
return $html;
}
}
?>
Code RegistratieForm:
<?php
class RegistratieForm extends Form {
public function __construct($options) {
parent::__construct($options);
$initials = new TextElement();
// we kunnen een element aanmaken en pas daarna de opties zetten, zoals hier:
$initials->setOptions(array(
'name' => 'voorletters',
'label' => 'Uw voorlettes',
'value' => isset($_GET['voorletters']) ? $_GET['voorletters'] : '',
'validators' => array(
new InitialValidator(array() /* no options necessary */),
),
)
);
$lastname = new TextElement();
// we kunnen een element aanmaken en pas daarna de opties zetten, zoals hier:
$lastname->setOptions(array(
'name' => 'achternaam',
'label' => 'Uw achternaam',
'value' => isset($_GET['achternaam']) ? $_GET['achternaam'] : '',
'validators' => array(
new LastNameValidator(array() /* no options necessary */),
),
'filters' => array(
new FilterStripTags(),
new FilterTrim()
)
)
);
$email = new TextElement();
// we kunnen een element aanmaken en pas daarna de opties zetten, zoals hier:
$email->setOptions(array(
'name' => 'email',
'label' => 'Uw e-mailadres',
'value' => isset($_GET['email']) ? $_GET['email'] : '',
'validators' => array(
new EmailValidator(array() /* no options necessary */),
),
'filters' => array(
new FilterStripTags(),
new FilterTrim()
)
)
);
// of we maken een element en geven bij de constructie al de opties weer, zoals hier
$password = new PasswordElement(array(
'name' => 'password',
'label' => 'Uw wachtwoord',
'value' => isset($_GET['password']) ? $_GET['password'] : '',
'validators' => array(
new StringLengthValidator(array('min' => 3, 'minError' => 'Het wachtwoord is te kort', 'max' => 12, 'maxError' => 'Het wachtwoord is te lang'))
),
'filters' => array(
new FilterStripTags(),
new FilterTrim()
)
)
);
$submit = new SubmitElement(array(
'value' => 'verzenden',
'name' => 'verzenden',
)
);
//de gemaakte elementen toevoegen aan het formulier
$this->add($initials);
$this->add($lastname);
$this->add($email);
$this->add($password);
$this->add($submit);
}
}
?>
code formulier.php:
<?php
//autoloader
function __autoload($class) {
require_once $class . '.php';
}
//het formulier aanmaken
$form = new RegistratieForm(
array(
'id' => 'frmRegistratie',
'css' => 'userform',
'method' => 'get',
'action' => ''
)
);
//de elementen aanmaken:
if (isset($_GET['verzenden'])) {
$result = $form->isValid();
if ($result == TRUE) {
var_dump('correct', $form->getValues());
} else {
var_dump('', $form->getMessages());
}
die();
}
$html = $form->render();
echo $html;
?>
Hopelijk kan iemand mij helpen om de foutmeldingen in het formulier te krijgen op de plek waar de waarde niet valide is. (en de rest van het formulier ingevuld houden).
Alvast bedankt!
1.634 views