Add validation for repeating events.

Ticket #43
This commit is contained in:
Tim Schumacher 2015-05-09 19:43:03 +02:00
parent 6f5ca2d29a
commit 60de4df4e6
3 changed files with 40 additions and 20 deletions

View file

@ -75,7 +75,8 @@ class RepeatingEventController extends Controller
{
$entity = new RepeatingEvent();
$this->fillEntity($request, $entity);
if ($this->validateRepeatingEvent($entity)) {
$errors = $entity->isValid();
if (count($errors) == 0) {
$ret = $this->saveRepeatingEvent($request, $entity);
if ($entity->id > 0) {
return $this->redirect($this->generateUrl('repeating_event_show'));
@ -85,6 +86,7 @@ class RepeatingEventController extends Controller
}
return [
'entity' => $entity,
'errors' => $errors,
];
}
@ -139,7 +141,8 @@ class RepeatingEventController extends Controller
}
$this->fillEntity($request, $entity);
if ($this->validateRepeatingEvent($entity)) {
$errors = $entity->isValid();
if (count($errors) == 0) {
$ret = $this->saveRepeatingEvent($request, $entity);
if ($entity->id > 0) {
return $this->redirect($this->generateUrl('repeating_event_show'));
@ -149,6 +152,7 @@ class RepeatingEventController extends Controller
}
return [
'entity' => $entity,
'errors' => $errors,
];
}
@ -172,20 +176,6 @@ class RepeatingEventController extends Controller
}
private function validateRepeatingEvent(RepeatingEvent $entity)
{
$fields = [
'nextdate',
'repeating_pattern',
'summary',
];
foreach ($fields as $field) {
if ((is_null($entity->$field)) && (strlen($entity->$field) > 0))
return false;
}
return true;
}
private function saveRepeatingEvent(Request $request, RepeatingEvent $entity)
{
$location = $request->get('location');

View file

@ -4,6 +4,7 @@ namespace Hackspace\Bundle\CalciferBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\PersistentCollection;
use enko\RelativeDateParser\RelativeDateParser;
/**
* RepeatEvent
@ -84,4 +85,25 @@ class RepeatingEvent extends BaseEntity
* )
*/
protected $tags = [];
public function isValid() {
$errors = [];
if ((is_null($this->nextdate)) && (strlen($this->nextdate) > 0)) {
$errors['nextdate'] = "Bitte gebe einen nächsten Termin an.";
}
if ((is_null($this->repeating_pattern)) && (strlen($this->repeating_pattern) > 0)) {
$errors['repeating_pattern'] = "Bitte gebe ein gültiges Wiederholungsmuster an.";
} else {
$this->nextdate->setTimezone(new \DateTimeZone('Europe/Berlin'));
try {
$parser = new RelativeDateParser($this->repeating_pattern,$this->nextdate,'de');
} catch (\Exception $e) {
$errors['repeating_pattern'] = "Bitte gebe ein gültiges Wiederholungsmuster an.";
}
}
if ((is_null($this->summary)) && (strlen($this->summary) > 0)) {
$errors['summary'] = "Bitte gebe eine Zusammenfassung an.";
}
return $errors;
}
}

View file

@ -1,6 +1,6 @@
<form class="ui form" method="post"
action="{% if entity.id|default(0) > 0 %}{{ path('repeating_event_update',{'slug':entity.slug}) }}{% else %}{{ path('repeating_event_create') }}{% endif %}">
<div class="required field">
<div class="required field{% if(errors|default('0') != 0) %} {% if('nextdate' in errors|keys) %}error{% endif %}{% endif %}">
<label for="event_startdate">Nächster Termin</label>
<div class="ui icon input">
@ -11,7 +11,9 @@
value="{{ entity.nextdate.format('Y-m-d H:i')|default('') }}"
placeholder="{{ "now"|date('d.m.Y H:00') }}"
class="form-control">
{% if(errors|default('0') != 0) %} {% if('nextdate' in errors|keys) %}
<div class="ui red pointing above ui label">{{ errors.nextdate }}</div>
{% endif %}{% endif %}
<i class="icon calendar"></i>
</div>
@ -37,7 +39,7 @@
</div>
</div>
<div class="required field">
<div class="required field{% if(errors|default('0') != 0) %} {% if('repeating_pattern' in errors|keys) %}error{% endif %}{% endif %}">
<label for="event_duration">Terminwiederholungsmuster</label>
<div class="ui input">
@ -48,6 +50,9 @@
required="required"
maxlength="255"
class="form-control">
{% if(errors|default('0') != 0) %} {% if('repeating_pattern' in errors|keys) %}
<div class="ui red pointing above ui label">{{ errors.repeating_pattern }}</div>
{% endif %}{% endif %}
</div>
<div class="ui label">
@ -56,7 +61,7 @@
</div>
<div class="required field">
<div class="required field{% if(errors|default('0') != 0) %} {% if('summary' in errors|keys) %}error{% endif %}{% endif %}">
<label for="event_summary">Zusammenfassung</label>
<div class="ui input">
@ -67,6 +72,9 @@
required="required"
maxlength="255"
class="form-control">
{% if(errors|default('0') != 0) %} {% if('summary' in errors|keys) %}
<div class="ui red pointing above ui label">{{ errors.summary }}</div>
{% endif %}{% endif %}
</div>
</div>