return function () {
$intro = '<p class="docs-demo-intro">A sign-up form checked on the server by <code>form()->validate()</code>: built-in rules
(<code>required</code>, <code>valid_email</code>, <code>min_length</code>, <code>matches</code>, <code>integer</code>), a custom closure rule
and a rule on a field inside <code>row_start()</code>. Errors are shown on the fields; a valid form shows the cleaned values and stores nothing.
Classes: <code>yf_form2</code>, <code>yf_validate</code>.</p>';
// A custom rule is any callable: ($value, $param, $all_data, &$error_msg), true when the value is valid
$login_rule = function ($in, $param, $data, &$error_msg) {
if ( ! preg_match('~^[a-z][a-z0-9_]{2,19}$~', (string) $in)) {
$error_msg = 'Username: 3 to 20 lower case letters, digits or "_", starting with a letter';
return false;
}
if (in_array($in, ['admin', 'root', 'yf', 'support'], true)) {
$error_msg = 'Username "' . $in . '" is reserved, please choose another one';
return false;
}
return true;
};
$result = '';
// novalidate: the browser would block the bad values (form2 adds HTML5 attributes from the rules) before the server sees them
$form = form((array) $_POST, ['show_alerts' => true, 'novalidate' => 1])
->validate([
'__form_id__' => 'docs_validate_demo',
'name' => 'trim|required|min_length[2]|max_length[64]',
'email' => 'trim|required|valid_email',
'username' => ['trim', 'required', $login_rule],
'pass' => 'required|min_length[8]',
'pass2' => 'required|matches[pass]',
'age' => 'trim|integer|greater_than_equal_to[13]|less_than_equal_to[120]',
'phone_number' => 'trim|numeric|min_length[6]|max_length[12]',
'agree' => 'required',
])
->on_validate_ok(function ($data) use (&$result) {
$rows = [];
foreach (['name', 'email', 'username', 'age', 'phone_code', 'phone_number'] as $k) {
$rows[] = '<tr><th>' . $k . '</th><td>' . _prepare_html((string) ($data[$k] ?? ''), 0) . '</td></tr>';
}
$result = '<div class="alert alert-success"><b>Valid.</b> A real site would save these values now; this demo only shows them.</div>'
. '<table class="table table-condensed table-sm table-bordered docs-demo-result">' . implode('', $rows) . '</table>';
})
->text('name', 'Full name')
->email('email')
->text('username', ['tip' => 'Try "admin": a custom closure rule refuses it'])
->password('pass', 'Password', ['tip' => 'At least 8 characters'])
->password('pass2', 'Repeat password')
->number('age', ['class_add' => 'input-small'])
->row_start(['desc' => 'Phone', 'name' => 'phone'])
->select_box('phone_code', ['+1' => '+1', '+44' => '+44', '+380' => '+380'], ['class_add' => 'input-small'])
->text('phone_number', ['placeholder' => 'digits only', 'class_add' => 'input-medium'])
->row_end()
->check_box('agree', ['desc' => 'I accept the terms', 'no_label' => 1])
->submit(['value' => 'Sign up']);
// A failed CSRF check logs into the log_csrf_errors table; the demo writes no rows
$guard = _class('csrf_guard');
$log_errors = $guard->LOG_ERRORS;
$guard->LOG_ERRORS = false;
try {
$html = (string) $form;
} finally {
$guard->LOG_ERRORS = $log_errors;
}
return $intro . $result . $html;
};