2008/8/31 Malcolm <[EMAIL PROTECTED]>: > On Tuesday 26 August 2008, Carl Franks wrote: > >> >> > Is there a good way of doing a combo-box type element for a form? I've >> >> > tried several variations, but so far I haven't found anything that >> >> > doesn't end up requiring custom coding on the handler. >> >> > >> >> > This is the closest I've gotten, but their doesn't seem to be a way of >> >> > enforcing the SingleValue constraint. > >> >> Because Multi is also a Field-element, when you add a constraint to >> >> it, it doesn't get added to its children, unlike with other Block >> >> elements. >> >> If you add the constraint to one of the "country" fields, it should >> >> work. > >> > The reason I tried it on the Multi element is that it didn't work on the >> > member fields, as there's two child elements with the same name, >> > SingleValue fails. > >> I've added a test for what I think you're describing, and it seems to work >> okay. Can you have a look, and let me know if I'm not getting it? > >> > http://code.google.com/p/html-formfu/source/browse/trunk/HTML-FormFu/t/elements/combo_box.t >> > http://code.google.com/p/html-formfu/source/browse/trunk/HTML-FormFu/t/elements/combo_box.yml > > If I plug in the contents of combo_box.yml into my form it doesn't work, > so one of us is missing something. > > There are two issues with the combo_box.yml: > > * If the form is filled with default values (which I do, using > $form->model->default_values(...)), then both fields are filled out, > resulting in a definite double, if identical, value if submitted unaltered. > > * Even with no text in the text box, the SingleValue constraint is still > failing. > > > What I think is wrong with the test is that it's not actually doing a round > trip through the generated html, which looks like this: > > <form action="" method="post"> > <fieldset> > <div class="multi"> > <span class="elements"> > <select name="foo"> > <option value="one">One</option> > <option value="two">Two</option> > <option value="three">Three</option> > </select> > <input name="foo" type="text" /> > </span> > </div> > <div class="submit"> > <input name="submit" type="submit" /> > </div> > </fieldset> > </form> > > When you submit that form, the text field has an empty value, not a > non-existant one. > > There's also a missing test case of the "nothing selected from dropdown, but > text in field" variant, as there's no available empty value from the > dropdown.
I've added a new ComboBox element. You can build it just as you would a Select field, using options(), values() or value_range() - but under the hood it's really a Multi element, like the Date field. type: ComboBox name: foo values: [yes, no] If you set $field->default() to a value that's in the Select menu, it'll set that Select option to selected="selected". If you set $field->default() to an unknown value, it'll set the Text field's value="$default". It sets the Select's empty_first() to true - so there's always an empty option. If you both select an option from the Select menu *and* fill in the text field, it'll only use the value from the text field. Like the Date field, you must always call $form->process() before rendering the form. Like any other field, you retrieve any submitted value with $form->param_value('foo') I've also added tests for Model-DBIC, to ensure it works with options_from_model(), default_values and update(). Like the Date field, any constraints or other processors are added to the ComboBox itself - not to its child elements. I haven't yet put it into use in a real application, though I hope to do so in the next week. - Let me know if you come across any problems, or can think of any more tests I've missed. Cheers, Carl _______________________________________________ HTML-FormFu mailing list HTML-FormFu@lists.scsys.co.uk http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu