Add server side error handling

This commit is contained in:
Tim Schumacher 2013-09-19 14:59:38 +02:00
parent f687ce8e1e
commit ce0ae7a79d
2 changed files with 57 additions and 0 deletions

View file

@ -1,5 +1,6 @@
<?php <?php
require 'vendor/autoload.php'; require 'vendor/autoload.php';
require_once 'is_email.php';
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -33,4 +34,49 @@ $app->get('/', function (Request $request) use ($app) {
)); ));
}); });
$app->post('/', function (Request $request) use ($app) {
$errors = array();
// collect the params
$user = $request->get('username',null);
$host = $request->get('host',null);
$email = $request->get('mail',null);
$password = $request->get('password',null);
$password_repeat = $request->get('password_repeat',null);
// check for errors
if (!$user) {
$errors[] = $app->trans('Kein Benutzername angegeben.');
}
if (!$host) {
$errors[] = $app->trans('Keinen Hostnamen angegeben.');
}
if (!$email) {
$errors[] = $app->trans('Keine E-Mail-Adresse angegeben.');
} else {
if (!is_email($email)) {
$errors[] = $app->trans('Keine gültige E-Mail-Adresse angegeben.');
}
}
if (!$password) {
$errors[] = $app->trans('Kein Passwort angegeben.');
}
if ($password != $password_repeat) {
$errors[] = $app->trans('Bitte gebe in den Feldern Passwort und Passwortwiederholung identische Werte ein.');
}
if (count($errors) > 0) {
return $app['twig']->render('registration_form.twig', array(
'errors' => $errors,
));
} else {
return $app['twig']->render('success.twig', array(
));
}
});
$app->run(); $app->run();

View file

@ -21,6 +21,17 @@
<!-- Form Name --> <!-- Form Name -->
<legend>{% trans %}Ein XMPP-Konto registrieren{% endtrans %}</legend> <legend>{% trans %}Ein XMPP-Konto registrieren{% endtrans %}</legend>
{% if errors %}
<div class="alert alert-danger">
<p>{% trans %}Bitte behebe folgende Fehler bevor du weiter machen kannst{% endtrans %}:</p>
<ul>
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
<!-- Text input--> <!-- Text input-->
<div class="control-group"> <div class="control-group">
<label class="control-label" for="username">{% trans %}Benutzername{% endtrans %}</label> <label class="control-label" for="username">{% trans %}Benutzername{% endtrans %}</label>