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 :
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
<?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<?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 RadioFieldpublic 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 nameMerci de m'éclairer