Thread View: pl.comp.lang.php
4 messages
4 total messages
Started by jivanmukta
Thu, 21 Aug 2014 17:14
CodeIgniter: validation_errors()==='' mimo że $this->form_validation->run()===FALSE
Author: jivanmukta
Date: Thu, 21 Aug 2014 17:14
Date: Thu, 21 Aug 2014 17:14
78 lines
5220 bytes
5220 bytes
Mam problem z walidacją w CodeIgniterze.
Walidacje zdefiniowałem tak:
private function setRules() {
$fv = $this->form_validation;
$fv->set_rules('client_name', 'Nazwa / imię i nazwisko', 'trim|alpha_dash|max_length[45]');
$fv->set_rules('client_email', 'E-mail', 'trim|email|callback_client_data');
$fv->set_rules('client_mobile1', 'Telefon komórkowy 1', 'trim|callback_mobile1');
$fv->set_rules('client_mobile2', 'Telefon komórkowy 2', 'trim|callback_mobile2');
$fv->set_rules('client_direction1', 'Numer kierunkowy 1', 'trim|integer|exact_length[2]');
$fv->set_rules('client_phone1', 'Telefon 1', 'trim|callback_phone1');
$fv->set_rules('client_direction2', 'Numer kierunkowy 2', 'trim|integer|exact_length[2]');
$fv->set_rules('client_phone2', 'Telefon 2', 'trim|callback_phone2');
$fv->set_rules('client_town', 'Miejscowość', 'trim|alpha_dash|max_length[25]');
$fv->set_rules('property_estate_type_id', 'Rodzaj nieruchomości', 'required|callback_estate_type');
$fv->set_rules('property_town', 'Miejscowość', 'trim|alpha_dash|max_length[25]');
$fv->set_rules('property_district', 'Dzielnica/gmina', 'trim|alpha_dash|max_length[25]');
$fv->set_rules('property_street', 'Ulica', 'trim|alpha_dash|max_length[30]');
$fv->set_rules('property_house_no', 'Numer domu', 'trim|max_length[8]');
$fv->set_rules('property_flat_no', 'Numer mieszkania', 'trim|alpa_numeric|max_length[5]');
$fv->set_rules('property_floor_no', 'Piętro', 'trim|integer|max_length[5]');
$fv->set_rules('property_floors', 'Ile pięter', 'trim|integer|max_length[5]');
$fv->set_rules('property_area', 'Powierzchnia', 'trim|numeric|max_length[11]');
$fv->set_rules('property_rooms', 'Liczba pokoi', 'trim|integer|max_length[10]');
$fv->set_rules('property_offices', 'Liczba pomieszczeń', 'trim|integer|max_length[10]');
$fv->set_rules('property_year_built', 'Rok budowy', 'trim|integer|max_length[5]');
$fv->set_rules('property_remarks', 'Dodatkowy opis', 'callback_property_remarks');
$fv->set_rules('property_pictures', 'Zdjęcia lub filmy', 'callback_property_pictures');
$fv->set_rules('offer_type_id', 'Rodzaj oferty', 'required|callback_offer_type');
$fv->set_rules('change_to', 'Na co zamiana', 'callback_change_to');
$fv->set_rules('remarks', 'Uwagi', 'callback_remarks');
$fv->set_rules('valid_from', 'Od kiedy ważne', 'trim|date|callback_dates_valid');
$fv->set_rules('valid_to', 'Do kiedy ważne', 'trim|date');
$fv->set_rules('captcha', 'Captcha', 'trim|required|callback_captcha');
}
(wszystkie wymienione pola istnieją na formularzu, callbacki też istnieją i dobrze działają).
Przykładowy callback:
public function dates_valid() {
$d1 = $this->input->post('valid_from');
$d2 = $this->input->post('valid_to');
if (!empty($d1) && !empty($d2)) {
$d1 = new DateTime($d1);
$d2 = new DateTime($d2);
if ($d1 > $d2) {
$this->form_validation->set_message('dates_valid', 'Data od kiedy ważne nie może być późniejsza.');
return FALSE;
}
}
return TRUE;
}
Waliduję tak:
private function validate() {
$b = $this->form_validation->run();
if (!$b) {
if (array_key_exists('property_pictures', $_FILES) &&
!empty($_FILES['property_pictures']['name']) &&
!empty($_FILES['property_pictures']['name'][0])) {
$this->session->set_flashdata('propertyPicturesError', 'Proszę ponownie dodać pliki.');
}
}
return $b;
}
public function insert() { // akcja announcement/insert
$this->setRules();
if (!$this->validate()) {
redirect('/announcement/edit');
} else {
...
}
}
Problem w tym, że chociaż $this->form_validation->run()===FALSE, to mimo to w akcji announcement/edit mam validation_errors()===''.
Nie mogę dojść dlaczego. Proszę o pomoc.
Re: CodeIgniter: validation_errors()==='' mimo że $this->form_validation->run()===FALSE
Author: Borys Pogoreło
Date: Thu, 21 Aug 2014 18:17
Date: Thu, 21 Aug 2014 18:17
34 lines
2468 bytes
2468 bytes
Dnia Thu, 21 Aug 2014 10:14:09 -0700 (PDT), jivanmukta@poczta.onet.pl
napisał(a):
Limity na długościach pól Cię szybko ugryzą w dole pleców:
http://www.slideshare.net/datawise_pl/najdusza-nazwa-ulicy-i-miejscowoci-polska
> $fv->set_rules('property_floor_no', 'Piętro', 'trim|integer|max_length[5]');
> $fv->set_rules('property_floors', 'Ile pięter', 'trim|integer|max_length[5]');
> $fv->set_rules('property_area', 'Powierzchnia', 'trim|numeric|max_length[11]');
> $fv->set_rules('property_rooms', 'Liczba pokoi', 'trim|integer|max_length[10]');
> $fv->set_rules('property_offices', 'Liczba pomieszczeń', 'trim|integer|max_length[10]');
> $fv->set_rules('property_year_built', 'Rok budowy', 'trim|integer|max_length[5]');
A tu wykazujesz nadmierny optymizm ;)
> if ($d1 > $d2) {
> $this->form_validation->set_message('dates_valid', 'Data od kiedy ważne nie może być późniejsza.');
Nieczytelny komunikat.
> Problem w tym, że chociaż $this->form_validation->run()===FALSE, to mimo to w akcji announcement/edit mam validation_errors()===''.
> Nie mogę dojść dlaczego. Proszę o pomoc.
Nie znam CI, ale 5 minut z Google wskazuje na to, że validation_errors()
wyświetla błędy, a nie zwraca. Powinieneś sprawdzić indywidualnie przez
form_error($nazwa_pola), co jest z kolei drogą przez mękę.
Poczytałem dalej dokumentację i ta cała walidacja jest paskudnie
zaprojektowana, czego powyższe jest tylko jednym z przykładów.
--
Borys Pogoreło
borys(#)leszno,edu,pl
Re: CodeIgniter: validation_errors()==='' mimo że $this->form_validation->run()===FALSE
Author: jivanmukta
Date: Sun, 24 Aug 2014 06:17
Date: Sun, 24 Aug 2014 06:17
28 lines
2611 bytes
2611 bytes
form_error też zwraca pusty string.
Zrobiłem tak i teraz jest dobrze:
stworzyłem bibliotekę MY_Form_validation z callbackami i funkcją setRules i funkcją run:
public function run() {
$b = parent::run();
if (!$b) {
$e = $this->_error_array;
if (array_key_exists('property_pictures', $_FILES) &&
!empty($_FILES['property_pictures']['name']) &&
!empty($_FILES['property_pictures']['name'][0])) {
$e['property_pictures'] = 'Proszę ponownie dodać pliki.';
}
$this->ci->session->set_flashdata('field_errors', $e);
}
return $b;
}
oraz stworzyłem helpera:
function myFormError($fieldName) {
$ci =& get_instance();
$e = $ci->session->flashdata('field_errors');
if (isset($e[$fieldName])) {
return "<span class='atserver_error'>" . $e[$fieldName] . '</span>';
}
return '';
}
Re: CodeIgniter: validation_errors()==='' mimo że $this->form_validation->run()===FALSE
Author: jivanmukta
Date: Wed, 10 Sep 2014 15:42
Date: Wed, 10 Sep 2014 15:42
1 lines
1494 bytes
1494 bytes
Czy ktoś wie dlaczego form_error(...) zwraca mi pusty string?
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads