आप एक दृश्य में इस कर रहे हैं? मैं तुम्हें मैं इस संभाल है दिखाता हूँ, लेकिन यह सब एक नियंत्रक में क्या होता है:
// first, we can set a validation rule for the input 'country' (our dropdown), in this case it is required, and must be a natural number. You can look up more rules in the CI user guide, and you can write your own functions as well and add them to the 3rd parameter here. I believe some native PHP functions can be used as well.
$this->form_validation->set_rules('country', 'Country', 'required|is_natural');
// the form is not valid! we'll enter this block whenever the form validation rules above are not met, as well as when first going to this controller-action.
if ($this->form_validation->run() == FALSE) {
// buid your form, there's some CI functions to help with this that I'm using
$my_form = form_open('user/edit', 'class="superform"')
. form_fieldset()
. '<ol>'
. '<li>'
. form_label('Country<br/>', 'country')
// so here is the dropdown, matching the name given to the validation rule we've set, the second parameter takes an array, which I am grabbing from a model, the last parameter is the 'selected; value, which I am grabbing from some variable, if it's not present the first item in the dropdown will obviously be selected
. form_dropdown('country', $this->Country_model->get_countries_dropdown(), $user->country)
. form_error('country', ' <em>', '</em>'
. form_submit('mysubmit', 'Save', 'class="button"')
. '</li>'
. '</ol>'
. form_fieldset_close()
. form_close()
);
// sending the form variable to my view, where i will simply <?=$my_form?> it
$this->load->view('user_edit', $my_form);
} else {
// form has validated! do something!
}
form_dropdown() फ़ंक्शन एक सरणी है कि ऐसा सेटअप लेता है: $ कुंजी => $ मूल्य कहाँ में महत्वपूर्ण मेरा मामला एक देश आईडी है, और मूल्य देश का नाम है। मेरे पास मेरी देश सरणी की शुरुआत में जोड़ी '0' => 'नहीं' है, इसलिए उपयोगकर्ता एक को नहीं चुन सकता है। अगर मैं इसे अपनी स्थिति की तरह बनाना चाहता था, तो मैं इसे '-1' => 'कृपया चुनें ...' पर सेट कर सकता हूं और यह मान्य नहीं होगा, क्योंकि -1 प्राकृतिक संख्या नहीं है।
आशा है कि मेरी रैंपिंग मदद करेगी!
संपादित करें:
ठीक है, तो इससे पहले कि आप form_dropdown() के साथ लटकती बनाने के लिए, आप क्या करना चाहता हूँ पोस्ट सरणी से आने से एक चयनित मूल्य के लिए जाँच है।
सीआई के मामले में, आप, समारोह set_value ($ इनपुट) का उपयोग कर सकते तो मेरे उदाहरण के रूप में मैं की तरह कुछ कर सकता:
$selected = (!empty(set_value('country'))) ? set_value($country) : '';
form_dropdown('country', $this->Country_model->get_countries_dropdown(), $selected)
तो अब लटकती के चयनित मूल्य निर्धारित किया जाएगा अंतिम पोस्ट पर क्या चुना गया था। यह सुनिश्चित करने के लिए कि आप मान्य हैं, आप उस मान को देखना चाहेंगे। यदि कुछ भी नहीं चुना गया था, तो आप वर्तमान में डेटाबेस में मौजूद मान, या आपके द्वारा चुने गए डिफ़ॉल्ट मान जैसे कुछ के रूप में $ 100 सेट कर सकते हैं।
बहुत बढ़िया रिटर्न - कि ठीक काम किया आपको लगता है कि ऐसा करने का एक बेहतर तरीका होगा हालांकि? इसके अलावा, कोई विचार है कि आप ड्रॉपडाउन 'चिपचिपा' कैसे बनाते हैं? – Gaz
क्या आपका मतलब है, अगर फ़ॉर्म सबमिट किया गया था लेकिन कुछ और मान्य नहीं हुआ था, और आप ड्रॉपडाउन को वही चयनित मान रखना चाहते हैं? –
ये, मेरा मतलब यही है। पुन: सत्यापन - सीआई में बुनियादी इनपुट सत्यापन के लिए अच्छा प्रलेखन प्रतीत होता है लेकिन ड्रॉपडाउन के लिए इतना कुछ नहीं है। साथ ही, मैंने सोचा होगा कि डेटाबेस मानों से पॉप्युलेट किए गए ड्रॉपडाउन का एक उच्च प्रतिशत होगा और इसलिए set_select() इसके लिए पूरा करेगा, ऐसा नहीं लगता है? – Gaz