api formulaire quizz type radio
Bonjour, j'ai suivi un tuto sur la programmation objet et je l'adapte pour faire un quizz
Pour cela j'ai repris l'API dans le tp du tuto que je voudrai adapter en formulaire de type radio.
Dans le tuto, le champ name est associé à sa valeur dans la bibliothèque mysql par la méthode add dans Form comme ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11
|
public function add(Field $field)
{
// On récupère le nom du champ.
$attr = $field->name();
$field->setValue($this->entity->$attr()); // On assigne la valeur correspondante au champ.
$this->fields[] = $field; // On ajoute le champ passé en argument à la liste des champs.
return $this;
} |
Pour des champs de type text il n'y a pas de problème mais pour des champs de type radio, je peux définir qu'une seule et même valeur.
voici la class Field
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
<?php
namespace Library;
abstract class Field
{
protected $errorMessage;
protected $label;
protected $name;
protected $name2;
protected $validators = array();
protected $value;
protected $value_choix1;
public function __construct(array $options = array())
{
if (!empty($options))
{
$this->hydrate($options);
}
}
abstract public function buildWidget();
public function hydrate($options)
{
foreach ($options as $type => $value)
{
$method = 'set'.ucfirst($type);
if (is_callable(array($this, $method)))
{
$this->$method($value);
}
}
}
public function isValid()
{
foreach ($this->validators as $validator)
{
if (!$validator->isValid($this->value))
{
$this->errorMessage = $validator->errorMessage();
return false;
}
}
return true;
}
public function label()
{
return $this->label;
}
public function length()
{
return $this->length;
}
public function name()
{
return $this->name;
}
public function name2()
{
return $this->name2;
}
public function validators()
{
return $this->validators;
}
public function value()
{
return $this->value;
}
public function value_choix1()
{
return $this->value_choix1;
}
public function setLabel($label)
{
if (is_string($label))
{
$this->label = $label;
}
}
public function setLength($length)
{
$length = (int) $length;
if ($length > 0)
{
$this->length = $length;
}
}
public function setName($name)
{
if (is_string($name))
{
$this->name = $name;
}
}
public function setName2($name2)
{
if (is_string($name2))
{
$this->name2 = $name2;
}
}
public function setValidators(array $validators)
{
foreach ($validators as $validator)
{
if ($validator instanceof Validator && !in_array($validator, $this->validators))
{
$this->validators[] = $validator;
}
}
}
public function setValue($value)
{
if (is_string($value))
{
$this->value = $value;
}
}
public function setValue1($value_choix1)
{
if (is_string($value_choix1))
{
$this->value_choix1 = $value_choix1;
}
}
} |
La classe QuizzFormBuilder
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
<?php
namespace Library\FormBuilder;
class QuizzFormBuilder extends \Library\FormBuilder
{
public function build()
{
$this->form->add(new \Library\StringField(array(
'label' => 'Question',
'name' => 'question',
'maxLength' => 99,
'validators' => array(
new \Library\MaxLengthValidator('La question que vous avez proposé est trop longue', 100),
new \Library\NotNullValidator('Merci de spécifier votre question'),
),
)))
->add(new \Library\RadioField(array(
'label' => 'Propositions',
'name' => 'choix1',
'name2' => 'choix2',
'value2' => '5',
'value3' => '6',
'maxLength' => 49,
'validators' => array(
new \Library\MaxLengthValidator('Le choix1 que vous avez proposé est trop long', 50),
new \Library\NotNullValidator('Merci de spécifier votre choix1'),
),
)))
->add(new \Library\StringField(array(
'label' => 'Reponse',
'name' => 'reponse',
'maxLength' => 49,
'validators' => array(
new \Library\MaxLengthValidator('La reponse que vous avez proposé est trop longue', 50),
new \Library\NotNullValidator('Merci de spécifier votre reponse'),
),
)));
}
} |
et la classe RadioField
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
public function buildWidget()
{
$widget = '';
if (!empty($this->errorMessage))
{
$widget .= $this->errorMessage.'<br />';
}
$widget .= '
<label>'.$this->label.'</label>
<input type = "radio" name = "'.$this->name.'" value=""/> '.htmlspecialchars($this->value_choix1).' </br>
<input type = "radio" name = "'.$this->name.'" value=""/> '.htmlspecialchars($this->value).' </br>
<input type = "radio" name = "'.$this->name.'" value=""/> '.htmlspecialchars($this->value).'
';
return $widget;
} |
J'aimerai associer dans la fonction add() de la classe Form, name2 que j'ai définit dans Field, de la même maniere que name
Merci de m'éclairer