Skip to content

Commit

Permalink
Update the README.md file
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Jul 23, 2015
1 parent 941a79f commit 76a4d98
Showing 1 changed file with 127 additions and 9 deletions.
136 changes: 127 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ OVERVIEW
With it, you can have complete control over how a form looks and functions as well
as granular control over field validation. Features include:

* Field element configuration and creation
* Field element creation and configuration
* Validation
+ Use any callable validation object, such as `pop-validator` or custom validators
* Filtering
Expand Down Expand Up @@ -76,7 +76,7 @@ So a few things are going on in the above example:

1. We created the form object and gave it an 'id' attribute.
2. We created the individual field elements setting their name, label, attributes, validators, etc.
3. We added the field elements to the form object
3. We added the field elements to the form object.
4. We checked for a $_POST submission. If not detected, we just render the form for the first time.
5. If a $_POST submission is detected:
1. Set the field values with the values in the $_POST array
Expand Down Expand Up @@ -141,7 +141,7 @@ If it fails validation, it will render with the errors. In this case, the userna
### Using field configurations

We can do the same thing as above with a field configuration array,
which helps streamline the process a bit:
which helps streamline the process:

```php
use Pop\Form\Form;
Expand Down Expand Up @@ -273,30 +273,30 @@ And it will render like this:
Similarly, you could use a PHP file template. Consider the PHTML file `form.phtml`:

```php
<form action="<?php echo $action; ?>" method="<?php echo $method; ?>">
<form action="<?=$action; ?>" method="<?=$method; ?>">
<table>
<tr>
<td>
<?php echo $username_label . PHP_EOL; ?>
<?=$username_label . PHP_EOL; ?>
</td>
<td>
<?php echo $username . PHP_EOL; ?>
<?=$username . PHP_EOL; ?>
</td>
</tr>
<tr>
<td>
<?php echo $email_label . PHP_EOL; ?>
<?=$email_label . PHP_EOL; ?>
</td>
<td>
<?php echo $email . PHP_EOL; ?>
<?=$email . PHP_EOL; ?>
</td>
</tr>
<tr>
<td>
&nbsp;
</td>
<td>
<?php echo $submit . PHP_EOL; ?>
<?=$submit . PHP_EOL; ?>
</td>
</tr>
</table>
Expand Down Expand Up @@ -346,12 +346,130 @@ and it would render like this:

### Filtering

As mentioned above, when dealing user-submitted values, it's a bad idea to use them or
display them back on the screen without filtering them. A common set a filters to employ
would be `strip_tags` and `htmlentities`. So in the first example, we would add filters
to the $_POST block:

```php
if ($_POST) {
$form->addFilter('strip_tags')
->addFilter('htmlentities', [ENT_QUOTES, 'UTF-8']);
$form->setFieldValues($_POST);
if (!$form->isValid()) {
echo $form; // Has errors
} else {
$form->clearFilters();
$form->addFilter('html_entity_decode', [ENT_QUOTES, 'UTF-8']);
echo 'Valid!';
}
} else {
echo $form;
}
```

Of course, the `strip_tags` filter will strip out and possible malicious tags. The `htmlentities`
filter is useful if the form has to render with the values in it again:

```html
<input type="text" name="username" id="username" value="Hello&quot;World&quot;" required="required" size="40" />
```

Without the `htmlentities` filter, the quotes within the value would break the HTML of the input field.
Of course, if you want to use the values after the form is validated, then you have to call `clearFilters()`
and filter the values with `html_entity_decode`.

[Top](#basic-usage)

### Validation

Of course, one of the main reasons of using a form component such as this one is the leverage
the validation aspect of it. You've already seen the use of a basic validator from the `pop-validator`
component and those are easy enough to use. But, you can create your own custom validators by
either extending the `pop-validator` component with your own or just writing your own custom
callable validators. The only real rule that needs to be followed is that the custom validator
must return null on success or a string message on failure that is then used in error display.
Here are some examples:

##### Using a closure

```php
$username = new Input\Text('username');
$username->addValidator(function ($value) {
if (strlen($value) < 6) {
return 'The username value must be greater than or equal to 6.';
}
});
```

##### Using a custom class

```php
class MyValidator
{
public function validate($value)
{
if (strlen($value) < 6) {
return 'The password value must be greater than or equal to 6';
}
}
}

$username = new Input\Text('username');
$username->addValidator([new MyValidator(), 'validate']);
```

[Top](#basic-usage)

### Dynamic fields from a database table

The `pop-form` comes with the functionality to very quickly wire up form fields that are mapped
to the columns in a database. It does require the installation of the `pop-db` component to work.
Consider that there is a database table class called `Users` that is mapped to the `users` table
in the database.

For more information on using `pop-db` [click here](https://github.com/popphp/pop-db).

```php
use Pop\Form\Form;
use Pop\Form\Fields;

$fields = new Fields(Users::getTableInfo(), null, null, 'id');
$fields->submit = [
'type' => 'submit',
'value' => 'SUBMIT'
];

$form = new Form($fields->getFields());
echo $form;
```

This will render like:

```html
<form action="/fields2.php" method="post">
<dl id="pop-form-field-group-1" class="pop-form-field-group">
<dt>
<label for="username" class="required">Username:</label>
</dt>
<dd>
<input type="text" name="username" id="username" value="" required="required" />
</dd>
<dt>
<label for="password" class="required">Password:</label>
</dt>
<dd>
<input type="password" name="password" id="password" value="" required="required" />
</dd>
<dd>
<input type="submit" name="submit" id="submit" value="SUBMIT" />
</dd>
</dl>
</form>
```

You can set element-specific attributes and values, as well as set fields to omit, like
the 'id' parameter in the above examples. Any `TEXT` column type in the database is
created as textarea objects and then the rest are created as input text objects.

[Top](#basic-usage)

0 comments on commit 76a4d98

Please sign in to comment.