2012-05-18 15 views
5

काम नहीं कर रहा प्रोफ़ाइल डेटा की एक सरणी है मैं मान्य करने के लिए की जरूरत है:CodeIgniter: मान्य प्रपत्र सरणी

$user_group_profiles = $this->input->post('user_group_profiles', TRUE); 
foreach ($user_group_profiles as $key => $user_group_profile) 
{ 
    $this->form_validation->set_rules("user_group_profiles[$key][profile_name]", 'Profile Name', 'trim|required'); 
    $this->form_validation->set_rules("user_group_profiles[$key][birthdate]", 'Birthdate', 'trim|required'); 

    // TODO: heigth/weight not required, but the validation somehow makes it required 
    $this->form_validation->set_rules("user_group_profiles[$key][height]", 'Height', 'trim|greater_than[0]'); 
    $this->form_validation->set_rules("user_group_profiles[$key][weight]", 'Weight', 'trim|greater_than[0]'); 
} 

ऊंचाई और वजन विकल्प हैं, लेकिन जब कोई मूल्य नहीं उन क्षेत्रों के लिए निर्धारित है, सीआई सत्यापन शिकायत।

array 
    'ugp_b33333338' => 
    array 
     'profile_name' => string '' (length=0) 
     'birthdate' => string '' (length=0) 
     'height' => string '' (length=0) 
     'weight' => string '' (length=0) 

कोई भी विचार क्या गलत हो सकता है: एक var_dump($user_group_profiles); यह दिखाता है?

संपादित करें 1:

मैं सीआई के Form_validation पुस्तकालय में चला गया और $_field_data और जनता के सदस्य बनाया। जब मैंने इसे बाद var_export, मैं यह मिल गया:

'user_group_profiles[ugp_833333338][height]' => 
    array 
     'field' => string 'user_group_profiles[ugp_833333338][height]' (length=42) 
     'label' => string 'Height' (length=6) 
     'rules' => string 'greater_than[1]' (length=15) 
     'is_array' => boolean true 
     'keys' => 
     array 
      0 => string 'user_group_profiles' (length=19) 
      1 => string 'ugp_833333338' (length=13) 
      2 => string 'height' (length=6) 
     'postdata' => string '' (length=0) 
     'error' => string 'The Height field must contain a number greater than 1.' (length=54) 
+1

'profile_name' और' birthdate' भी खाली हैं? – Hindol

+0

हां, मैंने संरचना को दिखाने के लिए किसी भी इनपुट के बिना फॉर्म सबमिट किया है। – StackOverflowNewbie

+0

