wrappedElement = $element;
$element->prependWrapper($newWrapper);
return $newWrapper;
}
protected function renderLabel()
{
if ($this->wrappedElement instanceof BaseFormElement) {
$label = $this->wrappedElement->getLabel();
if ($label) {
return Html::tag('label', null, $label);
}
}
return null;
}
public function getAttributes()
{
$attributes = parent::getAttributes();
// TODO: only when sent?!
if ($this->wrappedElement->hasBeenValidatedAndIsNotValid()) {
$classes = $attributes->get('class');
if (
empty($classes)
|| (is_array($classes) && ! in_array('errors', $classes))
|| (is_string($classes) && $classes !== 'errors')
) {
$attributes->add('class', 'errors');
}
}
return $attributes;
}
protected function renderDescription()
{
if ($this->wrappedElement instanceof BaseFormElement) {
$description = $this->wrappedElement->getDescription();
if ($description) {
return Html::tag('p', ['class' => 'description'], $description);
}
}
return null;
}
protected function renderErrors()
{
if ($this->wrappedElement instanceof BaseFormElement) {
$errors = [];
foreach ($this->wrappedElement->getMessages() as $message) {
$errors[] = Html::tag('p', ['class' => 'error'], $message);
}
if (! empty($errors)) {
return $errors;
}
}
return null;
}
public function addHtml(ValidHtml ...$content)
{
// TODO: is this required?
if (! in_array($this->wrappedElement, $content, true)) {
parent::addHtml(...$content);
}
return $this;
}
protected function assemble()
{
$this->addHtml($this->dt(), $this->dd());
$this->ready = true;
}
protected function dt()
{
if ($this->dt === null) {
$this->dt = Html::tag('dt', null, $this->renderLabel());
}
return $this->dt;
}
/**
* @return \ipl\Html\HtmlElement
*/
protected function dd()
{
if ($this->dd === null) {
$this->dd = Html::tag('dd', null, [
$this->wrappedElement,
$this->renderErrors(),
$this->renderDescription()
]);
}
return $this->dd;
}
public function __destruct()
{
$this->wrapper = null;
}
}