Bonsoir,
Je débute dans les classes et je n'arrive pas à trouver la solution à mon problème. La classe est fortement inspirée de la classe
Le code
<?php
require 'classes/Form.class.php';
// $new_form = new Create_form('nom du formulaire', 'action', 'method : get/post', 'tag entre champ : p/div', 'position du label : before/after/top');
$new_form = new Create_form('formulaire', 'verification.php', 'post', 'p', 'top');
echo $new_form->Start_form();
echo $new_form->Field_form('text', 'username', 'votre nom', TRUE, 'login');
echo $new_form->Field_form('password', 'password', 'mot de passe', TRUE, 'mot de passe');
echo $new_form->Field_form('date', 'date de naissance', 'votre date de naissance', TRUE, 'date de naissance');
echo $new_form->End_form();
?>
<?php
/**
* Create_form
* Classe permettant la création de formulaire
* Formation POO
*/
class Create_form
{
public string $Form_name = 'formulaire';
public string $Form_action = 'verification.php';
public string $Form_method = 'post';
public string $Form_field_tag_surround = 'p';
public string $Form_field_label_position = 'before';
public function __construct($Form_name, $Form_action, $Form_method, $Form_field_tag_surround, $Form_field_label_position)
{
$this->Form_name = $Form_name;
$this->Form_action = $Form_action;
$this->Form_method = $Form_method;
$this->Form_field_tag_surround = $Form_field_tag_surround;
$this->Form_field_label_position = $Form_field_label_position;
}
private function surround($html)
{
return "<{$this->Form_field_tag_surround}>{$html}</{$this->Form_field_tag_surround}>";
}
/**
* Start_form
*
* @param mixed $Form_name
* @param mixed $Form_action
* @param mixed $Form_method
* @return void
*/
public function Start_form()
{
return '<form name="' . $this->Form_name . '" action="' . $this->Form_action . '"method="' . $this->Form_method . '">';
}
/**
* End_form
*
* @return void
*/
public function End_form()
{
return '</form>';
}
public function Field_form(string $Field_type = 'text', string $Field_name = '', string $Field_placeholder = '', bool $Field_required = TRUE, $Field_label = 'label')
{
$field_form_temp = '';
$field_form_label_top = '';
if ($this->Form_field_label_position === 'top') {
$field_form_label_top .= '<' . $this->Form_field_tag_surround . '><label for="' . $this->Form_name . '">' . $Field_label . '</label></' . $this->Form_field_tag_surround . '>';
}
if ($this->Form_field_label_position === 'before') {
$field_form_temp .= '<label for="' . $this->Form_name . '"> ' . $Field_label . '</label>';
}
$field_form_temp .= '<input type="' . $Field_type . '" name="' . $Field_name . '"';
$field_form_temp .= 'placeholder="' . $Field_placeholder . '"';
if ($Field_required === TRUE) {
$field_form_temp .= ' required';
}
$field_form_temp .= '>';
if ($this->Form_field_label_position === 'after') {
$field_form_temp .= '<label for="' . $this->Form_name . '"> ' . $Field_label . '</label>';
}
return $field_form_label_top . $this->surround($field_form_temp);
}
}
Ce que je souhaiterai
La méthode $surround($html) entoure les fields du formulaire avec la balise définie par $Form_field_tag_surround
J'aimerai réussir à faire la même chose avec les labels pour les placer devant, après ou en haut du fied. J'arrive à le faire dans la méthode $Field_form et Form_field_label_position mais je pense qu'il y a moyen de faire plus propre et plus efficace.
Merci.