ContactForm.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace Tools\Form;
  3. use Cake\Form\Form;
  4. use Cake\Form\Schema;
  5. use Cake\Validation\Validator;
  6. /**
  7. * A default ContactForm form fitting most apps.
  8. * Extend in your app to fill _execute() and to customize
  9. *
  10. * @author Mark Scherer
  11. * @license MIT
  12. */
  13. class ContactForm extends Form {
  14. protected function _buildSchema(Schema $schema) {
  15. return $schema->addField('name', ['type' => 'string', 'length' => 40])
  16. ->addField('email', ['type' => 'string', 'length' => 50])
  17. ->addField('subject', ['type' => 'string', 'length' => 60])
  18. ->addField('body', ['type' => 'text']);
  19. }
  20. protected function _buildValidator(Validator $validator) {
  21. return $validator
  22. ->notEmpty('name', __('This field cannot be left empty'))
  23. ->add('email', 'format', [
  24. 'rule' => 'email',
  25. 'message' => __('A valid email address is required'),
  26. ])
  27. ->notEmpty('subject', __('This field cannot be left empty'))
  28. ->notEmpty('message', __('This field cannot be left empty'));
  29. }
  30. protected function _execute(array $data) {
  31. // Overwrite in your extending class
  32. return true;
  33. }
  34. }