Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Laravel] Request (Validacja) pole z reqex
KotWButach
post 17.06.2019, 09:44:07
Post #1





Grupa: Zarejestrowani
Postów: 361
Pomógł: 10
Dołączył: 8.02.2012

Ostrzeżenie: (0%)
-----


Witam,
zawsze sprawiało mi to problem, nie umiem uruchomić walidacji zawierającej wyrażenie regularne, czy ktoś mógłbym napisać mi jak to powinno być i dlaczego?

wyrażenie regularne: ^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$
prawidłowe dane YYYY-MM-DD np 2019-06-17

Wyrażenie jest prawidłowe, problem stanowi uruchomienie go wewnątrz walidacji od Laravela.

Kod:

  1.  
  2. <?php
  3.  
  4. namespace App\Http\Requests;
  5.  
  6. use Illuminate\Foundation\Http\FormRequest;
  7.  
  8. class FiltersRequest extends FormRequest
  9. {
  10. /**
  11.   * Determine if the user is authorized to make this request.
  12.   *
  13.   * @return bool
  14.   */
  15. public function authorize()
  16. {
  17. return true;
  18. }
  19.  
  20. /**
  21.   * Get the validation rules that apply to the request.
  22.   *
  23.   * @return array
  24.   */
  25. public function rules()
  26. {
  27. return [
  28.  
  29. 'date' => 'required|regex:/\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/', // CZESC KTORA NAS INTERESUJE
  30.  
  31. ];
  32. }
  33. }
  34.  
  35.  


Ten post edytował KotWButach 17.06.2019, 09:46:02
Go to the top of the page
+Quote Post
viking
post 17.06.2019, 09:46:29
Post #2





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1113
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Ale przecież masz validator daty https://laravel.com/docs/5.8/validation#rule-date


--------------------
Go to the top of the page
+Quote Post
Pyton_000
post 17.06.2019, 10:03:40
Post #3





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Bardziej się nada ten: https://laravel.com/docs/5.8/validation#rule-date-format
Go to the top of the page
+Quote Post
KotWButach
post 17.06.2019, 10:05:11
Post #4





Grupa: Zarejestrowani
Postów: 361
Pomógł: 10
Dołączył: 8.02.2012

Ostrzeżenie: (0%)
-----


Kompletnie zapomniałem o "date_format"
dzięki.

A co do regex czy ktoś wie dlaczego to nie działa, bo chciałbym jednak się nauczyć stosować regex w laravel
Go to the top of the page
+Quote Post
Pyton_000
post 17.06.2019, 10:25:50
Post #5





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Nie działa bo:

Cytat
Note: When using the regex / not_regex patterns, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.


Powinno być coś takiego:

Kod
['required', 'regex:/\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/']
Go to the top of the page
+Quote Post
markonix
post 17.06.2019, 19:48:57
Post #6





Grupa: Zarejestrowani
Postów: 2 707
Pomógł: 290
Dołączył: 16.12.2008
Skąd: Śląsk

Ostrzeżenie: (0%)
-----


Ja ogólnie polecam używać tablic zamiast stringów, jak dla mnie to chyba jakieś przyzwyczajenia z CodeIgniter albo podobnych FW.
Do tablic wrzucisz obiekt Rule, łatwiej manipulować np. dodać rule w zależności od jakichś warunków itp.


--------------------
Go to the top of the page
+Quote Post
phamyen
post 25.06.2019, 04:59:03
Post #7





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 14.05.2019

Ostrzeżenie: (10%)
X----


The article you shared here is very awesome. I really like and appreciate your work. I have read your article deeply, the points that you mentioned in this article are useful
girls go games
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.03.2024 - 07:25