new form type to select a daytime in system-configuration (#1895)

This commit is contained in:
Kevin Papst
2020-08-17 00:18:51 +02:00
committed by GitHub
parent 4200208c7b
commit 661c3897b0
8 changed files with 230 additions and 17 deletions

View File

@@ -15,6 +15,7 @@ use App\Form\Model\Configuration;
use App\Form\Model\SystemConfiguration as SystemConfigurationModel;
use App\Form\SystemConfigurationForm;
use App\Form\Type\DateTimeTextType;
use App\Form\Type\DayTimeType;
use App\Form\Type\LanguageType;
use App\Form\Type\RoundingModeType;
use App\Form\Type\SkinType;
@@ -23,6 +24,7 @@ use App\Form\Type\WeekDaysType;
use App\Form\Type\YesNoType;
use App\Repository\ConfigurationRepository;
use App\Validator\Constraints\DateTimeFormat;
use App\Validator\Constraints\TimeFormat;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
@@ -34,8 +36,8 @@ use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Regex;
@@ -136,15 +138,19 @@ final class SystemConfigurationController extends AbstractController
$form = $this->createConfigurationsForm($configModel);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$this->repository->saveSystemConfiguration($form->getData());
$this->flashSuccess('action.update.success');
} catch (\Exception $ex) {
$this->flashError('action.update.error', ['%reason%' => $ex->getMessage()]);
}
if ($form->isSubmitted()) {
if ($form->isValid()) {
try {
$this->repository->saveSystemConfiguration($form->getData());
$this->flashSuccess('action.update.success');
} catch (\Exception $ex) {
$this->flashError('action.update.error', ['%reason%' => $ex->getMessage()]);
}
return $this->redirectToRoute('system_configuration');
return $this->redirectToRoute('system_configuration');
} else {
$this->flashError('action.update.error', ['%reason%' => 'Validation problem']);
}
}
$configSettings = $this->getInitializedConfigurations();
@@ -393,23 +399,23 @@ final class SystemConfigurationController extends AbstractController
(new Configuration())
->setName('calendar.businessHours.begin')
->setTranslationDomain('system-configuration')
->setType(TextType::class)
->setConstraints([new DateTime(['format' => 'H:i']), new NotNull()]),
->setType(DayTimeType::class)
->setConstraints([new NotBlank(), new TimeFormat()]),
(new Configuration())
->setName('calendar.businessHours.end')
->setTranslationDomain('system-configuration')
->setType(TextType::class)
->setConstraints([new DateTime(['format' => 'H:i']), new NotNull()]),
->setType(DayTimeType::class)
->setConstraints([new NotBlank(), new TimeFormat()]),
(new Configuration())
->setName('calendar.visibleHours.begin')
->setTranslationDomain('system-configuration')
->setType(TextType::class)
->setConstraints([new DateTime(['format' => 'H:i']), new NotNull()]),
->setType(DayTimeType::class)
->setConstraints([new NotBlank(), new TimeFormat()]),
(new Configuration())
->setName('calendar.visibleHours.end')
->setTranslationDomain('system-configuration')
->setType(TextType::class)
->setConstraints([new DateTime(['format' => 'H:i']), new NotNull()]),
->setType(DayTimeType::class)
->setConstraints([new NotBlank(), new TimeFormat()]),
(new Configuration())
->setName('calendar.slot_duration')
->setTranslationDomain('system-configuration')

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DayTimeType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'attr' => [
'placeholder' => 'hh:mm'
],
]);
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return TextType::class;
}
}

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
class TimeFormat extends Constraint
{
public const INVALID_FORMAT = 'kimai-time-00';
protected static $errorNames = [
self::INVALID_FORMAT => 'The given value is not a valid time.',
];
public $message = 'This time format is invalid.';
public function getTargets()
{
return self::PROPERTY_CONSTRAINT;
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
class TimeFormatValidator extends ConstraintValidator
{
/**
* @param string|mixed $value
* @param Constraint $constraint
*/
public function validate($value, Constraint $constraint)
{
if (!($constraint instanceof TimeFormat)) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\TimeFormat');
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;
if (preg_match('/^([01][0-9]|2[0-3]):([0-5][0-9])$/', $value) !== 1) {
$this->context->buildViolation('The given value is not a valid time.')
->setTranslationDomain('validators')
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(TimeFormat::INVALID_FORMAT)
->addViolation();
}
}
}

View File

@@ -353,8 +353,10 @@ class SystemConfigurationControllerTest extends ControllerBaseTest
[
'#system_configuration_form_calendar_configuration_2_value',
'#system_configuration_form_calendar_configuration_3_value',
'#system_configuration_form_calendar_configuration_3_value',
'#system_configuration_form_calendar_configuration_4_value',
'#system_configuration_form_calendar_configuration_5_value',
'#system_configuration_form_calendar_configuration_5_value',
],
true
);

View File

@@ -0,0 +1,89 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Validator\Constraints;
use App\Validator\Constraints\TimeFormat;
use App\Validator\Constraints\TimeFormatValidator;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/**
* @covers \App\Validator\Constraints\TimeFormatValidator
*/
class TimeFormatValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator()
{
return new TimeFormatValidator();
}
public function testConstraintIsInvalid()
{
$this->expectException(UnexpectedTypeException::class);
$this->validator->validate('foo', new NotBlank());
}
public function testWrongValueThrowsException()
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Expected argument of type "string", "stdClass" given');
$this->validator->validate(new \stdClass(), new TimeFormat());
}
/**
* @dataProvider getValidTimes
*/
public function testValidationSucceeds(string $value)
{
$this->validator->validate($value, new TimeFormat());
$this->assertNoViolation();
}
public function getValidTimes()
{
return [
['00:00'],
['00:01'],
['23:00'],
['23:10'],
['23:01'],
['23:59'],
];
}
/**
* @dataProvider getInvalidTimes
*/
public function testValidationProblem(string $value)
{
$this->validator->validate($value, new TimeFormat());
$this->buildViolation('The given value is not a valid time.')
->setParameter('{{ value }}', '"' . $value . '"')
->setCode(TimeFormat::INVALID_FORMAT)
->assertRaised();
}
public function getInvalidTimes()
{
return [
['1:00'],
['01:1'],
['00:60'],
['23:60'],
['23:1'],
['24:00'],
];
}
}

View File

@@ -30,6 +30,10 @@
<source>You already have an entry for this time.</source>
<target>Es existiert bereits ein Eintrag für diesen Zeitpunkt.</target>
</trans-unit>
<trans-unit id="The given value is not a valid time.">
<source>The given value is not a valid time.</source>
<target>Der eingetragene Wert ist keine gültige Uhrzeit.</target>
</trans-unit>
</body>
</file>
</xliff>

View File

@@ -30,6 +30,10 @@
<source>You already have an entry for this time.</source>
<target>You already have an entry for this time.</target>
</trans-unit>
<trans-unit id="The given value is not a valid time.">
<source>The given value is not a valid time.</source>
<target>The given value is not a valid time.</target>
</trans-unit>
</body>
</file>
</xliff>