2017-06-01 09:44:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Krutush\Form;
|
|
|
|
|
|
|
|
class Select extends Element{
|
|
|
|
public function option(string $value, string $text = null, string $more = '') : Select{
|
|
|
|
$this->data['options'][] = array(
|
|
|
|
'value' => $value,
|
|
|
|
'text' => isset($text) ? $text : $value,
|
|
|
|
'more' => $more
|
|
|
|
);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function options(array $options) : Select{
|
|
|
|
foreach($options as $option){
|
|
|
|
if(is_string($option)){
|
|
|
|
$this->option($option);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data['options'][] = $option; //TODO convert to $this->option
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:21:03 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-25 17:15:57 +00:00
|
|
|
public function valid($data)/*: bool|string*/{
|
2017-06-01 09:44:36 +00:00
|
|
|
$parent = parent::valid($data);
|
|
|
|
if($parent !== true || !isset($data))
|
|
|
|
return $parent;
|
|
|
|
|
|
|
|
foreach($this->data['options'] as $option){
|
|
|
|
if($option['value'] == $data)
|
|
|
|
return $parent;
|
|
|
|
}
|
2017-06-10 15:21:03 +00:00
|
|
|
if(isset($this->data['other'])){
|
|
|
|
$input = $this->data['other'];
|
|
|
|
return $input->valid($data);
|
|
|
|
}
|
|
|
|
|
2017-06-01 09:44:36 +00:00
|
|
|
return 'incorrect';
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:21:03 +00:00
|
|
|
public function html(string $more = ''): string{
|
|
|
|
$selected = false;
|
|
|
|
$options = '<option disabled '.(isset($this->data['value']) ? '' : 'selected ').'value style="display:none"> --- </option>';
|
2017-06-01 09:44:36 +00:00
|
|
|
foreach($this->data['options'] as $option){
|
2017-06-10 15:21:03 +00:00
|
|
|
$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>';
|
|
|
|
}
|
|
|
|
|
2018-04-28 14:19:06 +00:00
|
|
|
$html = $this->htmlLabel().
|
|
|
|
'<select name="'.$this->data['name'].'" '.
|
|
|
|
'id="'.$this->getId().'" ';
|
2017-06-10 15:21:03 +00:00
|
|
|
$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']);
|
2017-06-01 09:44:36 +00:00
|
|
|
}
|
2017-06-10 15:21:03 +00:00
|
|
|
return $html;
|
2017-06-01 09:44:36 +00:00
|
|
|
}
|
|
|
|
}
|