diff --git a/class/boolfield.php b/class/boolfield.php index c1407b92dbfff5350d4ba46fcdc5ed3eb992a25b..71d5bc20551a987d7e94126e06622333b6c4bd68 100644 --- a/class/boolfield.php +++ b/class/boolfield.php @@ -2,7 +2,7 @@ class BoolField extends Field { - public function html() { + public function html($form) { $html = '<div class = "form-group"> <label for = "form'.$this->name.'">'.ucfirst($this->name).'</label> <input type = "checkbox" class="form-control" name = "'.$this->name.'" value ="'.$this->value.'"> diff --git a/class/field.php b/class/field.php index 283fdbe60274a6330ae28832127fbfac0c243b31..f587932a04d4b25a012648a1befd237317ae6170 100644 --- a/class/field.php +++ b/class/field.php @@ -3,16 +3,24 @@ class Field { protected $name; protected $value; - public function __construct($name, &$value) { + protected $primary; + + public function __construct($name, &$value, $primary=false) { $this->name = $name; $this->value =& $value; + $this->primary = $primary; } - public function html() { + public function html($form) { // Todo, if isset($_POST[$this->name]) && !$this->validate() => Show a error msg explanation for this line. + $disabled = ""; + + if($this->primary && $form->action == "Ajouter") { return ""; } + if($this->primary && $form->action == "Modifier") { $disabled = "disabled"; } + return '<div class="form-group"> <label for="form'.$this->name.'">'.ucfirst($this->name).'</label> - <input type="text" class="form-control" name="'.$this->name.'" value="'.$this->value.'" > + <input type="text" class="form-control" name="'.$this->name.'" value="'.$this->value.'" '.$disabled.'> </div>'; } @@ -21,6 +29,11 @@ class Field { and return false, if data is not valid ! */ public function validate() { + // On ne peut pas changer la valeur de la clef primaire + if($this->primary && $this->value) { + return true; + } + $this->value = $_POST[$this->name]; return true; } diff --git a/class/form.php b/class/form.php index 0c836fbd1c3a36aa1ec9cbe5ec7b038c67b3bcf2..9b80ff3198b844c049756a001e4cf18b23b10920 100644 --- a/class/form.php +++ b/class/form.php @@ -2,7 +2,7 @@ class Form { protected $object; - protected $action; + public $action; protected $error = null; public function __construct(&$obj) { diff --git a/class/objet.php b/class/objet.php index 984c3e14627c2a49414bbafe001757c181183407..323d4fbaeedad131db0fe28112b11853867725fa 100644 --- a/class/objet.php +++ b/class/objet.php @@ -52,6 +52,7 @@ class Objet $ret = array(); foreach(array_keys(get_object_vars($this)) as $keyName) { if($keyName == "primaryAttr") { continue; } + if($keyName == $this->primaryAttr) { $ret[] = new Field($keyName, $this->$keyName, true); continue; } $ret[] = new Field($keyName, $this->$keyName); } return $ret; diff --git a/controller/animal.php b/controller/animal.php index 79858d4dab5f68f578da73bad536db071a18b2a0..efd52b93dbc37c2c1c63d7b8d5de3372ae2b7be4 100644 --- a/controller/animal.php +++ b/controller/animal.php @@ -18,7 +18,7 @@ switch($action) { "keys" => array("nom", "proprio", "race", "poids", "genre", "sterile", "dateNaissance", "dateDeces", "taille", "code")); include 'view/list.php'; break; - case "add": + case "addAnimal": $animal = new Animal(); $formConf = $animal->getForm(); include 'view/form.php'; diff --git a/view/form.php b/view/form.php index 2d0fc365e49b79d443efdf7bea706c88cc4d99a7..16d9945f830ffc7e2c5e578c6e73308a2ecd8eb3 100644 --- a/view/form.php +++ b/view/form.php @@ -7,7 +7,7 @@ <form role="form" method="post"> <input type="hidden" name="submitForm" value="1"> <?php foreach($formConf->getFields() as $field): ?> - <?php echo $field->html(); ?> + <?php echo $field->html($formConf); ?> <?php endforeach; ?> <button type="submit" class="btn btn-default"><?php echo $formConf->getSubmitText(); ?></button> </form>