EE Docs. Also details Ajax handling of XID.
As Bhashkar stated:
$this->EE->functions->form_declaration($data);
Or the new method:
ee()->functions->form_declaration($data);
"so the XID (secure hash ID) is added automatically as a hidden input
field."
As part fo the $data array sent you can force secure by adding it:
$data['secure'] = TRUE;
Example of complete code:
$formDetails = array('action' => '',
'name' => 'filtered_listing',
'id' => $this->EE->TMPL->form_id,
'class' => $this->EE->TMPL->form_class,
'hidden_fields' => array('ACT' => $actionID,
'RET' => $_SERVER["REQUEST_URI"]),
'secure' => TRUE
);
$header = $this->EE->functions->form_declaration($formDetails);
If you're checking the receipt of the form and need to validate the XID...
//### Check Security Hash ###
if ($this->EE->security->secure_forms_check($this->EE->input->post('XID')) == FALSE) {
//### Deny Form submission if a hash isn't found or is too old
$this->EE->functions->redirect(stripslashes($this->EE->input->get_post('RET', false)));
}
Update based on Form Helper example code
Try Outputting {XID_SECURE_HASH}
Reference
form_hidden('XID', '{XID_SECURE_HASH}')
Failing that:
form_hidden('XID', $this->EE->functions->add_form_security_hash('{XID_HASH}'));
Ref: Rob Sanchez
Form Helper Documentation