-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ukol 3 - Vojtěch Bartoš #27
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{% extends 'base.html.twig' %} | ||
|
||
{% block body %} | ||
{{ form_start(form) }} | ||
{{ form_row(form.name) }} | ||
{{ form_row(form.phone) }} | ||
{{ form_row(form.username) }} | ||
{{ form_row(form.address) }} | ||
<br /> | ||
<button class="btn btn-lg btn-primary btn-block" type="submit">Uložit</button> | ||
{{ form_end(form) }} | ||
{% endblock %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
namespace AppBundle\Controller; | ||
use AppBundle\Entity\User; | ||
use AppBundle\Facade\UserFacade; | ||
use AppBundle\FormType\PersonalInfoFormType; | ||
use AppBundle\FormType\RegistrationFormType; | ||
use Doctrine\ORM\EntityManager; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
|
@@ -91,6 +92,42 @@ public function loginAction() | |
]; | ||
} | ||
|
||
/** | ||
* @Route("/profile", name="user_profile") | ||
* @Template("user/profile.html.twig") | ||
* | ||
* @param Request $request | ||
* @return array|RedirectResponse | ||
*/ | ||
public function profileAction(Request $request) | ||
{ | ||
// 1) build the form | ||
$user = $this->userFacade->getUser(); | ||
$form = $this->formFactory->create(PersonalInfoFormType::class, $user); | ||
|
||
// 2) handle the submit (will only happen on POST) | ||
$form->handleRequest($request); | ||
if ($form->isSubmitted() && $form->isValid()) { | ||
|
||
if ($address = $user->getAddress()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Je dobře volat pokaždé persist & flush ? |
||
$this->entityManager->persist($address); | ||
$this->entityManager->flush($address); | ||
} | ||
|
||
$this->entityManager->persist($user); | ||
$this->entityManager->flush($user); | ||
|
||
// ... do any other work - like sending them an email, etc | ||
// maybe set a "flash" success message for the user | ||
return RedirectResponse::create($this->router->generate("homepage")); | ||
} | ||
|
||
return [ | ||
"user" => $this->userFacade->getUser(), | ||
"form" => $form->createView(), | ||
]; | ||
} | ||
|
||
/** | ||
* @Route("/odhlasit", name="user_logout") | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace AppBundle\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
/** | ||
* @ORM\Entity | ||
*/ | ||
class Address | ||
{ | ||
/** | ||
* @var int | ||
* @ORM\Id | ||
* @ORM\GeneratedValue | ||
* @ORM\Column(type="integer") | ||
* @var int | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @ORM\Column(type="string", length=255) | ||
* @Assert\NotBlank() | ||
* @var string | ||
*/ | ||
private $street; | ||
|
||
/** | ||
* @ORM\Column(type="string", length=255) | ||
* @Assert\NotBlank() | ||
* @var string | ||
*/ | ||
private $city; | ||
|
||
/** | ||
* @ORM\Column(type="string", length=255) | ||
* @Assert\NotBlank() | ||
* @var string | ||
*/ | ||
private $zip; | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getStreet() | ||
{ | ||
return $this->street; | ||
} | ||
|
||
/** | ||
* @param string $street | ||
*/ | ||
public function setStreet(string $street) | ||
{ | ||
$this->street = $street; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getCity() | ||
{ | ||
return $this->city; | ||
} | ||
|
||
/** | ||
* @param string $city | ||
*/ | ||
public function setCity(string $city) | ||
{ | ||
$this->city = $city; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getZip() | ||
{ | ||
return $this->zip; | ||
} | ||
|
||
/** | ||
* @param string $zip | ||
*/ | ||
public function setZip(string $zip) | ||
{ | ||
$this->zip = $zip; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,25 @@ class User implements UserInterface | |
private $password; | ||
|
||
/** | ||
* @Assert\NotBlank() | ||
* @ORM\Column(type="string", length=255, name="name", nullable=true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nejsem si jistý, jak správně řešit tuto validaci když pro registraci chci aby to nebylo null ale pro editaci adresy to null naopak chci.. |
||
* @Assert\Length(max=4096) | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @ORM\Column(type="string", length=20, name="phone", nullable=true) | ||
* @Assert\Length(max=4096) | ||
*/ | ||
private $phone; | ||
|
||
/** | ||
* @ORM\OneToOne(targetEntity="Address") | ||
* @ORM\JoinColumn(name="address_id", referencedColumnName="id") | ||
* @var Address | ||
*/ | ||
private $address; | ||
|
||
/** | ||
* @Assert\Length(max=4096) | ||
*/ | ||
private $plainPassword; | ||
|
@@ -129,4 +147,51 @@ public function eraseCredentials() | |
return; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @param mixed $name | ||
*/ | ||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getPhone() | ||
{ | ||
return $this->phone; | ||
} | ||
|
||
/** | ||
* @param mixed $phone | ||
*/ | ||
public function setPhone($phone) | ||
{ | ||
$this->phone = $phone; | ||
} | ||
|
||
/** | ||
* @return Address | ||
*/ | ||
public function getAddress() | ||
{ | ||
return $this->address; | ||
} | ||
|
||
/** | ||
* @param Address $address | ||
*/ | ||
public function setAddress($address) | ||
{ | ||
$this->address = $address; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace AppBundle\FormType; | ||
|
||
use AppBundle\Entity\Address; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class AddressFormType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder | ||
->add('street', TextType::class, [ | ||
"label" => "Ulice", | ||
"attr" => [ | ||
"class" => "form-control", | ||
], | ||
]) | ||
->add('city', TextType::class, [ | ||
"label" => "Město", | ||
"attr" => [ | ||
"class" => "form-control", | ||
], | ||
]) | ||
->add('zip', TextType::class, [ | ||
"label" => "PSČ", | ||
"attr" => [ | ||
"class" => "form-control", | ||
], | ||
]); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults(array( | ||
"data_class" => Address::class, | ||
)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace AppBundle\FormType; | ||
|
||
use AppBundle\Entity\User; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\EmailType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class PersonalInfoFormType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder | ||
->add("name", TextType::class, [ | ||
"label" => "Jméno", | ||
"attr" => [ | ||
"class" => "form-control", | ||
], | ||
]) | ||
->add("phone", TextType::class, [ | ||
"label" => "Telefon", | ||
"attr" => [ | ||
"class" => "form-control", | ||
], | ||
]) | ||
->add("username", EmailType::class, [ | ||
"label" => "E-mail", | ||
"attr" => [ | ||
"class" => "form-control", | ||
], | ||
]) | ||
->add('address', AddressFormType::class); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults(array( | ||
"data_class" => User::class, | ||
)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Našel jsem si, že lze sdílet context mezi více firewally. Nelíbí se mi ale, že musím u obou konfigurovat form_login i logout a jediné v čem se vlastně liší je, že tady chybí anonymous. Lze to napsat i úsporněji?