Add some basic validation.

This commit is contained in:
Tim Schumacher 2014-09-25 23:18:45 +02:00
parent 80182644bc
commit 0a0fd1e8b0
4 changed files with 43 additions and 11 deletions

View file

@ -63,7 +63,8 @@ class EventController extends Controller
$em = $this->saveEvent($request, $entity); $em = $this->saveEvent($request, $entity);
if ($entity->isValid()) { $errors = $entity->isValid();
if ($errors === true) {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$em->persist($entity); $em->persist($entity);
$em->flush(); $em->flush();
@ -73,6 +74,7 @@ class EventController extends Controller
return array( return array(
'entity' => $entity, 'entity' => $entity,
'errors' => $errors,
); );
} }
@ -171,7 +173,8 @@ class EventController extends Controller
$em = $this->saveEvent($request, $entity); $em = $this->saveEvent($request, $entity);
if ($entity->isValid()) { $errors = $entity->isValid();
if ($errors === true) {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$em->persist($entity); $em->persist($entity);
$em->flush(); $em->flush();
@ -181,6 +184,7 @@ class EventController extends Controller
return array( return array(
'entity' => $entity, 'entity' => $entity,
'errors' => $errors,
); );
} }
@ -199,8 +203,10 @@ class EventController extends Controller
$entity->summary = $request->get('summary'); $entity->summary = $request->get('summary');
$entity->url = $request->get('url'); $entity->url = $request->get('url');
$startdate = $request->get('startdate'); $startdate = $request->get('startdate');
if (strlen($startdate) > 0) {
$startdate = new \DateTime($startdate); $startdate = new \DateTime($startdate);
$entity->startdate = $startdate; $entity->startdate = $startdate;
}
$entity->slug = $entity->generateSlug($entity->summary,$em); $entity->slug = $entity->generateSlug($entity->summary,$em);
$enddate = $request->get('enddate'); $enddate = $request->get('enddate');
@ -267,7 +273,6 @@ class EventController extends Controller
$entity->addTag($tag_obj); $entity->addTag($tag_obj);
} }
} }
return $em;
} }
return $em; return $em;
} }

View file

@ -56,7 +56,11 @@ abstract class BaseEntity {
public function __set($name,$value) { public function __set($name,$value) {
if (property_exists($this,$name)) { if (property_exists($this,$name)) {
if ($value == '') {
$this->$name = null;
} else {
$this->$name = $value; $this->$name = $value;
}
return $this; return $this;
} else { } else {
throw new \Exception("Property {$name} does not Exists"); throw new \Exception("Property {$name} does not Exists");

View file

@ -3,6 +3,7 @@
namespace Hackspace\Bundle\CalciferBundle\Entity; namespace Hackspace\Bundle\CalciferBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints\DateTime;
/** /**
* Event * Event
@ -102,7 +103,18 @@ class Event extends BaseEntity
} }
public function isValid() { 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() { public function getFormatedDate() {

View file

@ -1,6 +1,6 @@
<form method="post" action="{% if entity.id|default(0) > 0 %}{{ path('_update',{'slug':entity.slug}) }}{% else %}{{ path('_create') }}{% endif %}"> <form method="post" action="{% if entity.id|default(0) > 0 %}{{ path('_update',{'slug':entity.slug}) }}{% else %}{{ path('_create') }}{% endif %}">
<div class="ui form segment"> <div class="ui form segment">
<div class="field"> <div class="field{% if(errors|default('0') != 0) %} {% if('startdate' in errors|keys) %}error{% endif %}{% endif %}">
<label class="control-label required" for="event_startdate">Startdatum</label> <label class="control-label required" for="event_startdate">Startdatum</label>
<div class="ui left labeled icon input"> <div class="ui left labeled icon input">
@ -11,6 +11,9 @@
value="{{ entity.startdate.format('Y-m-d H:i')|default('') }}" value="{{ entity.startdate.format('Y-m-d H:i')|default('') }}"
placeholder="{{ "now"|date('Y-m-d H:i') }}" placeholder="{{ "now"|date('Y-m-d H:i') }}"
class="form-control"> class="form-control">
{% if(errors|default('0') != 0) %} {% if('startdate' in errors|keys) %}
<div class="ui red pointing above ui label">{{ errors.startdate }}</div>
{% endif %}{% endif %}
<i class="icon calendar"></i> <i class="icon calendar"></i>
@ -19,7 +22,7 @@
</div> </div>
</div> </div>
</div> </div>
<div class="field"> <div class="field{% if(errors|default('0') != 0) %} {% if('enddate' in errors|keys) %}error{% endif %}{% endif %}">
<label class="control-label required" for="event_enddate">Enddatum</label> <label class="control-label required" for="event_enddate">Enddatum</label>
<div class="ui left labeled icon input"> <div class="ui left labeled icon input">
@ -30,10 +33,14 @@
placeholder="{{ "now"|date('Y-m-d H:i') }}" placeholder="{{ "now"|date('Y-m-d H:i') }}"
class="form-control"> class="form-control">
{% if(errors|default('0') != 0) %} {% if('enddate' in errors|keys) %}
<div class="ui red pointing above ui label">{{ errors.enddate }}</div>
{% endif %}{% endif %}
<i class="icon calendar"></i> <i class="icon calendar"></i>
</div> </div>
</div> </div>
<div class="field"> <div class="field{% if(errors|default('0') != 0) %} {% if('summary' in errors|keys) %}error{% endif %}{% endif %}">
<label class="" for="event_summary">Zusammenfassung</label> <label class="" for="event_summary">Zusammenfassung</label>
<div class="ui left labeled input"> <div class="ui left labeled input">
@ -45,6 +52,10 @@
maxlength="255" maxlength="255"
class="form-control"> 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 class="ui corner label"> <div class="ui corner label">
<i class="icon asterisk"></i> <i class="icon asterisk"></i>
</div> </div>