add id and label

develop
sheychen 2018-04-28 16:19:06 +02:00
parent ead22b395a
commit 4b2927860e
4 changed files with 29 additions and 4 deletions

View File

@ -16,6 +16,17 @@ class Element{
public function name() : string{ return $this->data['name']; }
public function id(string $id): self{
$this->data['id'] = $name;
return $this;
}
public function label(string $label, string $more = ''): self {
$this->data['label'] = $label;
$this->data['label.more'] = $more;
return $this;
}
public function required(bool $value = true) : self{
$this->data['required'] = $value;
return $this;
@ -42,7 +53,15 @@ class Element{
return true;
}
protected function getId(): string{
return isset($this->data['id']) ? $this->data['id'] : $this->data['name'];
}
protected function htmlLabel(): string{
return isset($this->data['label']) ? '<label for="'.$this->getId().'" '.$this->data['label.more'].'>'.$this->data['label']."</label>\n" : '';
}
public function html(string $more = '') : string{
return '<span '.$more.'></span>';
return $this->htmlLabel().'<span '.$more.'></span>';
}
}

View File

@ -135,7 +135,9 @@ class Input extends Element{
}
public function html(string $more = '') : string{
return '<input name="'.$this->data['name'].'" '.
return $this->htmlLabel().
'<input name="'.$this->data['name'].'" '.
'id="'.$this->getId().'" '.
(isset($this->data['value']) && !(isset($this->data['password']) && $this->data['password'] == true) ? 'value="'.$this->data['value'].'" ' : '').
(isset($this->data['type']) ? 'type="'.$this->data['type'].'" ' : '').
(isset($this->data['title']) ? 'title="'.$this->data['title'].'" ' : '').

View File

@ -62,7 +62,9 @@ class Select extends Element{
$options .= $option['more'].'>'.$option['text'].'</option>';
}
$html = '<select name="'.$this->data['name'].'" ';
$html = $this->htmlLabel().
'<select name="'.$this->data['name'].'" '.
'id="'.$this->getId().'" ';
$inputmore = '';
if(isset($this->data['other.text'])){
$options .= '<option value="'.$this->data['other.text'].'" '.(isset($this->data['value']) && $selected == false ? 'selected="selected" ' : '').'>'.$this->data['other.text'].'</option>';

View File

@ -8,7 +8,9 @@ class TextArea extends Element{
}
public function html(string $more = '') : string{
return '<textarea name="'.$this->data['name'].'" '.
return $this->htmlLabel().
'<textarea name="'.$this->data['name'].'" '.
'id="'.$this->getId().'" '.
(isset($this->data['value']) ? 'value="'.$this->data['value'].'" ' : '').
(isset($this->data['required']) && $this->data['required'] == true ? 'required ' : '').
$more.'></textarea>';