Modelless Forms

    • class Form

    Generally when using the Form class you’ll want to use a subclass to define yourform. This makes testing easier, and lets you re-use your form. Forms are putinto src/Form and usually have Form as a class suffix. For example,a simple contact form would look like:

    In the above example we see the 3 hook methods that forms provide:

    • _buildSchema is used to define the schema data that is used by FormHelperto create an HTML form. You can define field type, length, and precision.
    • Gets a instancethat you can attach validators to.
    • _execute lets you define the behavior you want to happen whenexecute() is called and the data is valid.

    You can always define additional public methods as you need as well.

    Processing Request Data

    Once you’ve defined your form, you can use it in your controller to processand validate request data:

    1. // In a controller
    2. namespace App\Controller;
    3.  
    4. use App\Controller\AppController;
    5. use App\Form\ContactForm;
    6.  
    7. {
    8. public function index()
    9. {
    10. $contact = new ContactForm();
    11. if ($this->request->is('post')) {
    12. if ($contact->execute($this->request->getData())) {
    13. $this->Flash->success('We will get back to you soon.');
    14. } else {
    15. $this->Flash->error('There was a problem submitting your form.');
    16. }
    17. }
    18. $this->set('contact', $contact);
    19. }
    20. }
    1. $isValid = $form->validate($this->request->getData());

    You can set default values for modelless forms using the setData() method.Values set with this method will overwrite existing data in the form object:

    Values should only be defined if the request method is GET, otherwiseyou will overwrite your previous POST Data which might have validation errorsthat need corrections.

    Getting Form Errors

    Once a form has been validated you can retrieve the errors from it:

    1. $errors = $form->getErrors();
    2. /* $errors contains
    3. 'email' => ['A valid email address is required']
    4. ]
    5. */

    It is possible to invalidate individual fields from the controller without theuse of the Validator class. The most common use case for this is when thevalidation is done on a remote server. In such case, you must manuallyinvalidate the fields accordingly to the feedback from the remote server:

    1. // in src/Form/ContactForm.php
    2. public function setErrors($errors)
    3. {
    4. $this->_errors = $errors;
    5. }

    Now you will be able to invalidate form fields by setting the fieldName, thenset the error messages:

    1. // In a controller
    2. $contact = new ContactForm();
    3. $contact->setErrors(["email" => ["_required" => "Your email is required"]]);

    Proceed to Creating HTML with FormHelper to see the results.

    Creating HTML with FormHelper

    Once you’ve created a Form class, you’ll likely want to create an HTML form forit. FormHelper understands Form objects just like ORM entities:

    1. echo $this->Form->create($contact);
    2. echo $this->Form->control('name');
    3. echo $this->Form->control('email');
    4. echo $this->Form->control('body');
    5. echo $this->Form->button('Submit');
    6. echo $this->Form->end();

    The above would create an HTML form for the ContactForm we defined earlier.HTML forms created with FormHelper will use the defined schema and validator todetermine field types, maxlengths, and validation errors.