parent
1b92c77116
commit
13af47ae5c
@ -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 |
||||
); |
||||
} |
||||
} |
||||
@ -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…
Reference in new issue