diff --git a/Sources/src/app/App.php b/Sources/src/app/App.php index 48929fbe..767e9fa2 100644 --- a/Sources/src/app/App.php +++ b/Sources/src/app/App.php @@ -1,4 +1,5 @@ doRender($view, null, $parameters, $response, __FUNCTION__); } +/** + * Renders a view. + * + * If an invalid form is found in the list of parameters, a 422 status code is returned. + * Forms found in parameters are auto-cast to form views. + */ + protected function render(string $view, array $parameters = [], Response $response = null): Response + { + $content = $this->renderView($view, $parameters); + $response ??= new Response(); + + if (200 === $response->getStatusCode()) { + foreach ($parameters as $v) { + if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) { + $response->setStatusCode(422); + break; + } + } + } + $response->setContent($content); + + return $response; + } private function doRenderView(string $view, ?string $block, array $parameters, string $method): string { if (!$this->container->has('twig')) { @@ -74,6 +98,23 @@ abstract class BaseController{ return $response; } + + /** + * Creates and returns a Form instance from the type of the form. + */ + protected function createForm(string $type, mixed $data = null, array $options = []): FormInterface + { + return $this->container->get('form.factory')->create($type, $data, $options); + } + + /** + * Creates and returns a form builder instance. + */ + protected function createFormBuilder(mixed $data = null, array $options = []): FormBuilderInterface + { + return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options); + } + abstract public function index(); public function __call($method, $parameters) @@ -88,9 +129,8 @@ abstract class BaseController{ * * @param string $method * @param array $parameters - * @return \Symfony\Component\HttpFoundation\Response */ - public function callAction($method, $parameters) + public function callAction($method, $parameters): Response { return $this->{$method}(...array_values($parameters)); } diff --git a/Sources/src/app/views/form/AbstractFiled.php b/Sources/src/app/views/form/AbstractFiled.php index e69de29b..a6999d08 100644 --- a/Sources/src/app/views/form/AbstractFiled.php +++ b/Sources/src/app/views/form/AbstractFiled.php @@ -0,0 +1,74 @@ +name = $name; + } + + public function getName() { + return $this->name; + } + + // Autres méthodes... +} + +// abstract class AbstractField { +// protected $name; +// protected $value; +// protected $attributes; +// protected $errors = []; +// protected $validators = []; + +// public function __construct($name, $value = '', $attributes = []) { +// $this->name = $name; +// $this->value = $value; +// $this->attributes = $attributes; +// } + +// public function getName() { +// return $this->name; +// } + +// public function getValue() { +// return $this->value; +// } + +// public function setValue($value) { +// $this->value = $value; +// } + +// public function addValidator(callable $validator) { +// $this->validators[] = $validator; +// return $this; +// } + +// public function isValid() { +// foreach ($this->validators as $validator) { +// if (!$validator($this->value)) { +// return false; +// } +// } +// return true; +// } + +// public function getErrors() { +// return $this->errors; +// } + +// public function addError($error) { +// $this->errors[] = $error; +// } + +// public function getAttributesAsString() { +// $attributesStr = ''; +// foreach ($this->attributes as $key => $value) { +// $attributesStr .= " $key=\"$value\""; +// } +// return $attributesStr; +// } + +// abstract public function render(); +// } diff --git a/Sources/src/app/views/form/AbstractForm.php b/Sources/src/app/views/form/AbstractForm.php index e69de29b..4cf28307 100644 --- a/Sources/src/app/views/form/AbstractForm.php +++ b/Sources/src/app/views/form/AbstractForm.php @@ -0,0 +1,29 @@ +form = new BasicForm(); + } + + public function addText($name, $options) { + $this->form->add(FieldFactory::createField('text', $options)); + return $this; + } + + public function build() { + return $this->form; + } +} \ No newline at end of file diff --git a/Sources/src/app/views/form/IForm.php b/Sources/src/app/views/form/IForm.php index e69de29b..562b6e35 100644 --- a/Sources/src/app/views/form/IForm.php +++ b/Sources/src/app/views/form/IForm.php @@ -0,0 +1,8 @@ + + \ No newline at end of file diff --git a/Sources/src/app/views/form/TextField.php b/Sources/src/app/views/form/TextField.php index e69de29b..f34b36a5 100644 --- a/Sources/src/app/views/form/TextField.php +++ b/Sources/src/app/views/form/TextField.php @@ -0,0 +1,11 @@ +name}' value='{$this->value}' />"; + } + + public function validate($value) { + // Logique de validation + } +} \ No newline at end of file