I'm using rules module to automatically create a webform on a node type. I can easily do everything except setup validation on a particular field.

Is there a way to programatically load a webform after it's created and add a particular type of validation to a particular field? Basically instead of clicking "webform validation" and setting up validation in the UI, I need to do it in code.

Any advice?

Comments

stewart.adam’s picture

I needed to do this too for my module. I ran SELECT queries on the {webform_validation_rule} and {webform_validation_rule_components} tables before/after adding a validator to a test node and then found the code in the module that inserted into said tables. It looks like adding a validator to a web form is handled by the webform_validation_rule_save() function, which takes the its data from add validator form.

If you need to add a handler programatically, add a new line in webform_validation_rule_save():

drupal_set_message(print_r($values, TRUE));

Then go ahead and add a validator to any web form. After adding the validator you should see the required structure for $values that you can mimic in your module.

attisan’s picture

hi, I'm looking for exact the some thing too - though I'm a bit shocked there is no "simple" way to accomplish this.

I though it to be something like another entry in the array ...

	$components[] = array(
		'cid' => $i,
		'pid' => '0',
		'form_key' => 'string' . $i,
		'name' => 'name',
		'type' => 'textfield',
		'value' => '',
		'extra' => array(
			'title_display' => 'inline',
			'private' => 0,
			'disabled' => 0,
			'unique' => 0,
		),
		'mandatory' => '0',
		'weight' => $i,
		'page_num' => 1,
	);

and to this I would have added my validation rule:

	$components[] = array(
		/* ... */
		'validation' => 'myPersonalValidationsRule',
	);

the mentioned way (by stewart.adam) seems to me more a rumpy workaround. (no offense) ...

greets

attisan’s picture

Title: Setup a validation Programatically » setup / add webform validation programmatically

hi folks,

just in case someone would like to know how to add validation without much investigatory work:

programmatically create your node;
add your webform;
save the node;
prepare the validation;
run webform_validation_rule_save( $validation );

function customModule_add_node($newnode) {
	$node 			= new stdClass();
	$node->type		= "content_type_machine_name";

	node_object_prepare($node);

	$components = array();

	$i = 1;
	$components[] = array(
		'cid' => $i,
		'pid' => '0',
		'form_key' => 'custom-string-' . $i,
		'name' => t('Title'),
		'type' => 'select',
		'value' => '',
		'extra' => array(
			"items" => array('1|1','2|2'),
			"multiple" => 0,
			"title_display" => "before",
			"private" => 0,
			"aslist" => 0,
			"optrand" => 0,
			"conditional_operator" => "=",
		),
		'mandatory' => '1',
		'weight' => $i,
		'page_num' => 1,
	);

	$i++;
	$components[] = array(
		'cid' => $i,
		'pid' => '0',
		'form_key' => 'custom-string-' . $i,
		'name' => t('Other Titel'),
		'type' => 'date',
		'value' => '',
		'extra' => array(
			'timezone' => 'user',
			'title_display' => 'before',
			'private' => 0,
			'datepicker' => 1,
			'year_textfield' => 0,
			'start_date' => '+2 days',
			'end_date' => '+1 years',
			'webform_conditional_field_value' => '1',
			'webform_conditional_cid' => '1',
			'webform_conditional_operator' => '=',
			'description' => '',
			'conditional_component' => '',
			'conditional_operator' => '=',
			'conditional_values' => '',
		),
		'mandatory' => '0',
		'weight' => $i,
		'page_num' => 1,
	);

	$node->language		= LANGUAGE_NONE;
	$node->path['alias']		= 'node_path';
	$node->status			= 1;
	$node->uid			= 1;

	$node->title 							= $newnode['titel'];
	$node->body[$node->language][0]['value']	= $newnode['body'];

	
	$node->webform = array(
		'confirmation' => '',
		'confirmation_format' => 'full_html',
		'redirect_url' => '<confirmation>',
		'status' => '1',
		'block' => '0',
		'teaser' => '0',
		'allow_draft' => '0',
		'auto_save' => '0',
		'submit_notice' => '1',
		'submit_text' => 'Submit me',
		'submit_limit' => '1',
		'submit_interval' => '-1',
		'total_submit_limit' => '1',
		'total_submit_interval' => '-1',
		'record_exists' => TRUE,
		'roles' => array (
			0 => '1',
			1 => '2',
		),
		'emails' => array(),
		'components' => $components,
	);

	if( $node = node_submit($node) ) {
		node_save($node);

		// Add validation
		$validation = array(
			'validator'			=> 'validation_rule_machine_name',
			'action'			=> 'add',
			'nid'				=> $node->nid,
			'rulename'			=> 'Validation Rule One',
			'rule_components'	=> array(
					1		=> '1',
					2		=> '2',
			),
		);

		webform_validation_rule_save( $validation );

		return $node;
	}
}

greets

liam morland’s picture

Component: Code » Documentation
Category: support » task

If you write this up nicely, I can add it to the documentation.

liam morland’s picture

Issue summary: View changes
Status: Active » Fixed

References to functions included in handbook.

liam morland’s picture

Improved documentation of webform_validation_rule_save() and webform_dynamic_delete_rule().

http://drupalcode.org/project/webform_validation.git/commitdiff/17127a4

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.