How to trigger form attribute validation

I’m wanting to trigger the validation of an attribute whenever another attribute on the form is changed.

I tried

'onchange' => '$("#projects").yiiActiveForm("validateAttribute", "projects-imessageid");',

but this generates the following error that I simply do not understand

Uncaught TypeError: $(…).data(…) is undefined

Can anyone help?

I also tried simply validating the entire form

'onchange' => "$('#projects').data('yiiActiveForm').submitting = true;$('#projects').yiiActiveForm('validate');",

but that gives the same error.

To give a little context, the reason I’m needing to do this is because I’m using a conditional required rule

['IMessageId', 'required', 'when' => function($model) {
	return (($model->source->Source == 'E-mail') || ($model->source->Source == 'Website'));
}, 'whenClient' => "function (attribute, value) {
	return (($('#projects-sourceid option:selected').text() == 'E-mail') || ($('#projects-sourceid option:selected').text() == 'Website'));
	// return ($('#projects-sourceid').val() == 1);
}"],

and the validation of the attribute does not get reset/reevaluated in the form when the dependent attribute is changed, to my surprise.

Use jQuery instead of $, like this:

'onchange' => 'jQuery("#projects").yiiActiveForm("validateAttribute", "projects-imessageid");'

Basically there is no $ (dollar sign) defined in your javascript, and most likely jquery was defined with the keyword jQuery.