Permalink
Browse files

Merge pull request #12 from aramirez-es/master

Validación del formulario en lado cliente.
  • Loading branch information...
2 parents bbd43ce + b890a43 commit 0c10340ae66470fa42a063f4a2d7ab574a368293 Ricard Clau committed Feb 5, 2012
@@ -91,7 +91,34 @@ public function sendMailAction()
'mensaje' => $this->getRequest()->get('mensaje'),
);
- $this->validateContactData($contactData);
+ try {
+
+ $this->validateContactData($contactData);
+
+ } catch (HttpException $e) {
+
+ /**
+ * Due to HttpException only accept a text message and we need
+ * a json array, we catching the HttpException to return a new
+ * Response object with message in json format and status code.
+ */
+ return new Response($e->getMessage(), $e->getStatusCode());
+ }
+
+ $this->sendMailOnContactFormSuccess($contactData);
+
+ return new Response(json_encode(
+ array('message' => 'Mail enviado correctamente. En breve contactaremos contigo')
+ ));
+ }
+
+ /**
+ * Extract method to send the email with data receive from contact form.
+ *
+ * @param array $contactData Data given from contact form.
+ */
+ private function sendMailOnContactFormSuccess(Array $contactData)
+ {
$mailTo = $this->container->getParameter('contactmail');
$message =\Swift_Message::newInstance()
@@ -101,8 +128,6 @@ public function sendMailAction()
->setBody($contactData['mensaje']);
$this->container->get('mailer')->send($message);
-
- return new Response(json_encode(array('message' => 'Mail enviado correctamente. En breve contactaremos contigo')));
}
/**
@@ -128,10 +153,25 @@ private function validateContactData(array $contactData)
$errors = $this->container->get('validator')->validateValue($contactData, $collectionConstraint);
if (count($errors) !== 0) {
- throw new HttpException(400, $errors[0]->getPropertyPath() . ':' . $this->container->get('translator')->trans($errors[0]->getMessage(), array(), 'validators'));
+ throw new HttpException(400, $this->parseErrors($errors));
}
}
+ private function parseErrors($errors) {
+
+ $translator = $this->container->get('translator');
+ $parsedErrors = array();
+
+ foreach($errors as $error) {
+
+ $translatedError = $translator->trans($error->getMessage(), array(), 'validators');
+ $parsedErrors[substr($error->getPropertyPath(), 1, -1)] = $translatedError;
+
+ }
+
+ return json_encode($parsedErrors);
+ }
+
/**
* @var array $founders Symfony-Barcelona group founders
*/
@@ -240,6 +240,17 @@ aside.related p {
width: 50%;
}
+#form-message-success {
+ position: relative;
+ background-color: #DFF2BF;
+ -moz-border-radius: 5px;
+ -webkit-border-radius: 5px;
+ border-radius: 5px;
+ margin: 10px;
+ padding: 10px;
+ color: #4F8A10;
+}
+
form {
background-color: #f6f6f6;
-moz-border-radius: 5px;
@@ -256,6 +267,15 @@ label {
margin-bottom: 5px;
}
+label.form-error {
+ color: red;
+}
+
+.form-label-message {
+ font-size: 10px;
+ display: block;
+}
+
input[type=submit] {
border: 0 none;
-moz-border-radius: 5px;
@@ -270,6 +290,14 @@ input[type=submit]:hover {
text-decoration: underline;
}
+input[type=submit].form-sending {
+ cursor: default;
+}
+
+input[type=submit]:hover.form-sending {
+ text-decoration: none;
+}
+
#contact p {
margin: 1em;
}
Oops, something went wrong.

0 comments on commit 0c10340

Please sign in to comment.