First FormSignature must be provided with a secret unique to your app. This is best
done in the bootstrap process. The secret key should be a random lengthy string.
php
use lithium\security\validation\FormSignature;
FormSignature::config(array('secret' => 'a long secret key'));
In the view call the sign() method before creating the form.
php
security->sign(); ?>
form->create(...); ?>
Form fields...
form->end(); ?>
In the corresponding controller action verify the signature.
php
if ($this->request->is('post') && !FormSignature::check($this->request)) {
The key didn't match, meaning the request has been tampered with.
}
Calling this method before a form is created adds two additional options to the $options
parameter in all form inputs:
- 'locked' _boolean_: If true, _locks_ the value specified in the field when the field
is generated, such that tampering with the value will invalidate the signature. Defaults
to true for hidden fields, and false for all other form inputs.
- 'exclude' _boolean_: If true, this field and all subfields of the same name will be
excluded from the signature calculation. This is useful in situations where fields may be
added dynamically on the client side. Defaults to false.