Commit 3476cc5c authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Formulaire multipage : améliorations et refactorisations

parent 576dc5db
Pipeline #6911 passed with stage
in 21 seconds
......@@ -214,9 +214,10 @@ class MultipageFormPlugin extends AbstractPlugin
$postData = $this->getPostDataForCurrentFieldset();
$submitAction = $this->getSubmitAction($postData);
$currentFieldset = $this->getCurrentFieldset();
$currentFieldset->populateValues($this->getSessionValues($currentFieldset->getName()));
if ($submitAction === false) {
$currentFieldset->populateValues($this->getSessionValues($currentFieldset->getName()));
return [
'fieldset' => $currentFieldset,
'stepIndex' => $this->getCurrentStepIndex(),
......@@ -651,8 +652,8 @@ class MultipageFormPlugin extends AbstractPlugin
{
$formData = [];
foreach ($this->fieldsetActions as $formName => $action) {
$formData[$formName] = $this->getSessionValues($formName);
foreach ($this->fieldsetActions as $fieldsetName => $action) {
$formData[$fieldsetName] = $this->getSessionValues($fieldsetName);
}
return $formData;
......
......@@ -478,7 +478,7 @@ abstract class MultipageForm extends Form
$valueToString = $value;
if ($element instanceof SearchAndSelect) {
$valueToString = $value['label'] ?? $value['id'] ?? implode(' - ', $value);
$valueToString = $value['label'] ?? $value['id'] ?? implode(' - ', (array)$value);
}
elseif ($element instanceof DoctrineObjectSelect) {
foreach ($element->getValueOptions() as $key => $optionArray) {
......
......@@ -84,29 +84,24 @@ class MultipageFormFieldset extends AbstractHelper
}
/**
* Génère le code HTML.
* Génère le code HTML du fieldset à l'intérieur d'une balise "form".
*
* @return string
* @return string
*/
public function render()
{
$fieldset = $this->fieldset;
$template = '<form method="POST"><fieldset><legend>%s</legend>%s</fieldset></form>';
$template = <<<EOS
<form method="POST">
%s
</form>
EOS;
$label = $fieldset->getLabel();
$step = "Étape";
$on = "sur";
if (($translator = $this->getTranslator()) !== null) {
$label = $translator->translate($label, $this->getTranslatorTextDomain());
$step = $translator->translate($step, $this->getTranslatorTextDomain());
$on = $translator->translate($on, $this->getTranslatorTextDomain());
}
$html = sprintf($template, $label, $this->renderFieldset($fieldset));
$step = $this->translate("Étape");
$on = $this->translate("sur");
$html = sprintf($template, $this->renderFieldset($fieldset));
if ($stepIndex = $this->view->get('stepIndex')) {
$step .= " " . $stepIndex;
......@@ -120,26 +115,50 @@ class MultipageFormFieldset extends AbstractHelper
}
/**
* Génère le code HTML du fieldset seul.
* Si un partial est fourni, il est utilisé.
*
* @param Fieldset $fieldset
* @return string
*/
private function renderFieldset(Fieldset $fieldset)
{
$html = '';
if ($this->partial) {
// fieldset
$html .= $this->view->partial($this->partial);
// navigation
$navElement = $this->getNavigationElement();
$html .= $this->view->multipageFormNav($navElement);
} else {
/** @var Element $element */
foreach ($fieldset->getIterator() as $element) {
$html .= $this->view->multipageFormRow($element);
}
return $this->renderFieldsetPartial();
}
$template = <<<EOS
<fieldset>
<legend>%s</legend>
%s
</fieldset>
EOS;
/** @var Element $element */
$html = '';
foreach ($fieldset->getIterator() as $element) {
$html .= $this->view->multipageFormRow($element);
}
$legend = $this->translate($fieldset->getLabel());
return sprintf($template, $legend, $html);
}
/**
* Génère le code HTML du fieldset à l'aide du partial fourni.
*
* @return string
*/
protected function renderFieldsetPartial()
{
// fieldset
$html = $this->view->partial($this->partial);
// navigation
$navElement = $this->getNavigationElement();
$html .= $this->view->multipageFormNav($navElement);
return $html;
}
......@@ -155,4 +174,17 @@ class MultipageFormFieldset extends AbstractHelper
return $element;
}
/**
* @param string $text
* @return string
*/
protected function translate(string $text)
{
if (($translator = $this->getTranslator()) !== null) {
return $translator->translate($text, $this->getTranslatorTextDomain());
}
return $text;
}
}
\ No newline at end of file
......@@ -53,8 +53,11 @@ class MultipageFormRow extends FormRow
elseif ($element instanceof \UnicaenApp\Form\Element\DateInfSup) {
return $this->view->formRowDateInfSup($element);
}
return parent::render($element);
return
'<div class="form-group">' . PHP_EOL .
parent::render($element). PHP_EOL .
'</div>' . PHP_EOL ;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment