Skip to content

Commit

Permalink
[Form] Fixed: If a DateField or TimeField is displayed with select bo…
Browse files Browse the repository at this point in the history
…xes, either all or no select box must have a value selected
  • Loading branch information
Bernhard Schussek authored and fabpot committed Feb 16, 2011
1 parent 40acc6a commit 14c3518
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/views/form.html.twig
Expand Up @@ -141,6 +141,8 @@

{% block date_time_field %}
{% spaceless %}
{{ form_errors(field.date) }}
{{ form_errors(field.time) }}
{{ form_field(field.date) }}
{{ form_field(field.time) }}
{% endspaceless %}
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Form/DateField.php
Expand Up @@ -294,4 +294,28 @@ public function isDayWithinRange()

return null === $date || in_array($date->format('d'), $this->getOption('days'));
}

/**
* Returns whether the field is neither completely filled (a selected
* value in each dropdown) nor completely empty
*
* @return Boolean
*/
public function isPartiallyFilled()
{
if ($this->isField()) {
return false;
}

if ($this->isEmpty()) {
return false;
}

if ($this->get('year')->isEmpty() || $this->get('month')->isEmpty()
|| $this->get('day')->isEmpty()) {
return true;
}

return false;
}
}
10 changes: 10 additions & 0 deletions src/Symfony/Component/Form/Resources/config/validation.xml
Expand Up @@ -43,6 +43,11 @@
</class>

<class name="Symfony\Component\Form\DateField">
<getter property="partiallyFilled">
<constraint name="AssertFalse">
<option name="message">The date is not fully selected</option>
</constraint>
</getter>
<getter property="yearWithinRange">
<constraint name="AssertTrue">
<option name="message">The year is invalid</option>
Expand All @@ -61,6 +66,11 @@
</class>

<class name="Symfony\Component\Form\TimeField">
<getter property="partiallyFilled">
<constraint name="AssertFalse">
<option name="message">The time is not fully selected</option>
</constraint>
</getter>
<getter property="hourWithinRange">
<constraint name="AssertTrue">
<option name="message">The hour is invalid</option>
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Form/TimeField.php
Expand Up @@ -227,4 +227,28 @@ public function isSecondWithinRange()

return null === $date || in_array($date->format('s'), $this->getOption('seconds'));
}

/**
* Returns whether the field is neither completely filled (a selected
* value in each dropdown) nor completely empty
*
* @return Boolean
*/
public function isPartiallyFilled()
{
if ($this->isField()) {
return false;
}

if ($this->isEmpty()) {
return false;
}

if ($this->get('hour')->isEmpty() || $this->get('minute')->isEmpty()
|| ($this->isWithSeconds() && $this->get('second')->isEmpty())) {
return true;
}

return false;
}
}
56 changes: 56 additions & 0 deletions tests/Symfony/Tests/Component/Form/DateFieldTest.php
Expand Up @@ -232,4 +232,60 @@ public function testIsDayWithinRange_returnsFalseIfNotContained()

$this->assertFalse($field->isDayWithinRange());
}

public function testIsPartiallyFilled_returnsFalseIfInput()
{
$field = new DateField('name', array(
'widget' => 'input',
));

$field->submit('7.6.2010');

$this->assertFalse($field->isPartiallyFilled());
}

public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyEmpty()
{
$field = new DateField('name', array(
'widget' => 'choice',
));

$field->submit(array(
'day' => '',
'month' => '',
'year' => '',
));

$this->assertFalse($field->isPartiallyFilled());
}

public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyFilled()
{
$field = new DateField('name', array(
'widget' => 'choice',
));

$field->submit(array(
'day' => '2',
'month' => '6',
'year' => '2010',
));

$this->assertFalse($field->isPartiallyFilled());
}

public function testIsPartiallyFilled_returnsTrueIfChoiceAndDayEmpty()
{
$field = new DateField('name', array(
'widget' => 'choice',
));

$field->submit(array(
'day' => '',
'month' => '6',
'year' => '2010',
));

$this->assertTrue($field->isPartiallyFilled());
}
}
108 changes: 108 additions & 0 deletions tests/Symfony/Tests/Component/Form/TimeFieldTest.php
Expand Up @@ -243,4 +243,112 @@ public function testIsSecondWithinRange_returnsFalseIfNotContained()

$this->assertFalse($field->isSecondWithinRange());
}

public function testIsPartiallyFilled_returnsFalseIfCompletelyEmpty()
{
$field = new TimeField('name', array(
'widget' => 'choice',
));

$field->submit(array(
'hour' => '',
'minute' => '',
));

$this->assertFalse($field->isPartiallyFilled());
}

public function testIsPartiallyFilled_returnsFalseIfCompletelyEmpty_withSeconds()
{
$field = new TimeField('name', array(
'widget' => 'choice',
'with_seconds' => true,
));

$field->submit(array(
'hour' => '',
'minute' => '',
'second' => '',
));

$this->assertFalse($field->isPartiallyFilled());
}

public function testIsPartiallyFilled_returnsFalseIfCompletelyFilled()
{
$field = new TimeField('name', array(
'widget' => 'choice',
));

$field->submit(array(
'hour' => '0',
'minute' => '0',
));

$this->assertFalse($field->isPartiallyFilled());
}

public function testIsPartiallyFilled_returnsFalseIfCompletelyFilled_withSeconds()
{
$field = new TimeField('name', array(
'widget' => 'choice',
'with_seconds' => true,
));

$field->submit(array(
'hour' => '0',
'minute' => '0',
'second' => '0',
));

$this->assertFalse($field->isPartiallyFilled());
}

public function testIsPartiallyFilled_returnsTrueIfChoiceAndHourEmpty()
{
$field = new TimeField('name', array(
'widget' => 'choice',
'with_seconds' => true,
));

$field->submit(array(
'hour' => '',
'minute' => '0',
'second' => '0',
));

$this->assertTrue($field->isPartiallyFilled());
}

public function testIsPartiallyFilled_returnsTrueIfChoiceAndMinuteEmpty()
{
$field = new TimeField('name', array(
'widget' => 'choice',
'with_seconds' => true,
));

$field->submit(array(
'hour' => '0',
'minute' => '',
'second' => '0',
));

$this->assertTrue($field->isPartiallyFilled());
}

public function testIsPartiallyFilled_returnsTrueIfChoiceAndSecondsEmpty()
{
$field = new TimeField('name', array(
'widget' => 'choice',
'with_seconds' => true,
));

$field->submit(array(
'hour' => '0',
'minute' => '0',
'second' => '',
));

$this->assertTrue($field->isPartiallyFilled());
}
}

0 comments on commit 14c3518

Please sign in to comment.