1. Go to this page and download the library: Download molajo/fieldhandler library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
molajo / fieldhandler example snippets
// 1. Instantiate the Molajo Fieldhandler and inject $fieldhandler into class
$fieldhandler = new Molajo\Fieldhandler\Request();
// 2. Verify the password is still valid
$results = $request->validate('Last Changed', $last_changed, 'Date', array('LT' => 91, 'Context' => 'Days');
if ($results->getValidateResponse() === false) {
// deal with the problem
$messages = $results->getValidateMessages();
}
// 3. Verify data values using the *Alphanumeric Constraint* and values (_), ($), and (#).
$results = $request->validate('Password', $password, 'Alphanumeric', array('special_characters' => '-, $, #');
if ($results->getValidateResponse() === false) {
// deal with the problem
$messages = $results->getValidateMessages();
}
// 4. Passwords must be from 8 to 30 characters in length.
$results = $request->validate('Password', $password, 'Length', array('minimum' => 8, 'maximum' => 30);
if ($results->getValidateResponse() === false) {
// deal with the problem
$messages = $results->getValidateMessages();
}
// 5. Display Password
$results = $request->escaoe('Password', $display_password, 'Password';
if ($results->getChangeIndicator() === true) {
$display_password = $results->getFieldValue();
}
// 1. Instantiate the Molajo Fieldhandler and inject $fieldhandler into class
$fieldhandler = new Molajo\Fieldhandler\Request();
// 2. Enforce Password Constraints using a terse syntax
$results = $request->ensureFieldConstraints(
'Display Password', $display_password,
array('verify' => 'date', 'verify' => 'Alphanumeric', 'verify' => 'Length', 'escape' => 'Password'),
array('LT' => 91, 'Context' => 'Days', 'special_characters' => '-, $, #' );
if ($results->getSuccessIndicator() === false) {
$field->messages = $results->getValidateMessages();
} elseif ($results->getChangeIndicator() === true) {
$field->value = $results->getFieldValue();
}
// 1. Instantiate the Molajo Fieldhandler and inject $fieldhandler into class
$fieldhandler = new Molajo\Fieldhandler\Request();
// 2. Process all fields in a loop
foreach ($data_object as $field) {
$results = $request->ensureFieldConstraints(
$field->name,
$field->value,
$field->tests,
$field->options);
if ($results->getSuccessIndicator() === false) {
$field->messages = $results->getValidateMessages();
} elseif ($results->getChangeIndicator() === true) {
$field->value = $results->getFieldValue();
}
}
$fieldhandler = new Molajo/Fieldhandler/Driver();
$response = $request->validate('alias_field', 'This will not validate', 'Alias');
if ($response->getValidateResponse() === true) {
// all is well
} else {
foreach ($response->getValidateMessages as $code => $message) {
echo $code . ': ' . $message . '/n';
}
}
$response = $request->sanitize('alias_field', 'Jack and Jill', 'Alias');
if ($response->getChangeIndicator() === true) {
$field_value = $response->getFieldValue();
}
$options = array();
$options['allow_space_character'] = true;
$response = $request->validate('employee_name', 'Pat 3Nelson', 'Alpha', $options);
if ($response->getValidateResponse() === true) {
// all is well
} else {
foreach ($response->getValidateMessages as $code => $message) {
echo $code . ': ' . $message . '/n';
}
}
// The value of field `numeric_field` is 'ABC123'. The filtered and escaped values will be 0.
// For 'validate', an exception is thrown. The following will return 123.
$results = $request->sanitize('numeric_field', '123', 'Int');
$response = $request->validate('integer_field', 100, 'Integer');
if ($response->getValidateResponse() === true) {
// all is well
} else {
foreach ($response->getValidateMessages as $code => $message) {
echo $code . ': ' . $message . '/n';
}
}
// The value of field `input_field` is '127.0.0.1'.
// Validate, filtered and escaped values will return the same.
$results = $request->sanitize('input_field', '127.0.0.1', 'Ip');
$options = array();
$options['allow_space_character'] = true;
$response = $request->validate('lower_field', 'This is lower', 'Lower');
if ($response->getValidateResponse() === true) {
// all is well
} else {
foreach ($response->getValidateMessages as $code => $message) {
echo $code . ': ' . $message . '/n';
}
}
$response = $request->format('lower_field', 'This is lower.', 'Lower');
if ($response->getChangeIndicator() === true) {
$field_value = $response->getFieldValue();
}
// The value of field `input_field` is 10. Maximum is 3. Validate will fail.
// Filtered and escaped values will return 3.
$field_name = 'my_field';
$field_value = 10;
$constraint = 'Maximum';
$options = array();
$options['maximum'] = 3;
$results = $request->validate($field_name, $field_value, $constraint, $options);
// The value of field `input_field` may not be null
$field_name = 'my_field';
$field_value = [email protected];
$constraint = 'Regex';
$options = array();
$options['regex'] = $regex_expression;
$results = $request->validate($field_name, $field_value, $constraint);
$response = $request->validate('random_field', $value, 'Notnull');
if ($response->getValidateResponse() === true) {
// all is well
} else {
foreach ($response->getValidateMessages as $code => $message) {
echo $code . ': ' . $message . '/n';
}
}
// The value of field `input_field` may not be null
$field_name = 'my_field';
$field_value = 'Lots of stuff in here that is stringy.';
$constraint = 'String';
$results = $request->validate($field_name, $field_value, $constraint);
// The value of field `input_field` may not be null
$options = array();
$options['from'] = 5;
$options['to'] = 10;
$results = $request->validate('My Field Name', $field_to_measure, 'Stringlength', $options);
$response = $request->validate('space_field', '\n \r \t', 'Space');
if ($response->getValidateResponse() === true) {
// all is well
} else {
foreach ($response->getValidateMessages as $code => $message) {
echo $code . ': ' . $message . '/n';
}
}
$response = $request->validate('upper_field', ' This is not trimmed. ', 'Upper');
if ($response->getValidateResponse() === true) {
// all is well
} else {
foreach ($response->getValidateMessages as $code => $message) {
echo $code . ': ' . $message . '/n';
}
}
$options = array();
$options['allow_space_character'] = true;
$response = $request->sanitize('upper_field', ' This is trimmed. ', 'Upper');
if ($response->getChangeIndicator() === true) {
$field_value = $response->getFieldValue();
}