main
Robert 2 months ago
parent d734efaffe
commit 0b723efc60
  1. 57
      src/bootstrap/safer_io.php
  2. 36
      src/classes/form_builder/html_form.php

@ -43,6 +43,10 @@ final class use_io {
public $input_var; public $input_var;
public string $field_description; public string $field_description;
public FORM_INPUT_TYPES $form_type; public FORM_INPUT_TYPES $form_type;
public bool $show_label = true;
public string $div_class = "";
public string $label_class = "";
public string $form_control_class = "";
public INPUTS $input_type; public INPUTS $input_type;
public FIELD_FILTER $field_filter; public FIELD_FILTER $field_filter;
public HTML_FLAG $escape_html; public HTML_FLAG $escape_html;
@ -97,6 +101,43 @@ final class safer_io {
protected function __construct() { protected function __construct() {
} }
public static function build_html(array $input): array {
$html_output = [];
$errors = [];
$headers = [];
$show_labels =[];
$form_type = [];
$required = [];
$classes = [];
foreach(self::esv($input['html']) as $html) {
$key = $html['name'] ?? "";
$html_output[$key] = $html['html'];
$headers[$key] = $html['meta']['field_desc'][$key] ?? "";
$show_labels[$key] = $html['meta']['show_label'][$key] ?? true;
$form_type[$key] = $html['meta']['form_type'][$key] ?? FORM_TYPE::text->value;
$classes['label'][$key] = $html['meta']['label_class'] ?? "";
$classes['div'][$key] = $html['meta']['div_class'] ?? "";
$classes['ctrl'][$key] = $html['meta']['form_control_class'] ?? "";
$required[$key] = $html['meta']['required'][$key] ?? false;
if (\CodeHydrater\common::get_count($html['errors'])) {
$errors[$key] = $html['errors'][$key];
}
// dump($html);
}
return [
'div-class' => $input['classes']['div-class'] ?? "mb-3",
'form-control-class' => $input['classes']['form-control-class'] ?? "",
'label-class' => $input['classes']['label-class'] ?? "",
'classes'=>$classes,
'html'=>$html_output,
'errors'=>$errors,
'headers'=>$headers,
'show_labels'=>$show_labels,
'form_type'=>$form_type,
'required'=>$required
];
}
/** /**
* Get raw data for inputs, to be used by filters * Get raw data for inputs, to be used by filters
@ -484,6 +525,22 @@ final class safer_io {
$meta['field_desc'][$input_field_name] = $a->field_description; $meta['field_desc'][$input_field_name] = $a->field_description;
} }
if (isset($a->show_label)) {
$meta['show_label'][$input_field_name] = $a->show_label;
}
if (isset($a->div_class)) {
$meta['div_class'][$input_field_name] = $a->div_class;
}
if (isset($a->label_class)) {
$meta['label_class'][$input_field_name] = $a->label_class;
}
if (isset($a->form_control_class)) {
$meta['form_control_class'][$input_field_name] = $a->form_control_class;
}
if (isset($a->validation_rule)) { if (isset($a->validation_rule)) {
if (str_contains($a->validation_rule, "required")) { if (str_contains($a->validation_rule, "required")) {
$meta['required'][$input_field_name] = true; $meta['required'][$input_field_name] = true;

@ -53,6 +53,42 @@ class html_form {
return $this->input($name, $options); return $this->input($name, $options);
} }
public function build_group(array $a): void {
foreach($a['html'] as $name => $v) {
// Fix Values, so they are always a String!
$value = ($v === false || $v === null) ? "" : $v;
$type_of_form_input = $a['form_type'][$name]->value ?? FORM_TYPE::text->value;
if ($type_of_form_input === "none") {
continue; // Skip none controls
}
$is_required = $a['required'][$name] ?? false;
$r_a = ($is_required) ? ['required' => ""] : [];
$my_classes = $a['classes'] ?? [];
$my_div_class = $classes['div'][$name] ?? null;
$my_label_class = $classes['label'][$name] ?? null;
$my_ctrl_class = $classes['ctrl'][$name] ?? null;
$form_control_class = $my_ctrl_class ?? $a['form-control-class'] ?? "form-control";
$fc = ['class'=> $form_control_class];
$div_class = $my_div_class ?? $a['div-class'] ?? "mb-3";
$label_class = $my_label_class ?? $a['label-class'] ?? "";
$this->open_div(['class' => $div_class]);
$label = $a['headers'][$name] ?? null;
$show_label = $a['show_labels'][$name] ?? true;
if (! empty($label) && $show_label) {
$this->label($label, array_merge(['class' => $label_class], ['for'=>$name]));
} else {
$label_name = $label ?? $name;
$r_a['placeholder'] = $label_name;
}
$this->$type_of_form_input($name, array_merge($fc, ['id'=>$name, 'value'=>$value], $r_a));
$this->close_div();
}
}
private function do_escape(string $content, array & $options): ?string { private function do_escape(string $content, array & $options): ?string {
$escape = $options['escape'] ?? $this->escape; $escape = $options['escape'] ?? $this->escape;

Loading…
Cancel
Save