Article View: pl.comp.lang.php
Article #230536CodeIgniter: validation_errors()==='' mimo że $this->form_validation->run()===FALSE
From: 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.
Technical Details
Message-ID:
<26006dfa-74bf-4280-900e-d39d7ad3cb72@googlegroups.com>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!news.nask.pl!news.nask.org.pl!newsfeed.pionier.net.pl!news.glorb.com!r2no4310708igi.0!news-out.google.com!ef6ni5522igb.0!nntp.google.com!m5no1695909qaj.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mailShow Full Headers
Complete Article Headers
X-Received: by 10.66.252.69 with SMTP id zq5mr30122135pac.29.1408641249415; Thu, 21 Aug 2014 10:14:09 -0700 (PDT) X-Received: by 10.140.22.70 with SMTP id 64mr26141qgm.27.1408641249262; Thu, 21 Aug 2014 10:14:09 -0700 (PDT) Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!news.nask.pl!news.nask.org.pl!newsfeed.pionier.net.pl!news.glorb.com!r2no4310708igi.0!news-out.google.com!ef6ni5522igb.0!nntp.google.com!m5no1695909qaj.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: pl.comp.lang.php Date: Thu, 21 Aug 2014 10:14:09 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=83.31.201.235; posting-account=Fd8BNwoAAACl8PCi6-fI0bkHGOgt6LMa NNTP-Posting-Host: 83.31.201.235 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <26006dfa-74bf-4280-900e-d39d7ad3cb72@googlegroups.com> Subject: CodeIgniter: validation_errors()==='' mimo że $this->form_validation->run()===FALSE From: jivanmukta@poczta.onet.pl Injection-Date: Thu, 21 Aug 2014 17:14:09 +0000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit