Welcome Guest, Not a member yet? Register   Sign In
set_value, select boxes and array fieldnames
#1

[eluser]oscaralexander[/eluser]
I'm completely baffled by something weird happening in my application. I have two controllers, one named District, another named Venue. Both controllers administer related models and feature a form where you select the city to which the district or venue belongs. These select boxes are named as associative arrays: district[city_id] and venue[city_id] to allow for easy record creating/updating.

When each form is submitted and validation fails, the form is redisplayed with the posted data. Since I share a view template for both creating and updating a record, I determine the selected city with this line of code:

Code:
$city_id = set_value('district[city_id]', isset($district) ? $district->city_id : false);

And somehow, for a reason that has me stupefied for over two hours already, the code works for the district form, yet fails at the (nearly identical) venue form. Even when I strip all other form fields, validation and other code from the processing action. I've also tried outputting the following directly from the processing action:

Code:
// in District::create()
echo set_value('district[city_id]'); // works

// in Venue::create()
echo set_value('venue[city_id]'); // fails
echo $_POST['venue']['city_id']; // works

Needless to say, I double-checked the field names a gazillion times.

Does anybody have any clue what's going on here? I've officially lost my mind. Thanks!
#2

[eluser]oscaralexander[/eluser]
Found it! Sheesh! Apparently the venue[city_id] value is only filled if validation is performed on that field. I am finally getting my city ID returned after I added this rule:

Code:
$this->form_validation->set_rules('venue[city_id]', 'City', 'required|integer');

Whadda ya know...
#3

[eluser]pickupman[/eluser]
If you take a look at the set_value() function it is checking if the field name exists in the _field_data object. The _field_data object is created from the set_rules() function from the field names that are passed to it.




Theme © iAndrew 2016 - Forum software by © MyBB