From a5539f2ae12c60b5ae2b5f4ebd9aba5c9dfa4d8b Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 15 Dec 2025 16:29:49 -0500 Subject: [PATCH] required --- protected/cli/run.php | 4 +++ protected/src/controllers/app/home_ctrl.php | 18 ++++------ .../{home_in.php => home/val_tests_in.php} | 33 ++++++++++-------- .../{home_out.php => home/val_tests_out.php} | 34 ++++++++++++------- .../src/views/default/app/home_index.php | 2 +- protected/src/views/default/app/val_tests.php | 27 +++++++++++++++ .../src/views/twig/app/val_tests.html.twig | 25 +++++--------- public/index.php | 4 +++ 8 files changed, 91 insertions(+), 56 deletions(-) rename protected/src/inputs/app/{home_in.php => home/val_tests_in.php} (73%) rename protected/src/outputs/app/{home_out.php => home/val_tests_out.php} (57%) create mode 100644 protected/src/views/default/app/val_tests.php diff --git a/protected/cli/run.php b/protected/cli/run.php index 634feef..ff99baa 100755 --- a/protected/cli/run.php +++ b/protected/cli/run.php @@ -28,6 +28,10 @@ function dd($var = 'nothing', endDump $end = endDump::exit_and_stop) { \CodeHydrater\common::dump($var, $end); } +function dump($var = 'nothing', endDump $end = endDump::keep_working) { + \CodeHydrater\common::dump($var, $end); +} + $is_cli = \CodeHydrater\console_app::is_cli(); if (! $is_cli) { die("Cannot run CLI from the Web!"); diff --git a/protected/src/controllers/app/home_ctrl.php b/protected/src/controllers/app/home_ctrl.php index 232ae3c..723b7b4 100644 --- a/protected/src/controllers/app/home_ctrl.php +++ b/protected/src/controllers/app/home_ctrl.php @@ -22,43 +22,38 @@ class home_ctrl extends base_controller { public function init() { // Defined from: on_footer_banner.php in the configs folder... $this->set_footer_and_authors(); + + $this->view->set('html', $this->html); + $this->view->set_php_template('main'); + $this->view->include("app/header", ViewType::PHP); + $this->view->include("app/footer", ViewType::PHP); } public function index() { - $this->view->set('html', $this->html); - $this->view->set_template('main'); - $this->view->include("app/header", ViewType::PHP); - $this->view->include("app/footer", ViewType::PHP); $content = $this->view->fetch($this, "app/home_index", ViewType::PHP); - $this->response->set_content($content); return $this->response; } public function twig() { - echo $this->footer; - $this->view->set('twig_data', ['name' => "John Doe"]); $content = $this->view->fetch($this, "app/test", ViewType::TWIG); - $this->response->set_content($content); return $this->response; } public function liquid() { - echo $this->footer; $this->view->set('template_assigns', [ 'name' => "James Smith", 'date' => date('Y-m-d') ]); $content = $this->view->fetch($this, "app/liquid", ViewType::LIQUID); - $this->response->set_content($content); return $this->response; } public function val_tests() { - IOL::auto_wire($this, "app", "home", "val_tests", null); + IOL::auto_wire($this, "app/home", "val_tests", "entry", null); return $this->response; } @@ -88,7 +83,6 @@ class home_ctrl extends base_controller { public function make_hash() { $content = \CodeHydrater\security::do_password_hash("Hello, World"); - $this->response->set_content($content); return $this->response; } diff --git a/protected/src/inputs/app/home_in.php b/protected/src/inputs/app/home/val_tests_in.php similarity index 73% rename from protected/src/inputs/app/home_in.php rename to protected/src/inputs/app/home/val_tests_in.php index 1ed425b..d77d29f 100644 --- a/protected/src/inputs/app/home_in.php +++ b/protected/src/inputs/app/home/val_tests_in.php @@ -8,19 +8,18 @@ declare(strict_types=1); * @license MIT */ -namespace Project\inputs\app; +namespace Project\inputs\app\home; use \CodeHydrater\attributes\validators as Assert; use \CodeHydrater\enums as E; use \CodeHydrater\bootstrap\use_io as IO; use \CodeHydrater\bootstrap\safer_io as SafeIO; -class home_in { +class val_tests_in { private static function define_io(string $v_rule = 'required|max: 70') { $required_post_string_field = new IO(); $required_post_string_field->input_type = E\INPUTS::variable; - $required_post_string_field->error_on_null = true; $required_post_string_field->field_filter = E\FIELD_FILTER::raw_string; $required_post_string_field->escape_html = E\HTML_FLAG::escape; $required_post_string_field->validation_rule = $v_rule; @@ -29,7 +28,7 @@ class home_in { return $required_post_string_field; } - public static function val_tests(object $app): array { + private static function validate_tests_form_class(object $app): array { $posts_data = $app->request->get_post_data(); $i_age = intval($posts_data->get("age")); @@ -42,28 +41,32 @@ class home_in { ); unset($posts_data); // free it up... - SafeIO::set_data_from($user_test_data); - $v = new \CodeHydrater\attributes\validators\my_validator(); - $v->validate($user_test_data, default_value: Assert\DEFAULT_VALUE::do_error_on_null); - + $v->validate($user_test_data); + $errors = $v->get_errors(); + return ['obj' => $user_test_data, 'err' => $errors]; + } + + public static function entry(object $app): array { + $a = self::validate_tests_form_class($app); + SafeIO::set_data_from($a['obj']); + $html['name'] = self::define_io(); $html['age'] = new IO(); $html['age']->input_type = E\INPUTS::variable; - $html['age']->error_on_null = true; $html['age']->field_filter = E\FIELD_FILTER::integer_number; - $html['age']->validation_rule = 'number_range: 18, 130'; + $html['age']->validation_rule = 'required|number_range: 18, 130'; $html['age']->validation_message = ['number_range' => 'The %s must be an Adult over %d and under %d!']; - $html['email'] = self::define_io('required|max: 255|email'); + $html['email'] = self::define_io('required|between: 7, 255|email'); return [ 'method' => $app->request->get_method(), 'table_headers' => ['Full Name', 'Age', 'Email'], 'html' => $html, - 'post_data' => get_object_vars($user_test_data), - 'a_errors' => $v->get_errors() + 'post_data' => get_object_vars($a['obj']), + 'a_errors' => $a['err'], ]; } } @@ -73,10 +76,12 @@ class my_val_tests_form { #[Assert\Required] #[Assert\Max(70)] public ?string $name = null, + #[Assert\Required] #[Assert\Positive] #[Assert\NumberRange(18, 130)] public ?int $age = null, - #[Assert\Between(2, 255)] + #[Assert\Required] + #[Assert\Between(7, 255)] #[Assert\Email] public ?string $email = null ) {} diff --git a/protected/src/outputs/app/home_out.php b/protected/src/outputs/app/home/val_tests_out.php similarity index 57% rename from protected/src/outputs/app/home_out.php rename to protected/src/outputs/app/home/val_tests_out.php index 62fa0ef..9ef5c92 100644 --- a/protected/src/outputs/app/home_out.php +++ b/protected/src/outputs/app/home/val_tests_out.php @@ -8,43 +8,51 @@ declare(strict_types=1); * @license MIT */ -namespace Project\outputs\app; +namespace Project\outputs\app\home; -use CodeHydrater\bootstrap\safer_io as SafeIO; +use \CodeHydrater\bootstrap\safer_io as SafeIO; use \CodeHydrater\enums\view_type as ViewType; -class home_out { - public static function val_tests(object $app, array & $input): array { +class val_tests_out { + + public static function entry(object $app, array & $input): array { $html_output = []; $errors = []; foreach(SafeIO::html_escape_and_sanitize($input['html']) as $html) { $key = $html['name'] ?? ""; $html_output[$key] = $html['html']; - if (\CodeHydrater\common::get_count($html['errors'])) { $errors[$key] = $html['errors'][$key]; } -// dd($html, \CodeHydrater\enums\exit_on_dump::keep_working); - } +// dump($html); + } - $one_row[] = $html_output; $did_submit = ($input['method'] === "POST"); - + $name = $input['post_data']['name'] ?? "Anonymous"; $data = [ + 'table_options' => ['helper' => 'single-row','escape' => false], + 'safe_html' => $html_output, 'did_submit' => $did_submit, 'table_headers' => $input['table_headers'], - 'user' => $one_row, - 'safe_html' => $html_output, + 'escaping' => ['escape' => false], 'post_data' => $input['post_data'], - 'a_errors' => array_merge($input['a_errors'], $errors), + 'safe_name' => \CodeHydrater\bootstrap\safer_io::h($name), + 'a_errors' => array_merge($input['a_errors'], $errors) ]; $app->view->set('twig_data', $data); $app->view->set('twig_functions', [ - 'show_table' => '\CodeHydrater\html::show_table', + 'show_table' => '\CodeHydrater\html::show_table_from', 'show_errors' => '\CodeHydrater\html::show_errors', ]); +// $app->view->set('table_cells', $input['html']); +// $app->view->set('table_headers', $input['table_headers']); +// $app->view->include("app/val_tests", ViewType::PHP); $content = $app->view->fetch($app, "app/val_tests.html", ViewType::TWIG); + + $mem = \CodeHydrater\memory_usage::get_memory_stats(echo: false); + $content = \CodeHydrater\common::str_replace_first("", $mem, $content); + $app->response->set_content($content); return []; } diff --git a/protected/src/views/default/app/home_index.php b/protected/src/views/default/app/home_index.php index 13fd28b..89e71f3 100644 --- a/protected/src/views/default/app/home_index.php +++ b/protected/src/views/default/app/home_index.php @@ -29,7 +29,7 @@ $method_rss = "feed.xml"; || -HTML Form Filter Demo +HTML Form Filter Demo || diff --git a/protected/src/views/default/app/val_tests.php b/protected/src/views/default/app/val_tests.php new file mode 100644 index 0000000..a9d902a --- /dev/null +++ b/protected/src/views/default/app/val_tests.php @@ -0,0 +1,27 @@ + + * @copyright (c) 2025, Robert Strutts + * @license MIT + */ + +function show_table($headers, $s) { + $generator = \CodeHydrater\bootstrap\safer_io::html_escape_and_sanitize($s); + $options = [ + 'helper' => 'io', + 'escape' => false, + ]; + $d = \CodeHydrater\html::show_table_from($headers, $generator, $options); + if (\CodeHydrater\common::get_count($d['errors'])) { + echo "
";
+        foreach($d['errors'] as $error) {
+            echo $error;
+        }
+        echo "
"; + } +} + +show_table($table_headers, $table_cells); \ No newline at end of file diff --git a/protected/src/views/twig/app/val_tests.html.twig b/protected/src/views/twig/app/val_tests.html.twig index 599ce4b..d6ddf95 100644 --- a/protected/src/views/twig/app/val_tests.html.twig +++ b/protected/src/views/twig/app/val_tests.html.twig @@ -1,16 +1,12 @@ - - - - Values Testing - - -

Welcome!

+

Welcome!

+ + {% if did_submit and a_errors is empty %} - - {{ show_table(table_headers, user, false) }} - -

{{ safe_html.name }}, you Rock!

+ +

{{ safe_name }}, you Rock!

+ + {{ show_table(table_headers, safe_html, table_options) }} {% else %} @@ -19,7 +15,7 @@
{{ show_errors(a_errors) }}

{% endif %} - +
@@ -41,7 +37,4 @@
- {% endif %} - - - + {% endif %} \ No newline at end of file diff --git a/public/index.php b/public/index.php index fbac8c7..c29134b 100644 --- a/public/index.php +++ b/public/index.php @@ -26,6 +26,10 @@ function dd($var = 'nothing', endDump $end = endDump::exit_and_stop) { \CodeHydrater\common::dump($var, $end); } +function dump($var = 'nothing', endDump $end = endDump::keep_working) { + \CodeHydrater\common::dump($var, $end); +} + // Boot kernel $kernel = new \CodeHydrater\http\kernel();