Welcome Guest, Not a member yet? Register   Sign In
Form validation and SELECTs
#1

[eluser]jppi_Stu[/eluser]
(Solved: Disregard!)

I must be missing something obvious. When I submit a form that fails validation and the form is redisplayed, how is the browser ignoring the SELECTED attribute in an OPTION of a SELECT menu?

Here's an example... Let's say I have a form that includes the following:
Code:
<?php
    # Decode the cheese field
    if ($this_food->cheese == 'Cheddar') {
       $dbcheddar = TRUE;
       $dbwensleydale = FALSE;
   }
   else {
       $dbcheddar = FALSE;
       $dbwensleydale = TRUE;
   }
?>
<select id="cheese" name="cheese">
  <option value="">Choose your cheese</option>
  <option value="Cheddar" &lt;?php echo(set_select('cheese','Cheddar',$dbcheddar)); ?&gt;>Cheddar</option>
  <option value="Wensleydale" &lt;?php echo(set_select('cheese','Wensleydale',$dbwensleydale)); ?&gt;>Wensleydale</option>
</select>
When the form is first shown, the data is preloaded from a database, and the generated code might look like this:
Code:
<select id="cheese" name="cheese">
  <option value="">Choose your cheese</option>
  <option value="Cheddar" selected="selected">Cheddar</option>
  <option value="Wensleydale">Wensleydale</option>
</select>
Cheddar will be preselected for me, per the value in the database. I then cleverly change my cheese selection to Wensleydale. However, I not-so-cleverly neglect some other part of the form, causing $this->form_validation->run() to return False. The form will redisplay, and (via validation_errors()), I get a message indicating where I went wrong. The important part is that Wensleydale will remain selected -- yet examining the HTML code for the redisplayed form shows the SELECTED attribute still on the Cheddar option! Clearly this does not seem correct, and not just because I dislike cheddar cheese. Tastes in cheese aside, I wouldn't normally care as long as everything works out in the end, but I have a SELECT menu with a nasty habit of not exhibiting this behavior, no matter how I try to retain the user's choice. That's what made me look at the generated HTML code to begin with.

(As an aside, I have to wonder about the selected="selected" result of set_select() given that selected is a boolean attribute.)

So... what am I missing?
#2

[eluser]Watermark Studios[/eluser]
It seems to me that your object $this_food is not retaining the changes due to the validation error. I would create a trigger that maintains the state of the selected option despite validation. In other words, create another variable that holds the value you want it to hold instead of asking the invalid object to hold that value.

As a note, you can use SELECTED, SELECTED = "TRUE", SELECTED = "SELECTED. Any one of those attributes will have the same results. If I remember correctly, assigning anything to SELECTED makes it valid. "When set, this boolean attribute specifies that this option is pre-selected." - from www.w3.org

Thanks,

Ken
#3

[eluser]jppi_Stu[/eluser]
Thanks for the input.

What is most confusing to me is how the browser is displaying something that is not supported by the underlying source HTML, as far as pre-selecting an OPTION in the SELECT menu. (However, the validation failure message is visible in the source and the output, so clearly the browser is heeding the source to that extent.)

In fact, if I do a "view source" and copy the whole thing to a new file, and open that new .html file in a browser, it shows what I would expect -- in the example case, Cheddar would be pre-selected because that is what has the SELECTED attribute set. It's only within the context of CodeIgniter redisplaying the form that the underlying source does not match the browser output (and I've checked multiple browsers to see if it was possibly browser-specific).

I'm not too worried about getting the right result in the underlying HTML, as I'm sure I can make that happen, whether or not it's in a CI-friendly way or not. But to have the browser not pre-select the OPTION that has SELECTED set is confounding me.

FWIW, regarding my cheesy example, the controller is loading $this_food like this:
Code:
function grocery_prefs()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('Grocerymodel');
if ($this->form_validation->run() == FALSE)
  {
  $query = $this->Grocerymodel->grocery_detail($shopper_id);
  $data['this_food'] = $query[0];
  $this->load->view('food_edit_v.php', $data);
  }
# etc...
}
#4

[eluser]danmontgomery[/eluser]
selected="selected" is the correct XHTML standard syntax... the same with checked="checked", disabled="disabled", etc. http://www.w3schools.com/Xhtml/xhtml_syntax.asp

What you're saying doesn't make sense... The browser is completely ignorant of whether you're running CI or not. What you're seeing is probably a browser cached page, or a function of some browser addon.
#5

[eluser]jppi_Stu[/eluser]
[quote author="noctrum" date="1287542925"]What you're saying doesn't make sense... The browser is completely ignorant of whether you're running CI or not. What you're seeing is probably a browser cached page, or a function of some browser addon.[/quote]
I agree it doesn't make sense -- which is why I'm puzzled enough to ask here about it. :cheese: The page isn't cached since the validation error message is present. And it's not a browser add-on, as I've tested it in multiple browsers, including browsers I wouldn't normally use and thus wouldn't customize (not that I'm the type to use add-ons anyway, actually).

(Oh, and thanks for the XHTML info. See what happens when you get away from serious Web development for a few years?)
#6

[eluser]jppi_Stu[/eluser]
OK! :red: When things don't make sense, it means you're missing something. Right?

So, now things make sense again, because I found what I was missing... I thought I was being diligent in checking this scenario in multiple browsers, but that's where I missed something. This strange scenario ended up being limited to one browser -- Google Chrome in OpenSUSE 11.3 x86_64, to be specific -- but I didn't catch what I'd missed until this evening. Viewing the source in Chrome actually was viewing the (cached?) pre-submit/validation version of the page. The validation failure message was appearing in the source for all the browsers except Chrome. Too hasty, too sleepy, whatever... Just glad I found the quirk that was throwing me off.

Why I was using Chrome as my development browser is another matter entirely. I think I'll go back to Opera or Firefox. :roll:
#7

[eluser]SitesByJoe[/eluser]
Firefox on Mac has "sticky" select menus too. If you just hit refresh while testing forms it'll deceive you and ignore your selected="selected".

This issue has driven me nuts working on forms many times.




Theme © iAndrew 2016 - Forum software by © MyBB