Bonjour,
Ayant d'afficher les données de ma basse de donnée Mysql dans un formulaire pour les administrer et le modifier en Back-end. Je récupère un objet. que je passe dans un BoostrapForm. Et rien ne s'affiche... J'ai tenté foreach, de même. Voici mon code
<?php
use Core\HTML\BootstrapForm;
use App\App;
$post = App::getInstance()->getTable('Post')->find($_GET['id']);
$form = new BootstrapForm($post);
var_dump($post);
var_dump($form);
?>
<form method="post">
<?= $form-> input('title', 'Titre de l\'article'); ?>
<?= $form-> input('content', 'Contenu', ['type' => 'textarea']); ?>
<button class="btn btn-primary">Sauvegarder</button>
</form>
voici le resultat :
var_dump($post);
C:\wamp64\www\BloG\pages\admin\posts\edit.php:6:
array (size=1)
0 =>
object(App\Entity\PostEntity)[8]
public 'title' => string 'MON_TITRE' (length=9)
public 'content' => string ' Contenu de l'article..blablablablablablablablablablabla...
public 'date' => string '2016-07-27 16:51:12' (length=19)
public 'id' => string '1' (length=1)
public 'categories' => string 'Picine' (length=6)
var_dump($form);
C:\wamp64\www\BloG\pages\admin\posts\edit.php:7:
object(Core\HTML\BootstrapForm)[5]
private 'data' (Core\HTML\Form) =>
array (size=1)
0 =>
object(App\Entity\PostEntity)[8]
public 'title' => string 'MON_TITRE' (length=9)
public 'content' => string ' Contenu de l'article...blablablablablablablablablabla...
public 'date' => string '2016-07-27 16:51:12' (length=19)
public 'id' => string '1' (length=1)
public 'categories' => string 'Picine' (length=6)
public 'surround' => string 'p' (length=1)
A l'écran :
Titre de l'article
==> formulaire vide <==
Contenu
==> formulaire vide <==
Enfin voici le fichier BootstrapForm :
<?php
namespace Core\HTML;
use Core\HTML\Form;
class BootstrapForm extends Form{
protected function surround($html){
return "<div class=\"form-group\">{$html}</div>";
}
public function input($name,$label,$options = []){
var_dump ($name);
$type = isset($options['type']) ? $options['type'] : 'text';
$label = '<label>' . $label . '</label>';
if ($type === 'textarea'){
$input = '<textarea name="'. $name .'" class="form-control">' . $this->getValue($name) . '</textarea>';
} else {
$input = '<input type="' . $type . '" name="' . $name . '" value="' . $this->getValue($name) . '" class="form-control">';
}
return $this->surround($label . $input);
}
public function submit(){
return $this->surround('<button type="submit" class="btn btn-primary">Envoyer</button>');
}
}
?>
Et le Form
<?php
namespace Core\HTML;
class Form{
private $data;
public $surround = 'p';
public function __construct($data = array()){
$this->data = $data;
}
protected function surround($html){
return "<{$this->surround}>{$html}</{$this->surround}>";
}
protected function getValue($index){
if(is_object($this->data)){
return ($this->data->$index);
}
return isset($this->data[$index]) ? $this->data[$index] : null;
}
public function input($name, $label, $options = []){
var_dump($name);
$type = isset($options['type']) ? $options['type'] : 'text' ;
return $this->surround(
'<input type="'.$type.'"name="'.$name.'"value="'.$this->getValue($name).'">'
);
}
public function submit(){
return $this->surround('<button type="submit">Envoyer</button>');
}
}
?>
Preneur de toute piste de réflexion, je ne sais plus trop où chercher. Ayant eu le même problème dans un autre exercice, un foreach avait réglé le problème dans ce cas précis, non : dans mon $form, le dernier élément n'ai pas un objet donc ça ne marche pas. Les vidéos ne montrent aucunes de ces problèmatiques, mais peut-être y-a-til eu une mise à jour depuis, ou bien, ai-je loupé une étape dans une classe...(très probable ^^) ? Où dois-je chercher, les var-dump() n'avance pas mon chmilblic?
Merci d'avance!