Skip to content

Commit

Permalink
Merge pull request #12 from aramirez-es/master
Browse files Browse the repository at this point in the history
Validación del formulario en lado cliente.
  • Loading branch information
Ricard Clau committed Feb 5, 2012
2 parents bbd43ce + b890a43 commit 0c10340
Show file tree
Hide file tree
Showing 6 changed files with 504 additions and 27 deletions.
48 changes: 44 additions & 4 deletions src/SFBCN/WebsiteBundle/Controller/DefaultController.php
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
28 changes: 28 additions & 0 deletions src/SFBCN/WebsiteBundle/Resources/public/css/02-web-layout.css
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 @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit 0c10340

Please sign in to comment.