getAFValue() in jquery.yiiactiveform.js

getAFValue() function in jquery.yiiactiveform.js is causing a trouble in one of my projects …

I’m not sure if it’s a bug or my mistake, but please take a look at it.




var getAFValue = function (o) {

	var type = o.attr('type');

	if (o[0].tagName.toLowerCase()=='span'){

		var c = [];

		o.find(':checked').each(function () {

			c.push(this.value);

		});

		return c.join(',');

	}

	if (type === 'checkbox' || type === 'radio') {

		return o.filter(':checked').val();

	} else {

		return o.val();

	}

};



In most of the case it works fine. But the parameter o[0] sometimes becomes undefined and the function will break. The function was called from the line 59 of the script.

Here is my work around.




var getAFValue = function (o) {

	var type = o.attr('type');

	if (typeof o[0] !== 'undefined' && o[0].tagName.toLowerCase()=='span'){

		var c = [];

		o.find(':checked').each(function () {

			c.push(this.value);

		});

		return c.join(',');

	}

	if (type === 'checkbox' || type === 'radio') {

		return o.filter(':checked').val();

	} else {

		return o.val();

	}

};



FYI, I’m using JuiTabs and JuiDatepicker in the problematic page, though I don’t know whether it’s relevant or not.

[EDIT]

Probably the same issue …

http://code.google.com/p/yii/issues/detail?id=3070

[EDIT 2]

OK, I’ve got it. It was just the same for me.

find() function in the line 59 was returning an input field rendered INSIDE a HTML comment, and getAFValue() was called with an unexpected value.

So I’ve deleted the HTML commented lines altogether, and the problem was solved.

But I’m afraid there should be many guys who will have the same problem when they upgraded yii to 1.1.9. ;)

I understand this can seem like a bug at first… but in the end it leads to better code…

Problem is that in this case of fields inside the HTML comments… you can see now that there is some JS code being executed… so if we add the check for “undefined”… the error would not be fired… and the developer would not even now that some JS is still being processed for that element… that can possibly create some unexpected problems as something is executed when you don’t want it to (commented input)…

So it’s my personal opinion that is better to get the error, find the reason for it and solve it properly… than to not have any error but have some code executed without knowing it…

Thanks, mdomba. I get your points.