iol testing...

main
Robert 2 months ago
parent 1b92c77116
commit 13af47ae5c
  1. 11
      protected/src/controllers/app/home_ctrl.php
  2. 86
      protected/src/inputs/app/home_in.php
  3. 29
      protected/src/inputs/test_val.php
  4. 69
      protected/src/outputs/app/home_out.php
  5. 8
      protected/src/views/default/app/home_index.php
  6. 41
      protected/src/views/twig/app/val_tests.html.twig

@ -11,6 +11,7 @@ namespace Project\controllers\app;
use \CodeHydrater\base_controller; use \CodeHydrater\base_controller;
use \CodeHydrater\enums\view_type as ViewType; use \CodeHydrater\enums\view_type as ViewType;
use \CodeHydrater\bootstrap\use_iol as IOL;
/** /**
* Description of home_ctrl URL Route: /FOLDER/FILE/METHOD * Description of home_ctrl URL Route: /FOLDER/FILE/METHOD
@ -57,15 +58,7 @@ class home_ctrl extends base_controller {
} }
public function val_tests() { public function val_tests() {
$user_test = new \Project\inputs\test_val("Joe", 10, "Joe@AOL.com"); IOL::auto_wire($this, "app", "home", "val_tests", null);
$v = new \CodeHydrater\attributes\validators\my_validator();
$v->validate($user_test);
$errors = $v->get_errors();
print_r($errors);
$this->response->set_content("Done");
return $this->response; return $this->response;
} }

@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
/**
* @author Robert Strutts <Bob_586@Yahoo.com>
* @copyright (c) 2025, Robert Strutts
* @license MIT
*/
namespace Project\inputs\app;
use \CodeHydrater\attributes\validators as Assert;
use \CodeHydrater\enums\INPUTS;
use \CodeHydrater\enums\FIELD_FILTER;
use \CodeHydrater\enums\HTML_FLAG;
use \CodeHydrater\enums\DB_FILTER;
use \CodeHydrater\bootstrap\use_io as IO;
use \CodeHydrater\bootstrap\safer_io as SafeIO;
class home_in {
private static function define_io(string $v_rule = 'required|max: 70') {
$required_post_string_field = new IO();
$required_post_string_field->input_type = INPUTS::variable;
$required_post_string_field->field_filter = FIELD_FILTER::raw_string;
$required_post_string_field->escape_html = HTML_FLAG::escape;
$required_post_string_field->validation_rule = $v_rule;
$required_post_string_field->use_db_filter = DB_FILTER::OFF;
$required_post_string_field->skip_the_db = false;
return $required_post_string_field;
}
public static function val_tests(object $app): array {
$posts_data = $app->request->get_post_data();
$user_test_data = new my_val_tests_form(
$posts_data->get("name"),
intval($posts_data->get("age")),
$posts_data->get("email")
);
unset($posts_data); // free it up...
SafeIO::set_data_input('name', $user_test_data->name);
SafeIO::set_data_input('age', $user_test_data->age);
SafeIO::set_data_input('email', $user_test_data->email);
$v = new \CodeHydrater\attributes\validators\my_validator();
$v->validate($user_test_data);
$name_field = self::define_io();
$adult_post_int_age_field = new IO();
$adult_post_int_age_field->input_type = INPUTS::post;
$adult_post_int_age_field->field_filter = FIELD_FILTER::integer_number;
$adult_post_int_age_field->validation_rule = 'greater_than: 18';
$adult_post_int_age_field->validation_message = ['greater_than' => 'The %s must be an Adult over %d!'];
$email_field = self::define_io('required|max: 255');
return [
'html' => [
'name' => $name_field,
'age' => $adult_post_int_age_field,
'email' => $email_field,
],
'post_data' => get_object_vars($user_test_data),
'a_errors' => $v->get_errors()
];
}
}
class my_val_tests_form {
public function __construct(
#[Assert\Required]
#[Assert\Max(70)]
public ?string $name = null,
#[Assert\Positive]
#[Assert\NumberRange(18, 130)]
public ?int $age = null,
#[Assert\Between(2, 255)]
#[Assert\Email]
public ?string $email = null
) {}
}

@ -1,29 +0,0 @@
<?php
declare(strict_types=1);
/**
* @author Robert Strutts <Bob_586@Yahoo.com>
* @copyright (c) 2025, Robert Strutts
* @license MIT
*/
namespace Project\inputs;
use \CodeHydrater\attributes\validators as Assert;
class test_val {
public function __construct(
#[Assert\Required]
#[Assert\Max(70)]
public ?string $name = null,
#[Assert\Positive]
#[Assert\NumberRange(18, 65)]
public ?int $age = null,
#[Assert\Between(2, 255)]
#[Assert\Email]
public ?string $email = null
) {
echo $this->name;
}
}

@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
/**
* @author Robert Strutts <Bob_586@Yahoo.com>
* @copyright (c) 2025, Robert Strutts
* @license MIT
*/
namespace Project\outputs\app;
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 {
$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];
}
}
$data = [
'safe_html' => $html_output,
'post_data' =>$input['post_data'],
'a_errors' => array_merge($input['a_errors'], $errors),
];
$app->view->set('twig_data', [
...$data
]);
$app->view->set('twig_functions', [
'show_user_details' => '\Project\outputs\app\home_out::show_user_details',
'show_errors' => '\Project\outputs\app\home_out::show_errors',
]);
$content = $app->view->fetch($app, "app/val_tests.html", ViewType::TWIG);
$app->response->set_content($content);
return [];
}
public static function show_errors(?array $errors = []): void {
if ($errors) {
$message = "Please correct the following errors: ";
$i = 0;
foreach($errors as $error) {
$i++;
$message .= "<br/>{$i}) " . $error;
}
echo $message;
}
}
public static function show_user_details(array $input): void {
$rows[] = $input; // Force one rows
\CodeHydrater\html::show_table(
['Full Name', 'Age', 'Email'],
$rows,
escape: false
);
}
}

@ -8,6 +8,9 @@ $method_name_demo = "twig";
$route_liquid = "app/home"; $route_liquid = "app/home";
$method_liquid = "liquid"; $method_liquid = "liquid";
$route_form = "app/home";
$method_form = "val_tests";
$route_rss = "app/rss"; $route_rss = "app/rss";
$method_rss = "feed.xml"; $method_rss = "feed.xml";
@ -25,6 +28,11 @@ $method_rss = "feed.xml";
|| ||
<a href="<?= \CodeHydrater\misc::get_url($route_form, $method_form) ?>">HTML Form Filter Demo</a>
||
<a href="<?= \CodeHydrater\misc::get_url($route_rss, $method_rss) ?>">View RSS XML feed Demo</a> <a href="<?= \CodeHydrater\misc::get_url($route_rss, $method_rss) ?>">View RSS XML feed Demo</a>
</div> </div>

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html>
<head>
<title>Values Testing</title>
</head>
<body>
<h1>Welcome!</h1>
{% if post_data.name %}
{{ show_user_details(safe_html) }}
{% if a_errors %}
<h2>Errors Reporeted, are:</h2>
{{ show_errors(a_errors) }}
{% endif %}
{% else %}
<form method="POST">
<table>
<tr>
<td><label for="name">Your full name</label></td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td><label for="age">Age</label></td>
<td><input type="number" name="age" id="age"></td>
</tr>
<tr>
<td><label for="email">Email Address</label></td>
<td><input type="email" name="email" id="email"></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
{% endif %}
</body>
</html>
Loading…
Cancel
Save