Clone wiki

FrontendUser / Code snippets / Examples

Code snippets / examples

Form field sanitizing

Set form field sanitizer (FormHelper feature)

FormHelper add an additional attribute to the form field to set a sanitizer during field definition.

// PW sanitizer "email"
$emailField->fhSanitizer = 'email';

Get sanitized form field values (FormHelper feature)

Use FormHelper to get the sanitized form field value. It use the sanitizer set to the "fhSanitizer" attribute.

$value = $fu->form->fhValue('emailField')

Get field value with changed sanitizer (Formhelper feature)

// Get field value, but use sanitizer "test"
$value = $fu->form->fhValue('emailField', 'text')

PW native value sanitizing

You also can use the PW native sanitzer.

$currentField = $fu->form->get('myFormField');
$value = $currentField->value;
$sanitizedValue = wire('sanitizer')->pageName($value);

For example inside a hook fired after form field processed

// hook after field processed (form need to be submitted)
$myField->addHookAfter('processInput', function($event) {
  $currentField = $event->object;

  $value = $currentField->value;
  §sanitizedValue = wire('sanitizer')->pageName($value);

  // Do ...

Add custom form field with hook after field processed

Add an additional form field to the registration form with field processing (add value to user object) inside your template file.

$myField = $modules->get('InputfieldText');
$myField->label = $this->_('MyLabel');
$myField->attr('id+name', 'MyField');
//$myField->required = 1;
$myField->fhSanitizer = 'text';

// Call hook after field is processed by PW form api
$myField->addHookAfter('processInput', function($event) {
    $field = $event->object;

    // Value will be sanitized as text with "$sanitizer->text()"
    $mySanitizedCustomInput = wire('fu')->form->fhValue($field->name);

    // Do ...

    // Example: Add value to user during registration
    wire('fu')->userObj->myField = $mySanitizedCustomInput;

    // Need to set an field error?
    // $field->error('Custom field has an error...');

// Define the field before you call FU and add it as additional field...
$fu->register(array('username', 'email', 'password', $myField));