Fix and more...

master
sheychen 2017-06-10 17:21:03 +02:00
parent 89077f4548
commit e0afa98725
5 changed files with 95 additions and 15 deletions

View File

@ -8,7 +8,6 @@
}
],
"require": {
//PHP7.1
"krutush/template": "dev-master"
},
"autoload": {

View File

@ -9,14 +9,19 @@ class Element{
$this->data['name'] = $name;
}
public function rename(string $name): self{
$this->data['name'] = $name;
return $this;
}
public function name() : string{ return $this->data['name']; }
public function required(bool $value = true) : Element{
public function required(bool $value = true) : self{
$this->data['required'] = $value;
return $this;
}
public function value(string $value) : Element{
public function value(string $value) : self{
$this->data['value'] = $value;
return $this;
}
@ -25,7 +30,7 @@ class Element{
return $this->data['value'];
}
public function error(bool $value = true) : Element{
public function error(bool $value = true) : self{
$this->data['error'] = $value;
return $this;
}

View File

@ -58,7 +58,29 @@ class Form {
$this->method = $method;
$this->url = $url;
}
return '<form method="'.$method.'" '.(isset($url) ? 'action="'.$url.'" ' : '').' '.$more.'>';
$html = '<form method="'.$method.'" '.(isset($url) ? 'action="'.$url.'" ' : '').$more.'>';
$html .= "
<script type=\"text/javascript\">
function SelectOther(source, other){
if(source.tagName == 'SELECT' && source.value == other){
source.style.display = 'none';
source.disabled = true;
var input = document.getElementsByName(source.name)[1];
input.style.display = '';
input.disabled = false;
input.value = source.value;
}else if(source.tagName == 'INPUT' && source.value.length == 0){
source.style.display = 'none';
source.disabled = true;
var select = document.getElementsByName(source.name)[0];
select.style.display = '';
select.disabled = false;
select.value = '';
}
}
</script>
";
return $html;
}
public function end(string $more = '') : string{
@ -80,7 +102,10 @@ class Form {
return '<input type="submit" '.(isset($name) ? 'value="'.$name.'" ' : '').$more.'>';
}
function input(string $name) : Element{
function input(string $name, bool $add = true) : Element{
if($add == false)
return new Input($name);
if($this->set == true){
$input = $this->get($name);
if(isset($input))
@ -91,7 +116,9 @@ class Form {
return $input;
}
function select(string $name) : Element{
function select(string $name, bool $add = true) : Element{
if($add == false)
return new Select($name);
if($this->set == true){
$input = $this->get($name);
if(isset($input))
@ -113,7 +140,7 @@ class Form {
return $input;
}
public function add(Element $thing) : void{
public function add(Element $thing){
if($this->set == false)
$this->elements[] = $thing;
}

View File

@ -21,6 +21,13 @@ class Input extends Element{
return $this;
}
public function checkbox(): Input{
$this->data['type'] = 'checkbox';
$this->data['checkbox'] = true;
$this->data['value'] = $this->data['name'];
return $this;
}
public function minlength(int $value) : Input{
$this->data['minlength'] = $value;
return $this;
@ -96,7 +103,6 @@ class Input extends Element{
(isset($this->data['type']) ? 'type="'.$this->data['type'].'" ' : '').
(isset($this->data['title']) ? 'title="'.$this->data['title'].'" ' : '').
(isset($this->data['pattern']) ? 'pattern="'.$this->data['pattern'].'" ' : '').
(isset($this->data['pattern']) ? 'pattern="'.$this->data['pattern'].'" ' : '').
(isset($this->data['minlength']) ? 'minlength="'.$this->data['minlength'].'" ' : '').
(isset($this->data['maxlength']) ? 'maxlength="'.$this->data['maxlength'].'" ' : '').
(isset($this->data['required']) && $this->data['required'] == true ? 'required ' : '').

View File

@ -24,6 +24,15 @@ class Select extends Element{
return $this;
}
public function other(Input $input, string $text, string $more = ''): Select{
$input->rename($this->name());
$this->data['other'] = $input;
$this->data['other.text'] = $text;
$this->data['other.more'] = $more;
return $this;
}
public function valid(mixed $data)/*: bool|string*/{
$parent = parent::valid($data);
if($parent !== true || !isset($data))
@ -33,16 +42,50 @@ class Select extends Element{
if($option['value'] == $data)
return $parent;
}
if(isset($this->data['other'])){
$input = $this->data['other'];
return $input->valid($data);
}
return 'incorrect';
}
public function html(string $more = '') : string{
$html = '<select name="'.$this->data['name'].'" '.
(isset($this->data['required']) && $this->data['required'] == true ? 'required ' : '').
$more.'><option disabled '.(isset($this->data['value']) ? 'selected ' : '').'value style="display:none"> --- </option>';
public function html(string $more = ''): string{
$selected = false;
$options = '<option disabled '.(isset($this->data['value']) ? '' : 'selected ').'value style="display:none"> --- </option>';
foreach($this->data['options'] as $option){
$html .= '<option value="'.$option['value'].'" '.((isset($this->data['value']) && $this->data['value'] == $option['value']) ? 'selected="selected" ' : '' ).$option['more'].'>'.$option['text'].'</option>';
$options .= '<option value="'.$option['value'].'" ';
if(isset($this->data['value']) && $this->data['value'] == $option['value']){
$options .= 'selected="selected" ';
$selected = true;
}
$options .= $option['more'].'>'.$option['text'].'</option>';
}
return $html.'</select>';
$html = '<select name="'.$this->data['name'].'" ';
$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>';
//script in From->start()
$inputmore .= 'class="SelectOther" onchange="SelectOther(this,\''.$this->data['other.text'].'\')" ';
$html .= 'class="SelectOther" onchange="SelectOther(this,\''.$this->data['other.text'].'\')" ';
if(isset($this->data['value']) && $selected == false){
$html .= 'disabled style="display: none;" ';
$this->data['other']->value($this->data['value']);
}else{
$inputmore .= 'disabled style="display: none;" ';
}
}
if(isset($this->data['required']) && $this->data['required'] == true){
$html .= 'required ';
$inputmore .= 'required ';
}
$html .= $more.'>';
$html .= $options;
$html .= '</select>';
if(isset($this->data['other'])){
$html .= $this->data['other']->html($inputmore.$this->data['other.more']);
}
return $html;
}
}