Skip to content

Commit

Permalink
Validation client side. Tracking events
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Ramirez Fernandez authored and Alberto Ramirez Fernandez committed Feb 4, 2012
1 parent 5313fa0 commit b890a43
Show file tree
Hide file tree
Showing 4 changed files with 423 additions and 77 deletions.
48 changes: 44 additions & 4 deletions src/SFBCN/WebsiteBundle/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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')));
}

/**
Expand All @@ -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
*/
Expand Down
24 changes: 24 additions & 0 deletions src/SFBCN/WebsiteBundle/Resources/public/css/02-web-layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -260,6 +271,11 @@ label.form-error {
color: red;
}

.form-label-message {
font-size: 10px;
display: block;
}

input[type=submit] {
border: 0 none;
-moz-border-radius: 5px;
Expand All @@ -274,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;
}
Expand Down
Loading

0 comments on commit b890a43

Please sign in to comment.