parent
a5539f2ae1
commit
fb08e66d55
@ -0,0 +1,64 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/** |
||||
* @author Robert Strutts <Bob_586@Yahoo.com> |
||||
* @copyright (c) 2025, Robert Strutts |
||||
* @license MIT |
||||
*/ |
||||
|
||||
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; |
||||
|
||||
/** |
||||
* This is how to wire up using Attributes on a Class... |
||||
*/ |
||||
|
||||
class alt_val_tests_in { |
||||
|
||||
public static function entry(object $app): array { |
||||
$posts_data = $app->request->get_post_data(); |
||||
|
||||
$i_age = intval($posts_data->get("age")); |
||||
$age = ($i_age > 0) ? $i_age : null; |
||||
|
||||
$user_test_data = new my_val_tests_form( |
||||
$posts_data->get("name"), |
||||
$age, |
||||
$posts_data->get("email") |
||||
); |
||||
|
||||
unset($posts_data); // free it up... |
||||
$v = new \CodeHydrater\attributes\validators\my_validator(); |
||||
$v->validate($user_test_data); |
||||
$errors = $v->get_errors(); |
||||
|
||||
return [ |
||||
'method' => $app->request->get_method(), |
||||
'table_headers' => ['Full Name', 'Age', 'Email'], |
||||
'post_data' => get_object_vars($user_test_data), |
||||
'a_errors' => $errors, |
||||
]; |
||||
} |
||||
} |
||||
|
||||
class my_val_tests_form { |
||||
public function __construct( |
||||
#[Assert\Required] |
||||
#[Assert\Max(70)] |
||||
public ?string $name = null, |
||||
#[Assert\Required] |
||||
#[Assert\Positive] |
||||
#[Assert\NumberRange(18, 130)] |
||||
public ?int $age = null, |
||||
#[Assert\Required] |
||||
#[Assert\Between(7, 255)] |
||||
#[Assert\Email] |
||||
public ?string $email = null |
||||
) {} |
||||
} |
||||
@ -0,0 +1,81 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/** |
||||
* @author Robert Strutts <Bob_586@Yahoo.com> |
||||
* @copyright (c) 2025, Robert Strutts |
||||
* @license MIT |
||||
*/ |
||||
|
||||
namespace Project\outputs\app\home; |
||||
|
||||
use \CodeHydrater\bootstrap\safer_io as SafeIO; |
||||
use \CodeHydrater\enums\view_type as ViewType; |
||||
use \CodeHydrater\form_builder\html_form as Form; |
||||
|
||||
class alt_val_tests_out { |
||||
|
||||
private static function build_group(array $a, Form & $f, array $headers): void { |
||||
$i =0; |
||||
foreach($a as $name => $v) { |
||||
$o = ['class'=> 'form-control']; |
||||
$f->open_div(['class'=>'mb-3']); |
||||
$label = $headers[$i] ?? "Out of Bounds!"; |
||||
$f->label($label, array_merge($o, ['for'=>$name])); |
||||
$f->close_div(); |
||||
$f->open_div(['class'=>'mb-3']); |
||||
if ($name === "age") { |
||||
$f->number($name, array_merge($o, ['id'=>$name, 'value'=>$v, 'required'])); |
||||
} else { |
||||
$f->text($name, array_merge($o, ['id'=>$name, 'value'=>$v, 'required'])); |
||||
} |
||||
|
||||
$f->close_div(); |
||||
$i++; |
||||
} |
||||
} |
||||
|
||||
private static function build_form(array $a, array $headers): ?string { |
||||
$form_builder = new Form(); |
||||
$form_builder->set_escape(false); |
||||
$form_builder->open(options: ['autocomplete'=>'off']); |
||||
$form_builder->fieldset_open(['style'=>'width: 200px;']); |
||||
$form_builder->legend("News Letter"); |
||||
self::build_group($a, $form_builder, $headers); |
||||
$form_builder->open_div(['class'=>'mb-3']); |
||||
$form_builder->submit("go", ['class'=>'btn btn-primary']); |
||||
$form_builder->close_div(); |
||||
$form_builder->fieldset_close(); |
||||
$form_builder->close(); |
||||
return $form_builder->get_output(); |
||||
} |
||||
|
||||
public static function entry(object $app, array $input): array { |
||||
$did_submit = ($input['method'] === "POST"); |
||||
$name = $input['post_data']['name'] ?? "Anonymous"; |
||||
$data = [ |
||||
'form_builder' => self::build_form($input['post_data'], $input['table_headers']), |
||||
'table_options' => ['helper' => 'single-row','escape' => true], |
||||
'did_submit' => $did_submit, |
||||
'table_headers' => $input['table_headers'], |
||||
'escaping' => ['escape' => false], |
||||
'post_data' => $input['post_data'], |
||||
'safe_name' => \CodeHydrater\bootstrap\safer_io::h($name), |
||||
'a_errors' => $input['a_errors'], |
||||
]; |
||||
|
||||
$app->view->set('twig_data', $data); |
||||
$app->view->set('twig_functions', [ |
||||
'show_table' => '\CodeHydrater\html::show_table_from', |
||||
'show_errors' => '\CodeHydrater\html::show_errors', |
||||
]); |
||||
$content = $app->view->fetch($app, "app/alt_val_tests.html", ViewType::TWIG); |
||||
|
||||
$mem = \CodeHydrater\memory_usage::get_memory_stats(echo: false); |
||||
$content = \CodeHydrater\common::str_replace_first("</memory>", $mem, $content); |
||||
|
||||
$app->response->set_content($content); |
||||
return []; |
||||
} |
||||
} |
||||
@ -0,0 +1,16 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Project\routes; |
||||
|
||||
use \CodeHydrater\router as Router; |
||||
|
||||
class cli_routes { |
||||
|
||||
public static function get() { |
||||
// ::i is a int() shortcut, ::s is a string |
||||
Router::get('example/{id::i}', 'Project\routes\app\home_routes@test'); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,22 @@ |
||||
<h1>Welcome!</h1> |
||||
|
||||
</memory> |
||||
|
||||
{% if did_submit and a_errors is empty %} |
||||
|
||||
<p>{{ safe_name }}, you Rock!</p> |
||||
|
||||
{{ show_table(table_headers, post_data, table_options) }} |
||||
|
||||
{% else %} |
||||
|
||||
{% if did_submit and a_errors %} |
||||
<h2 style="color: red;">Errors Reporeted, are:</h2> |
||||
<div style="color: darkred"> {{ show_errors(a_errors) }} </div> |
||||
<hr> |
||||
{% endif %} |
||||
<br> |
||||
|
||||
{{ form_builder|raw }} |
||||
|
||||
{% endif %} |
||||
Loading…
Reference in new issue