Jump to content

Using action hooks to add, validate and send extra fields with inquiries


Alejandro

Recommended Posts

There are several inquiries hooks that can be used together to add extra fields to the form, validate them, and then include their content in the emails. Below you can find sample code that includes several different field types. 

This isn't a tutorial on using hooks. If you need help getting started refer to the Hooks developer documentation

<?php
defined('MVC_FRAMEWORK') or die;

Clickfwd\Hook\Action::add('inquiry_form_top', function($params)
{
	$Form = new FormHelper();
	?>
	<div class="jrFieldDiv">
	 
		<label for="jr-inquiry-phone">
			<?php __t("Phone Number");?><span class="jrIconRequired"></span>
		</label>
	 
		<?php echo $Form->text('data[Inquiry][phone]',[
			'id'=>'jr-inquiry-phone',
			'class'=>'jrText',
		]);?>
	 
	</div>

	<div class="jrFieldDiv">
	 
	    <label for="jr-inquiry-checkbox_example">
	    	<?php __t("Checkbox Example");?> <span class="jrIconRequired"></span>
	    </label>
	 
	    <?php echo $Form->checkbox('data[Inquiry][checkbox_example]',
	    	[
	    		'one'=>'One',
	    		'two'=>'Two',
	    		'three'=>'Three'
	    	],
	    	[
	    		'id'=>'jr-inquiry-checkbox_example',
	    		'option_class'=>'jr-option jrFieldOption',
	    	]
	    );?>
	 
	</div>

	<div class="jrFieldDiv">
	 
	    <label for="jr-inquiry-select_example">
	    	<?php __t("Select Example");?>:
	    </label>
	 
	    <?php echo $Form->select('data[Inquiry][select_example]',
	    	[
	    		''=>'Select Option', 
	    		'one'=>'One',
	    		'two'=>'Two',
	    		'three'=>'Three'
	    	],$selected = '',
	    	[
	    		'id'=>'jr-inquiry-select_example',
	    		'class'=>'jrSelect',
	    	]
	    );?>
	 
	</div>		

	<div class="jrFieldDiv">
	 
	    <label for="jr-inquiry-date_example">
	    	<?php __t("Date Example");?>
    	</label>
	 
	    <?php echo $Form->text('data[Inquiry][date_example]',[
	    	'id'=>'jr-inquiry-date_example',
	    	'class'=>'jrDate jr-date'
	    ]);?>
	 
	</div>	
	<?php
});

Clickfwd\Hook\Filter::add('inquiry_submit_validation', function($validation, $params)
{
	$required = [
		'phone' => 'Phone Number',
		'checkbox_example' => 'Checkbox Example',
		'select_example' => 'Select Example',
	];

	foreach($required as $key => $text)
	{
		$found = S2Array::get($params,"data.Inquiry.{$key}");

		if ( !$found ) 
		{
			$validation[] = "{$text} is required";
		}
	}

	return $validation;
}, 10);

Clickfwd\Hook\Action::add('inquiry_email_extra_fields', function($extra_fields, $params)
{
	$fieldLabels = [
		'phone' => 'Phone Number',
		'checkbox_example' => 'Checkbox Example',
		'select_example' => 'Select Example',
		'date_example' => 'Date Example'
	];

	if (empty($extra_fields))
	{
		return;		
	}

	foreach ($extra_fields as $key => $value):
	?>
		<p><?php echo $fieldLabels[$key]; ?>: <?php echo is_array($value) ? implode(', ',$value) : $value; ?></p>
	<?php
	endforeach;
});

 

 

  • Thanks 1
Link to post
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Please refer to our Privacy Policy.