Skip to content
Syone edited this page May 24, 2024 · 34 revisions

Form

How to create a custom form component

  1. Create a custom class that extends \Sy\Bootstrap\Component\Form, it must implement 2 methods: init() and submitAction()

Example:

<?php
namespace Project\Component;

class MyContactForm extends \Sy\Bootstrap\Component\Form {

	/**
	 * Send to email address
	 *
	 * @var string
	 */
	private $email;

	/**
	 * @param string $email
	 */
	public function __construct($email) {
		parent::__construct();
		$this->email = $email;
	}

	/**
	 * Form inputs must be added in this method
	 */
	public function init() {
		// TO DO
	}

	/**
	 * Form submit action must be implemented in this method
	 *
	 * @return string
	 */
	public function submitAction() {
		// TO DO
	}

}
  1. Add form inputs in the init() method
	/**
	 * Form inputs must be added in this method
	 */
	public function init() {
		// Add email input
		$this->addEmail([
			'name'        => 'email',
			'required'    => 'required',
			'placeholder' => 'Your e-mail',
		], [
			'label' => 'E-mail',
		]);

		// Add anti spam field
		$this->addAntiSpamField();

		// Add message textarea
		$this->textarea = $this->addTextarea([
			'name'        => 'message',
			'required'    => 'required',
			'placeholder' => 'Your message',
		], [
			'label' => 'Message',
		]);

		// Add submit button
		$this->addButton('Send');
	}

Clone this wiki locally