I tried to create a Module as CCK Field, that provides functionality to add custom validation code to any node-type via cck.

http://www.derhasi.de/cck_validation

http://www.derhasi.de/system/files/cck_validation.module
http://www.derhasi.de/system/files/cck_validation.info

When creating the Field, behaviour seems to be correct. But when I try to edit the content-field, there are two Errors.

  1. After submitting configuration the form_set_error set in the validation code, is called. It seems that hook_field($op=validation) is called. So changed field settings cannot be saved.
  2. After pressing on the Configuration submit button, the error "The default value is invalid." appears.

Can anyone help me with these problems?

Comments

mooffie’s picture

There's a 'default value' box on the settings form, which doesn't make sense in your case (to see what I'm talking about, create a number/text/options field and see the 'default value' box). You can use the 'callbacks' field $op to tell CCK not to provide this 'default value' entry.

derhasi’s picture

Thank you!

I solved the problem by adding:

	case 'callbacks':
      return array(
        'default value' => CONTENT_CALLBACK_NONE,
      );

to cck_validation_widget_settings(). I think that's what you meant.

Now the custom Form_set_error-Message at configuration page did disappear too. I don't have a clue why, but it works!

derhasi’s picture

First working Version available. Feel invited to test it and leave your comments.

http://www.derhasi.de/cck_validation

dkruglyak’s picture

Two things:

1) Can we add a field to enter custom validation via JavaScript, to avoid a trip to server
2) Can we create a project on Drupal.org to have proper development & issue queue ?

derhasi’s picture

1) Can we add a field to enter custom validation via JavaScript, to avoid a trip to server

Unfortunatley I don't have any knowledge in JavaScript. But if you are able to do so, sure we can extend the module.

2) Can we create a project on Drupal.org to have proper development & issue queue ?

I don't have any experience with creating a project, cvs and so on. But this will be the best way to extend the modules features. Though due to lack of time, I cannot assure propper support.

derhasi’s picture

CCK Validation is now available as Drupal Contribution/Project on http://drupal.org/project/cck_validation

I still fight to create the first release version there, but haven't been successfull till now. :/

chaloalvarezj’s picture

Thank you for this great module... It's a must to validate CCK fields. Excuse my ignorance, but I would like to know what the field and widget options are for? and how different the behaivior would be with each option?
Cheers!

derhasi’s picture

The difference of field and widget is the format of the variables you get. Especially in Fields like Date this does matter. While Widget-validation proofs on the Input format, field validation will proof on the storage format.

for example Date-Field:
widget: Array of (day, month, year,...)
field: DATETIME-String

You can also find some examples in the CCK Validation README.txt

chaloalvarezj’s picture

Thank you... I think I understand now... One more thing, how should I handle multiple value fields? The validation rules should be written for each delta?

derhasi’s picture

You can validate multiple values by running loops.
Multiple values are saved like

 $node->field_text[0]['value']
$node->field_text[1]['value']

so you have to loop the first array.

thedrupalguy’s picture

Can anybody please tell me how can I add custom validation on CCK field in Drupal 6?
I found somewhere I will need CCK_validation module but its not available for D6.

Can anybody please help me out?

Thanks,
Fernando

derhasi’s picture

You can use Rules for that.