![]() |
![]() |
![]()
Post
#1
|
|
Grupa: Zarejestrowani Postów: 1 168 Pomógł: 126 Dołączył: 5.02.2010 Skąd: Świdnica Ostrzeżenie: (0%) ![]() ![]() |
Witam,
szukam jak zrobić walidację danych w symfony, ale wszystko kieruje mnie albo do korzystania z czegoś takiego: http://symfony.com/doc/2.0/book/validation.html albo do tworzenia http://symfony.com/doc/2.0/reference/const.../MinLength.html a moje pytanie: jak zrobić prostą walidację powiedzmy pobranej nazwy użytkownika z formularza? Formularz mam na "czysto" w html.
no i wcześniej jak robiłem w czystym php to zrobiłem walidację na podstawie:
z tym, że to była moja klasa, którą mogłem sobie wykorzystać wszędzie tam, gdzie chciałem. Tutaj też teoretycznie mógłbym to zrobić, ale symfony2 chyba ma jakieś gotowe walidatory. Tylko jak ich użyć na podanym przykładzie? |
|
|
![]() |
![]()
Post
#2
|
|
Grupa: Zarejestrowani Postów: 1 168 Pomógł: 126 Dołączył: 5.02.2010 Skąd: Świdnica Ostrzeżenie: (0%) ![]() ![]() |
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:
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:
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 Ten post edytował Szymciosek 10.05.2013, 23:18:42 |
|
|
![]() ![]() |
![]() |
Aktualny czas: 13.10.2025 - 06:03 |