From 0a0fd1e8b034687e6e140490a9efc743b671f83a Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Thu, 25 Sep 2014 23:18:45 +0200 Subject: [PATCH] Add some basic validation. --- .../Controller/EventController.php | 17 +++++++++++------ .../Bundle/CalciferBundle/Entity/BaseEntity.php | 6 +++++- .../Bundle/CalciferBundle/Entity/Event.php | 14 +++++++++++++- .../Resources/views/Event/event_form.html.twig | 17 ++++++++++++++--- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/Hackspace/Bundle/CalciferBundle/Controller/EventController.php b/src/Hackspace/Bundle/CalciferBundle/Controller/EventController.php index b999599..78bfb9e 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Controller/EventController.php +++ b/src/Hackspace/Bundle/CalciferBundle/Controller/EventController.php @@ -63,7 +63,8 @@ class EventController extends Controller $em = $this->saveEvent($request, $entity); - if ($entity->isValid()) { + $errors = $entity->isValid(); + if ($errors === true) { $em = $this->getDoctrine()->getManager(); $em->persist($entity); $em->flush(); @@ -73,6 +74,7 @@ class EventController extends Controller return array( 'entity' => $entity, + 'errors' => $errors, ); } @@ -171,7 +173,8 @@ class EventController extends Controller $em = $this->saveEvent($request, $entity); - if ($entity->isValid()) { + $errors = $entity->isValid(); + if ($errors === true) { $em = $this->getDoctrine()->getManager(); $em->persist($entity); $em->flush(); @@ -180,7 +183,8 @@ class EventController extends Controller } return array( - 'entity' => $entity, + 'entity' => $entity, + 'errors' => $errors, ); } @@ -199,8 +203,10 @@ class EventController extends Controller $entity->summary = $request->get('summary'); $entity->url = $request->get('url'); $startdate = $request->get('startdate'); - $startdate = new \DateTime($startdate); - $entity->startdate = $startdate; + if (strlen($startdate) > 0) { + $startdate = new \DateTime($startdate); + $entity->startdate = $startdate; + } $entity->slug = $entity->generateSlug($entity->summary,$em); $enddate = $request->get('enddate'); @@ -267,7 +273,6 @@ class EventController extends Controller $entity->addTag($tag_obj); } } - return $em; } return $em; } diff --git a/src/Hackspace/Bundle/CalciferBundle/Entity/BaseEntity.php b/src/Hackspace/Bundle/CalciferBundle/Entity/BaseEntity.php index a33a608..ba972ee 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Entity/BaseEntity.php +++ b/src/Hackspace/Bundle/CalciferBundle/Entity/BaseEntity.php @@ -56,7 +56,11 @@ abstract class BaseEntity { public function __set($name,$value) { if (property_exists($this,$name)) { - $this->$name = $value; + if ($value == '') { + $this->$name = null; + } else { + $this->$name = $value; + } return $this; } else { throw new \Exception("Property {$name} does not Exists"); diff --git a/src/Hackspace/Bundle/CalciferBundle/Entity/Event.php b/src/Hackspace/Bundle/CalciferBundle/Entity/Event.php index 8a3a791..68104d4 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Entity/Event.php +++ b/src/Hackspace/Bundle/CalciferBundle/Entity/Event.php @@ -3,6 +3,7 @@ namespace Hackspace\Bundle\CalciferBundle\Entity; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Validator\Constraints\DateTime; /** * Event @@ -102,7 +103,18 @@ class Event extends BaseEntity } public function isValid() { - return true; + $errors = []; + if (!($this->startdate instanceof \DateTime)) { + $errors['startdate'] = 'Bitte gebe ein Startdatum ein.'; + } + if ((!is_null($this->startdate)) && (!is_null($this->enddate)) && ($this->enddate < $this->startdate)) { + $errors['enddate'] = 'Bitte setze ein Enddatum das nach dem Startdatum ist.'; + } + if (strlen($this->summary) == 0) { + $errors['summary'] = 'Bitte gebe eine Zusammenfassung an.'; + } + + return (count($errors) > 0) ? $errors : true; } public function getFormatedDate() { diff --git a/src/Hackspace/Bundle/CalciferBundle/Resources/views/Event/event_form.html.twig b/src/Hackspace/Bundle/CalciferBundle/Resources/views/Event/event_form.html.twig index d2f1e82..39ac995 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Resources/views/Event/event_form.html.twig +++ b/src/Hackspace/Bundle/CalciferBundle/Resources/views/Event/event_form.html.twig @@ -1,6 +1,6 @@
-
+
@@ -11,6 +11,9 @@ value="{{ entity.startdate.format('Y-m-d H:i')|default('') }}" placeholder="{{ "now"|date('Y-m-d H:i') }}" class="form-control"> + {% if(errors|default('0') != 0) %} {% if('startdate' in errors|keys) %} +
{{ errors.startdate }}
+ {% endif %}{% endif %} @@ -19,7 +22,7 @@
-
+
@@ -30,10 +33,14 @@ placeholder="{{ "now"|date('Y-m-d H:i') }}" class="form-control"> + {% if(errors|default('0') != 0) %} {% if('enddate' in errors|keys) %} +
{{ errors.enddate }}
+ {% endif %}{% endif %} +
-
+
@@ -45,6 +52,10 @@ maxlength="255" class="form-control"> + {% if(errors|default('0') != 0) %} {% if('summary' in errors|keys) %} +
{{ errors.summary }}
+ {% endif %}{% endif %} +