Some times you need to bu sure that form values your computation requires are valid. Simple validation can be done by the cck basics but if you compute complex conditions that won't work. Calling form_set_error on the computed_field['code'] is simply too late to stop form handling.

I've build a simple patch to include a third code text area for validation code. This code get's evaluated at computed_field_field('validate',...). Pay attention: that does not match every call of _computed_field_compute_value().

I think that is only a minor change and should not go to another module.

Regards,
Canis Lupus

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Canis Lupus’s picture

Assigned: Canis Lupus » Unassigned
mfrosch’s picture

Version: 6.x-1.0-beta3 » 7.x-1.0-beta1
Assigned: Unassigned » mfrosch
FileSize
4.68 KB

Hallo,

thanks lupus for your inpiration. I migrate your patch to drupal 7.15. There are just slightly differences.

!         '#default_value' => !empty($settings['validate']) ? $settings['validate'] : '',
!         '#markup' => t('This field is computed using @validate_func().', array('@validate_func' => $validate_func)),

And no need for the patch from line 102 till end.

Instead the following function

function computed_field_field_validate($entity_type, $entity, $field, $instance, $langcode, &$items) 
{

	$validate_func = 'computed_field_'. $field['field_name'] .'_validate';
	if (function_exists($validate_func)) 
	{
         $validate_func($entity_type, $entity, $field, $instance, $langcode, &$items);
	} 
	else 
	{
		if (isset($field['settings']['validate'])) 
		{
       	
           	$res = eval($field['settings']['validate']);
           
           	if (!$res)
           	{
           		form_set_error($field['field_name'], t('Please verify the dates.'));
			}
        
		}

	}
}

Attached the changed *.module.

Cheers,
Frosch

Alan D.’s picture

Version: 7.x-1.0-beta1 » 7.x-1.x-dev
Status: Patch (to be ported) » Needs review
FileSize
2.25 KB

Still a couple of formatting issues, but this is an untested start :)

k.skarlatos’s picture

I get a WSOD when i apply this patch. I use php 5.4

PHP Fatal error:  Call-time pass-by-reference has been removed in /srv/http/epik2/sites/all/modules/computed_field/computed_field.module on                               line 265
Fatal error: Call-time pass-by-reference has been removed in /srv/http/epik2/sites/all/modules/computed_field/computed_field.module on line                               265