क्या मैं आपके एचटीएमएल मार्कअप को देख सकता हूं? [पास्टबिन] (http://pastebin.com/) – Rocco

उत्तर

7

ठीक है - मैं सिर्फ इस पर एक घंटे खर्च किया है - और मैं समस्या + समाधान बाहर काम किया है

मुद्दा यह है कि जब चर आप परीक्षण कर रहे हैं एक सरणी का हिस्सा है, सीआई फ़ील्ड को सेट के रूप में व्याख्या करता है, और इस प्रकार "मान" होता है (यहां तक ​​कि जब यह खाली होता है)। क्योंकि वह "मान" 0 से अधिक नहीं है - यह विफल रहता है।

इसलिए आपको "खाली" होने पर $ _POST (NOT $ user_group_profiles) चर को अनसेट करने की आवश्यकता होगी ताकि यह सत्यापन पास हो जाए।नोट - सत्यापन $ _POST पर चलाया जाता है - यही वजह है कि आप $ _POST unsetting कर रहे हैं और $ नहीं user_group_profiles

तो वैकल्पिक हल यह है:

$user_group_profiles = $this->input->post('user_group_profiles', TRUE); 
foreach ($user_group_profiles as $key => $user_group_profile) 
{ 
    // Set the rules 
    $this->form_validation->set_rules($key."[profile_name]", "Profile Name", "trim|required"); 
    $this->form_validation->set_rules($key."[birthdate]", "Birthdate", "trim|required"); 
    $this->form_validation->set_rules($key."[height]", "Height", "trim|greater_than[0]"); 
    $this->form_validation->set_rules($key."[weight]", "Weight", "trim|greater_than[0]"); 

    // Now check if the field is actually empty 
    if (empty($user_group_profile['height'])) 
    { 
     // If empty, remove from array so CI validation doesnt get tricked and fail 
     unset($_POST[$key]['height']); 
    } 
    if (empty($user_group_profile['weight'])) 
    { 
     unset($_POST[$key]['weight']); 
    } 
} 

मैं इस परीक्षण किया है - और यह आपकी समस्या को ठीक करता है।

आप भी इसे इस तरह से प्रोग्राम सकता है अगर आप $ _POST डेटा छूना चाहता हूँ न:

foreach ($user_group_profiles as $key => $user_group_profile) 
    { 
     // Set the rules 
     $this->form_validation->set_rules($key."[profile_name]", "Profile Name", "trim|required"); 
     $this->form_validation->set_rules($key."[birthdate]", "Birthdate", "trim|required"); 


     // Now check if the field is actually empty 
     if (! empty($user_group_profile['height'])) 
     { 
      $this->form_validation->set_rules($key."[height]", "Height", "trim|greater_than[0]"); 
     } 
     if (! empty($user_group_profile['weight'])) 
     { 
      $this->form_validation->set_rules($key."[weight]", "Weight", "trim|greater_than[0]"); 
     } 
    } 
+0

महान काम! धन्यवाद, यह काम किया! – StackOverflowNewbie

1

की कोशिश करते हैं और इस भागों में टूट करते हैं। मुझे आशा है कि मैं आपकी मदद कर सकता हूं।

मैं दूसरा "TRUE" तर्क के साथ XSS छनन निम्नलिखित आप कर रहे हैं यह सोचते हैं हूँ:

$user_group_profiles = $this->input->post('user_group_profiles', TRUE); 

आप वास्तव में फ़ॉर्म सत्यापन नियमों के साथ XSS छानने कर सकते हैं, या आप चाहें तो नियमों के बाद पोस्ट फ़िल्टर करें। यहाँ मेरी प्राथमिकता के लिए देखें:

$this->form_validation->set_rules('profile_name', 'Profile Name', 'xss_clean|trim|required'); 

ताकि जानने अब के साथ, हम अपने फार्म मान्यकरण लाइब्रेरी के लिए सीआई का पालन कर सकते हैं। प्रपत्र सत्यापन पुस्तकालय का उपयोग करने से पहले पोस्ट को पकड़ना जरूरी नहीं है क्योंकि यह स्वतः पता लगाता है कि मैं फ़ील्ड नाम से पोस्ट डेटा पर विश्वास करता हूं। उदाहरण के लिए:

$this->form_validation->set_rules('profile_name', 'Profile Name', 'trim|required|xss_clean'); 
$this->form_validation->set_rules('birthdate', 'Birthdate', 'trim|required|xss_clean'); 
$this->form_validation->set_rules('height', 'Height', 'trim|greater_than[0]|numeric'); 
$this->form_validation->set_rules('weight', 'Weight', 'trim|greater_than[0]|numeric'); 

if ($this->form_validation->run() == FALSE) { 
    $this->load->view('my_view_where_this_post_came_from'); 
} else { 
    $profile_name = $this->input->post('profile_name'); 

    //or if you prefer the XSS check here, this: 
    //$profile_name = $this->input->post('profile_name', TRUE); 

    $birthdate= $this->input->post('birthdate'); 
    $height= $this->input->post('height'); 
    $weight= $this->input->post('weight'); 

    //$user_group_profiles = $this->input->post(); 

} 

मुझे उम्मीद है कि इससे मदद मिलती है!

संपादित करें: मैंने यह भी देखा है।

$user_group_profiles = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter 
$user_group_profiles = $this->input->post(); // returns all POST items without XSS filter 

नहीं: आप पूरी पोस्ट सरणी हड़पने के लिए कोड है प्रयास कर रहे हैं

$user_group_profiles = $this->input->post('user_group_profiles'); 

एक अच्छा मदद अगर आप अपने $ _POST नाम पता नहीं है या उलझन में हैं, तो आप यह देखने के लिए यह कर सकते हैं कि वह डेटा भी है या नहीं! आपकी पहली पंक्ति के रूप में इस तरह:

var_dump($_POST); 
exit(); 
+0

पोस्ट चर को पकड़ने का कारण यह है कि फ़ॉर्म मेरे आवेदन द्वारा गतिशील रूप से जेनरेट किया गया है। मैं सिर्फ "ऊंचाई" पर नियम नहीं जोड़ सकता क्योंकि यह वास्तव में "ऊंचाई_0", "ऊंचाई_1", "ऊंचाई_2" इत्यादि है - और मुझे इन चीजों को पहले से नहीं पता है। इसलिए, मैं उनके माध्यम से पोस्ट, लूप पकड़ता हूं और प्रत्येक पुनरावृत्ति में नियम आवंटित करता हूं। यही कारण है कि मैंने कहा कि मेरे पास "प्रोफाइल डेटा का सरणी" है .... – StackOverflowNewbie

+0

इसके अलावा, मैं '$ this-> इनपुट-> पोस्ट ('user_group_profiles') का उपयोग कर रहा हूं क्योंकि मैं केवल यही प्राप्त करना चाहता हूं पोस्ट सरणी का हिस्सा। – StackOverflowNewbie