Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forms): Ensure that the Number question type allows a floating value #18748

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Glpi/Form/QuestionType/AbstractQuestionTypeShortAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public function getFormEditorJsOptions(): string
JS;
}

/**
* Provide additional attributes for the input field
*
* @return array
*/
public function getInputAttributes(): array
{
return [];
}

#[Override]
public function renderAdministrationTemplate(?Question $question): string
{
Expand All @@ -103,6 +113,7 @@ class="form-control"
placeholder="{{ input_placeholder }}"
value="{{ question is not null ? question.fields.default_value : '' }}"
aria-label="{{ __('Default value') }}"
{{ attributes|map((value, key) => '%s=%s'|format(key, value))|join(' ') }}
ccailly marked this conversation as resolved.
Show resolved Hide resolved
/>
TWIG;

Expand All @@ -111,6 +122,7 @@ class="form-control"
'question' => $question,
'input_type' => $this->getInputType(),
'input_placeholder' => $this->getName(),
'attributes' => $this->getInputAttributes(),
]);
}

Expand All @@ -126,6 +138,7 @@ class="form-control"
value="{{ question.fields.default_value }}"
aria-label="{{ label }}"
{{ question.fields.is_mandatory ? 'required' : '' }}
{{ attributes|map((value, key) => '%s=%s'|format(key, value))|join(' ') }}
ccailly marked this conversation as resolved.
Show resolved Hide resolved
>
TWIG;

Expand All @@ -134,6 +147,7 @@ class="form-control"
'question' => $question,
'input_type' => $this->getInputType(),
'label' => $question->fields['name'],
'attributes' => $this->getInputAttributes(),
]);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Glpi/Form/QuestionType/QuestionTypeNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ public function getWeight(): int
{
return 30;
}

#[Override]
public function getInputAttributes(): array
{
return ['step' => 'any'];
}
}
96 changes: 96 additions & 0 deletions tests/cypress/e2e/form/question_types/number.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
ccailly marked this conversation as resolved.
Show resolved Hide resolved
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

describe('Number form question type', () => {
beforeEach(() => {
cy.createWithAPI('Glpi\\Form\\Form', {
'name': 'Tests form for the number form question type suite',
}).as('form_id');

cy.login();
cy.changeProfile('Super-Admin');

cy.get('@form_id').then((form_id) => {
const tab = 'Glpi\\Form\\Form$main';
cy.visit(`/front/form/form.form.php?id=${form_id}&forcetab=${tab}`);

// Add a new question
cy.findByRole("button", { name: "Add a new question" }).should('exist').click();

// Set the question name
cy.findByRole("textbox", { name: "Question name" }).should('exist').type("Test number question");

// Change question type
cy.getDropdownByLabelText('Question type').selectDropdownValue('Short answer');

// Change question sub type
cy.getDropdownByLabelText('Question sub type').selectDropdownValue('Number');
});
});

const testDefaultValue = (value) => {
// Define default value
cy.findByRole('spinbutton', { name: 'Default value' }).should('exist').type(value);

// Save
cy.findByRole('button', { name: 'Save' }).click();

// Reload the page
cy.reload();

// Check the default value
cy.findByRole('spinbutton', { name: 'Default value' }).should('have.value', value);

// Go to preview page (remove the target="_blank" attribute to stay in the same window)
cy.findByRole("link", { name: "Preview" })
.invoke('attr', 'target', '_self')
.click();

// Check the default value in the preview page
cy.findByRole('spinbutton', { name: 'Test number question' }).should('have.value', value);

// Send form
cy.findByRole('button', { name: 'Send form' }).click();

// Check the form was submitted
cy.checkAndCloseAlert('Item successfully created');
};

it('should be able to define an integer as default value', () => {
testDefaultValue('42');
});

it('should be able to define a float as default value', () => {
testDefaultValue('3.14');
});
});
Loading