Page MenuHomePhabricator

Implement getFormValues()
Open, LowPublic

Description

I want to be able to build a json object that reflects the key/value pairs that a whole OO.ui.FormLayout would submit were it to be POST'd.

I would expect this object to look like {element_name: element_value, ...} for all the named elements in the form.

Since OOjs UI provides the ability to set a "data" value which is far superior to the string-only "value" attribute that is set in an HTML element, I'd like to also request to be able to prefer the value of a "data" elements when they are available.

Using it might look something like this:

var option1 = new OO.ui.ButtonOptionWidget( {data: 1, label: 'Option 1'} );
var option2 = new OO.ui.ButtonOptionWidget( {data: 2, label: 'Option 2'} );
var option = new OO.ui.ButtonSelectWidget( { name: "option", items: [ option1, option2 ] } );
var text = new OO.ui.TextInputWidget( { name: 'text' } );

var fieldset = new OO.ui.FieldsetLayout( { label: 'Some fields' } );
fieldset.addItems( [
	new OO.ui.FieldLayout( option, { label: "Which option?" } ),
	new OO.ui.FieldLayout( text, { label: "What text?" } )
] );
var form = new OO.ui.FormLayout( { items: [ fieldset ] } )
$( 'body' ).append( form.$element );

// Some typing and clicking happens 

form.getValues( { preferData: true } )
/* returns
{
	option: 2,
	text: "Some text that was typed"
}
*/

Event Timeline

Halfak raised the priority of this task from to Needs Triage.
Halfak updated the task description. (Show Details)
Halfak added a project: OOUI.
Halfak added subscribers: Halfak, Catrope.

This is not exactly what you're asking for, but might be close enough to what you need: when using only InputWidgets in the form, you can use existing methods to do this, such as the jquery.form jQuery plugin. It provides the $.fn.formToArray method (which returns an array like [ { name: 'foo', value: 'bar' }, … ], not a { foo: 'bar', … } object as you'd like, though), which you could use by calling form.$element.formToArray().

Jdforrester-WMF set Security to None.