Powerful and Secure Forms with Live-Validation and 100% Custom Markup

RockForms is based on the great Nette Forms Component and adds a little ProcessWire magic here and there to make working with forms a breeze.

It will render forms directly into the markup of your website (no iframes!), which makes it a perfect companion for tools like HTMX or Alpine.js ❤️

Rendering forms is as simple as this:

echo $rockforms->render('MyForm');

Working Example

In this example we will build the following form.

  • Submit an empty form

    You will see that the form will not submit and show an error message! The interesting part here is that this validation happens on the client side without ever hitting the server. If anybody ever tried to manipulate your form via Devtools, no worries, the validation will also happen on the server side as well!

  • Submit the form with given name "baz"

    You will see that the form submits, but it will show an error. This is an example to demonstrate that server-side validation is also possible (for example validating sensitive information without exposing business secrets).

    Another great thing is that even when the form submission was not successful all inputfield values will be restored! Have you ever submitted a long form and then got an error message saying "please try it again" and all input was lost? Uncool! This will not happen when using RockForms.

  • Submit the form with "<script>alert('hello world')</script>" as first name

    You will see that RockForms will automatically entity encode the value in the returned success message. See more about security in the docs!

Now let's get to the code!

First, we define the fields of our form:

  ->addText("forename", "Enter your first name")
  ->setRequired("We need your first name to show it!");
  ->addText("surname", "Enter your given name");
  ->addSubmit("submit", "Submit your name");

Now our form knows which fields to show, great. The next thing is to define what it should do when the form is processed - for example it could send an E-Mail, it could create a User, it could create an Ivoice, whatever. Imagination and your PHP skills are the limit 😉

$values = $form->values();
if ($values->surname == 'baz') {
  $form['surname']->addError('Sorry, baz is not allowed as given name.');
// otherwise send an email
// or create an invoice etc...

Ok, great, now the form knows what to do after submit. So what if everything went as expected - we want to show the user a success message, right?

$name = $values->forename;
return "<div class='uk-alert uk-alert-success'>Thank you for submitting the form, $name!</div>";

That's all there is to know for building great forms when using RockForms!

If you are wondering where to put this code, head over to the next chapter, but don't worry - RockForms offers a GUI to create your form boilerplate 😉😎😅


You can find all examples of this docs in the /docs/.examples folder of the module.