HTTP Requests
Once a form has been submitted, you'll need to get the posted values from the HTTP request. There are many ways you can achieve this... all using a single method.
To get a field's value, simply pass the field's name to the request() method and receive a string with the value in return.
$name = $form->request('name');
Auto-Rules
Here's the easiest way to handle form validation: just call request() without any arguments! Flick will automatically use the validation rules you already defined in create().
if ($form->submitted()) { $data = $form->request(); } $form->create('Name[min:3, required], Email[email, required], Comments');
That's it! The rules you added to create() (like min:3 and email) are automatically applied when the form is submitted.
Tip
This is the recommended way to validate forms. Write your rules once, and Flick takes care of the rest.
Info
Auto-rules require a session to remember the rules from create(). Flick starts one automatically if needed.
Using Different Rules on Submit
Sometimes you might want stricter validation when the form is actually submitted. No problem — just pass rules to request() and they'll be used instead:
if ($form->submitted()) { // Require a longer name and a non-empty comment, only on submit $data = $form->request('Name[min:5, required], Email[email, required], Comments[required, min:10]'); } $form->create('Name[min:3, required], Email[email, required], Comments');
Validation Arrays
You can pass an array of validation rules to the second parameter.
$name = $form->request('name', ['min:3', 'max:32']);
You can also pass an array of custom validation messages to the third parameter.
$name = $form->request('name', ['min:3', 'max:32'], [ 'min' => 'Must be at least 3 characters', 'max' => 'Cannot be more than 32 characters' ] );
Get HTTP request data for a form created with an array.
$array = [ 'fields' => [ 'name' => [ 'label' => 'Name', 'rules' => ['min:3', 'required'], 'messages' => ['min' => 'Name must be at least 3 characters.'] ] ] ]; if ($form->submitted()) { $data = $form->request($array); } $form->create($array);
Tip
The name key is optional. Leave it out and the field is both rendered and read
back under its array key, so a field keyed full_name posts as full_name. Set
name only when you want it to differ from the key.
The array form returns a list of one-key arrays, one per field — not the flat map you get from the string form:
$data = $form->request($array); // [ ['name' => 'Gern Blanston'] ] // so reach in by position, or flatten it first $data = array_merge(...$form->request($array)); $name = $data['name'];
Get HTTP request data for a form created with a file.
// form files carry their own id ('form-login' here) — set it in the config so // the submission is recognized when create('/login') hasn't run yet this request $form = new Flick\Flick(['id' => 'form-login']); $data = $form->request('/login'); // [ ['username' => 'gern'], ['password' => 'hunter22'] ]
Validation Strings
Arrays are great, but they can also be a little time-consuming to type; why not use a string instead?
Get request data for a form created with a string.
$data = $form->request('Name, Email, Comments'); // get the value of the `name` field $name = $data['name'];
Add validation rules by enclosing them in square brackets, and separating the rule name from the match value with a colon.
$data = $form->request('Name[min:3, max:32], Email[email], Comments');
Add custom error messages by inserting an "array" of square brackets after the validation rules, then add the rule name, a colon, and the message string.
$data = $form->request(' name[min:3, max:32][min:Must be at least 3 characters, max:Cannot be more than 32 characters], email[email], comments' );
Warning
Keep both bracket groups attached to the field name. Splitting the brackets onto
their own lines (whitespace between name and [, or between ][) breaks the
parser, and Flick silently falls back to its built-in messages.
Complete Example
Here's a full working example that validates form input and saves it to a database.
<?php require 'vendor/autoload.php'; $form = new Flick\Flick([ 'views' => 'bootstrap', 'services' => [ 'sql' => [ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'myapp', 'username' => 'root', 'password' => 'secret' ] ] ]); $db = $form->sql; // Handle form submission if ($form->submitted()) { $request = $form->request(' Name[required, min:2, max:60], Email[required, email], Phone[phone], Message[required, min:10] '); if ($form->ok()) { // Save to database (useTimestamps sets created_at/updated_at for you — // a hand-supplied created_at would be stripped by mass assignment protection) $db->table('contacts')->useTimestamps()->save([ 'name' => $request['name'], 'email' => $request['email'], 'phone' => $request['phone'], 'message' => $request['message'] ]); $form->successMessage('Thank you for your message!'); } else { $form->errorMessage('Please fix the errors below.'); } } // Create the form $form->create('Name, Email, Phone, Message|textarea');
Tip
For even simpler code, use createAndValidate() which handles the submission check, validation, and messaging automatically. See Creating Forms. In Laravel/Blade, use renderValidated() instead.
Related
- Request Adapters — Inject custom request data for framework integration and testing