是否有一種創造性且簡單的方法來一次檢查多個表單域?如何驗證Codeigniter中組合的許多字段?
我有一個形式與生成的字段在飛,每個都有一個唯一的ID。
事情是提交所有字段不是必需的,但至少有一個字段必須填寫前提交。
有沒有辦法在Codeigniter中做到這一點,或者我將如何去有效驗證這一點。
我知道可以單獨檢查每個字段,但我正在尋求一種更簡潔的方式。
我希望你們清楚。謝謝。
是否有一種創造性且簡單的方法來一次檢查多個表單域?如何驗證Codeigniter中組合的許多字段?
我有一個形式與生成的字段在飛,每個都有一個唯一的ID。
事情是提交所有字段不是必需的,但至少有一個字段必須填寫前提交。
有沒有辦法在Codeigniter中做到這一點,或者我將如何去有效驗證這一點。
我知道可以單獨檢查每個字段,但我正在尋求一種更簡潔的方式。
我希望你們清楚。謝謝。
現在,我有一個自定義功能,這將批量檢查我的窗體上的每個提交確保它符合一套規則。我想這可能會將我的表單設置爲一個html數組(data[]
),在代碼點火器中檢索它,然後使用CI callback_
驗證函數確保一切正常。這看起來很複雜,所以我還沒有完全包裹頭部,但也許這可以讓你的車輪朝着正確的方向轉向。
編輯:
$this->load->library('form_validation');
// If there is any posted data, then we should assign it to our $post_data array.
$post_data = $this->input->post('project_data');
if (empty($post_data)) {die('empty form');}
// Now, we are ready to validate the incoming data.
// We will send the data through a callback function which will check to make sure it is valid.
// If it is not valid, the callback function will trigger a codeigniter validation error.
// Let's temporarily remove any commas from the submission data to avoid delimiter confusion when sending it through the callback
$post_data = str_replace(",", "DELIMITEDCOMMA", $post_data);
$post_data_str = http_build_query($post_data);
$this->form_validation->set_rules("project_data[errors]", 'Errors', "required|callback__validate_project_data[$post_data_str]");
$this->form_validation->run();
然後,只需編寫一個基於它是什麼,你需要驗證您的自定義驗證函數。
function _validate_project_data($value, $request)
{
// A callback rule check is being attempted by the CI validator
// $value is the actual value of the submission, while $request is the key and value
$request = explode(",", $request);
$request = str_replace("DELIMITEDCOMMA", ",", $request);
// rename the keys in the request back to the original convention
parse_str($request[0], $request);
//var_dump($request);
// perform validation here and return true or false (valid or invalid)
}
我離開了一段時間,我無法理解你的意思。目前我正在循環所有的字段,並且即使失敗也會返回一個錯誤。不是很方便。但我會試着理解你的意思並試圖實現。 – saint 2010-12-13 13:51:06
我已經添加了一些代碼給我的答案 - 這有幫助嗎? – 2010-12-13 14:47:08
不完全確定你的意思,因爲這些字段必須以任何方式單獨檢查。除非你連接所有的輸入,並檢查?也許我錯過了那裏的一些東西。
只是去客戶端:http://flowplayer.org/tools/demos/validator/index.html
Serverside集團:http://formigniter.org/
希望這有助於...
試試這個:
echo $this->input->post('data_you_want_to_validate_together');
如何在您的輸入字段命名爲:
現在你可以通過得到的數據? – Repox 2010-11-17 09:28:32
「前綴」後跟一個「時間戳」,用於日期範圍內的日期。 – saint 2010-11-17 10:08:15