W sumie dlatego tylko z tego nie korzystałem jeszcze, bo dopiero zacząłem przerzucać podstawę swojego CMS'a do symfony2, a co za tym idzie, nie zagłębiałem się jeszcze w niego na tyle.
Jakiś czas temu miałem pierwsze podejście do Symfony2, ale w końcu stwierdziłem, że to jeszcze nieodpowiednia chwila na to, no i natrafiłem na większy problem z tym...
Crozin, wezmę sobie Twoją radę do serca, bo jeżeli to rzeczywiście wygląda tak jak opisałeś w LoginModel (chodzi o adnotacje), to super sprawa.
EDIT::
Ale co do tego:
// Gdzieś tam po wysłaniu formularza wypełnij powyższy obiekt danymi po czym:
$login = new Login();
to chyba powinno być new LoginModel? Tak tylko przeglądając dopiero to co napisałeś stwierdzam to, jeszcze nie testowałem.
Napotkałem problem:
/**
* @Route("/panel/logowanie", name="cms_login")
* @Template("WebsiteCoreBundle:Default:login.html.twig")
*/
public function indexAction()
{
$request = Request::createFromGlobals();
if ($request->getMethod() == "POST")
{
$this->username = $request->get('username');
$this->password = $request->get('password');
$this->rememberMe = $request->get('remember-me');
$this->validate();
}
$loginForm = $this->createFormBuilder()
->add('username', 'text', array( 'label' => ' ',
'placeholder' => 'Nazwa użytkownika'
)
))
->add('password', 'text', array( 'label' => ' ',
'placeholder' => 'Hasło'
)
))
->getForm();
return array('logged' => $this->session->get('logged'), 'login_form' => $loginForm->createView()); }
/**
* Validates the input data from the login form
*/
private function validate()
{
$loginModel = new \Website\CoreBundle\Forms\Models\LoginModel();
$loginModel->username = $this->username;
$loginModel->password = $this->password;
$loginModel->rememberMe = $this->rememberMe;
$errors = $this->get('validator')->validate($loginModel);
$entityManager = $this->getDoctrine()->getEntityManager();
$this->userEntity = $entityManager->getRepository('WebsiteCoreBundle:CmsUsers')->findAll();
}
<?php
namespace Website\CoreBundle\Forms\Models;
use Symfony\Component\Validator\Constraints
as Assert;
class LoginModel
{
/**
* @Assert\NotBlank
* @Assert\MinLength(4)
* @Assert\MaxLength(18)
* @Assert\Regex("[a-Z]")
*/
public $username;
/**
* @Assert\NotBlank
* @Assert\MinLength(4)
* @Assert\MaxLength(18)
* @Assert\Regex("[a-Z]")
*/
public $password;
/**
* @Assert\Type("boolean")
*/
public $rememberMe;
}
1) Nie mogłem zrobić czegoś takiego jak: * @Assert\Regex("[a-Z]+") -> błąd był z tym plusem - wywaliłem
2) var_dump zwraca mi:
Kod
object(Symfony\Component\Validator\ConstraintViolationList)[281]
private 'violations' =>
array (size=3)
0 =>
object(Symfony\Component\Validator\ConstraintViolation)[322]
private 'message' => string 'This value should not be blank.' (length=31)
private 'messageTemplate' => string 'This value should not be blank.' (length=31)
private 'messageParameters' =>
array (size=0)
...
private 'messagePluralization' => null
private 'root' =>
object(Website\CoreBundle\Forms\Models\LoginModel)[279]
...
private 'propertyPath' => string 'username' (length=8)
private 'invalidValue' => null
private 'code' => null
1 =>
object(Symfony\Component\Validator\ConstraintViolation)[330]
private 'message' => string 'This value should not be blank.' (length=31)
private 'messageTemplate' => string 'This value should not be blank.' (length=31)
private 'messageParameters' =>
array (size=0)
...
private 'messagePluralization' => null
private 'root' =>
object(Website\CoreBundle\Forms\Models\LoginModel)[279]
...
private 'propertyPath' => string 'password' (length=8)
private 'invalidValue' => null
private 'code' => null
2 =>
object(Symfony\Component\Validator\ConstraintViolation)[333]
private 'message' => string 'This value should be of type boolean.' (length=37)
private 'messageTemplate' => string 'This value should be of type {{ type }}.' (length=40)
private 'messageParameters' =>
array (size=2)
...
private 'messagePluralization' => null
private 'root' =>
object(Website\CoreBundle\Forms\Models\LoginModel)[279]
...
private 'propertyPath' => string 'rememberMe' (length=10)
private 'invalidValue' => string 'on' (length=2)
private 'code' => null
Nie ważne jak i kiedy, ale zawsze mam niby puste pole
Zauważyłem, że przy takim tworzeniu pól, name nadaje wartość form[username]... a nie jak normalnie w html miałem username, przez co nie mam dostępu może do tego z $request?
Jak to działa?
UPDATE::
Poprawiłem na
$dataFromLoginForm = $request->get('form');
a później
$this->username = $dataFromLoginForm['username'];
UPDATE::
Tylko dodało mi jakiś _token, po co on jest?
UPDATE
Już kurde próbowałem chyba wszystkich możliwości, jeśli chodzi o regex...
a zawsze dostaję:
Kod
object(Symfony\Component\Validator\ConstraintViolationList)[223]
private 'violations' =>
array (size=3)
0 =>
object(Symfony\Component\Validator\ConstraintViolation)[271]
private 'message' => string 'This value is not valid.' (length=24)
private 'messageTemplate' => string 'This value is not valid.' (length=24)
private 'messageParameters' =>
array (size=1)
...
private 'messagePluralization' => null
private 'root' =>
object(Website\CoreBundle\Forms\Models\LoginModel)[42]
...
private 'propertyPath' => string 'username' (length=8)
private 'invalidValue' => string 'admin' (length=5)
private 'code' => null
1 =>
object(Symfony\Component\Validator\ConstraintViolation)[264]
private 'message' => string 'This value is not valid.' (length=24)
private 'messageTemplate' => string 'This value is not valid.' (length=24)
private 'messageParameters' =>
array (size=1)
...
private 'messagePluralization' => null
private 'root' =>
object(Website\CoreBundle\Forms\Models\LoginModel)[42]
...
private 'propertyPath' => string 'password' (length=8)
private 'invalidValue' => string 'admin' (length=5)
private 'code' => null
2 =>
object(Symfony\Component\Validator\ConstraintViolation)[278]
private 'message' => string 'This value should be of type boolean.' (length=37)
private 'messageTemplate' => string 'This value should be of type {{ type }}.' (length=40)
private 'messageParameters' =>
array (size=2)
...
private 'messagePluralization' => null
private 'root' =>
object(Website\CoreBundle\Forms\Models\LoginModel)[42]
...
private 'propertyPath' => string 'rememberMe' (length=10)
private 'invalidValue' => string 'on' (length=2)
private 'code' => null
Mimo, że tutaj
http://gskinner.com/RegExr/ ten sam ciąg znaków z regexp [a-zA-Z]+ działa dobrze...
FIXED