voici mon petit souci : j'ai une class pour mes input et un formulaire pour mes posts et mes comments :
je souhaite que mon champs SLUG ne soit pas modifiable !!!
<form method="POST" enctype="multipart/form-data">
<?= $form->input('name', 'Titre'); ?>
<?= $form->input('slug', 'Slug'); ?>
<?= $form->input('chapo', 'Chapo'); ?>
<div class="row">
<div class="col-md-9">
<?= $form->file('image', 'Image à la une'); ?>
</div>
<div class="col-md-3">
<?php if ($post->getImage()) : ?>
<img src="<?= $post->getImageURL('small') ?>" alt="" style="width: 100%;">
<?php endif ?>
</div>
</div>
<?= $form->textarea('content', 'Contenu'); ?>
<?= $form->input('author', 'Auteur (e)'); ?>
<?= $form->input('created_at', 'Modifié ou crée le'); ?>
<div class="d-flex justify-content-center">
<button class="btn btn-secondary" type="submit">
<?php if ($post->getID() !== null) : ?>
Modifier
<?php else : ?>
Créer
<?php endif ?>
</button>
</div>
<br>
</form>
j'ai ma class :
class Form
{
private $data;
private $errors;
public function __construct($data, array $errors)
{
$this->data = $data;
$this->errors = $errors;
}
public function input(string $key, string $label): string
{
$value = $this->getValue($key);
$type = $key === "password" ? "password" : "text";
return <<<HTML
<div class="form-group">
<label for="field{$key}">{$label}</label>
<input type="{$type}" id="field{$key}" class="{$this->getInputClass($key)}" name="{$key}" value="{$value}" required>
{$this->getErrorFeedback($key)}
</div>
HTML;
}
public function file(string $key, string $label): string
{
return <<<HTML
<div class="form-group">
<label for="field{$key}">{$label}</label>
<input type="file" id="field{$key}" class="{$this->getInputClass($key)}" name="{$key}">
{$this->getErrorFeedback($key)}
</div>
HTML;
}
public function textarea(string $key, string $label): string
{
$value = $this->getValue($key);
return <<<HTML
<div class="form-group">
<label for="field{$key}">{$label}</label>
<textarea type="text" id="field{$key}" class="{$this->getInputClass($key)}" name="{$key}">{$value}</textarea>
{$this->getErrorFeedback($key)}
</div>
HTML;
}
public function select(string $key, string $label, array $options = []): string
{
$optionsHTML = [];
$value = $this->getValue($key);
foreach ($options as $k => $v) {
$selected = in_array($k, $value) ? " selected" : "";
$optionsHTML[] = "<option value=\"$k\"$selected>$v</option>";
}
$optionsHTML = implode('', $optionsHTML);
return <<<HTML
<div class="form-group">
<label for="field{$key}">{$label}</label>
<select id="field{$key}" class="{$this->getInputClass($key)}" name="{$key}[]" required multiple>{$optionsHTML}</select>
{$this->getErrorFeedback($key)}
</div>
HTML;
}
private function getValue(string $key)
{
if (is_array($this->data)) {
return $this->data[$key] ?? null;
}
$method = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
$value = $this->data->$method();
if ($value instanceof \DateTimeInterface) {
return $value->format('Y-m-d H:i:s');
}
return $value;
}
private function getInputClass(string $key): string
{
$inputClass = 'form-control';
if (isset($this->errors[$key])) {
$inputClass .= ' is-invalid';
}
return $inputClass;
}
private function getErrorFeedback(string $key): string
{
if (isset($this->errors[$key])) {
if (is_array($this->errors[$key])) {
$error = implode(' <br>', $this->errors[$key]);
} else {
$error = $this->errors[$key];
}
return '<div class="invalid-feedback">' . $error . '</div>';
}
return '';
}
}
Alors malgré mes recherches et je l'avoue ca aurait ete bien plus simple sans la class, j'aimerai que mon champ SLUG ne puisse pas etre modifier ! alors que ce soit hidden, disable !!! mais ou je peux inserer ce code?
merci pour votre aide
merci