-
Notifications
You must be signed in to change notification settings - Fork 0
Forms
Forms are used to group the data form user input together. They are also needed for special operations such as file upload.
In EOSS the forms are very similar to the ones in Nette Framework. Let's make some registration form.
We'll start with indexEOSS class.
app/controller/indexEOSS.php:
<?php
use EOSS\EOSS;
use Forms\Form;
use Debug\Linda;
class indexEOSS extends EOSS {
public function load() {
$this->csi->params->ourForm = $this->createForm();
$this->csi->setFile("indexView.php");
}
public function bind() {}
private function createForm() {
$form = new Form("myForm", $this);
// Or we don't have to pass $this
// and we can do $eoss->registerForm($form)
// later.
$form->addText("username", "Username: ")->setRequired();
$form->addEmail("email", "Email: ");
$form->addPassword("password", "Password: ")->setRequired();
$form->addHidden("hidden", "secret") // hidden is the name and secret is the value
$form->addFile("avatar", FALSE, "Avatar: ");
$form->addSubmit("submit", "Submit our Form")->addAttribute("class", "btn btn-submit");
$form->onsubmit[] = "ourFormSubmitted";
}
public function ourFormSubmitted(SubmittedForm $form) {
Linda::dump("Hello " . $form->username);
// Store data into database for example...
}
}
Now let's render our form in indexView.php:
app/view/indexView.php:
<div class="form-wrapper">
<?= $ourForm; ?>
</div>or we can render form as table:
app/view/indexView.php:
<div class="form-wrapper">
<?= $ourForm->asTable(); ?>
</div>You can use $ourForm->asRaw(); to print the raw form. If you need some extra styling you can use this:
<div class="form-wrapper">
<?= $ourForm->asIWant(function($element, $label) {
return "<div class=\"" . ($element instanceof \Forms\Controls\Submit ? "" : "input-field ") . "col s3\">{$element}{$label}</div>";
}); ?>
</div>And those are the forms in EOSS2. We can also fill the default values with $form->setDefaults() which takes an array of key => value pair with field names => default values for the form.
Welcome to EOSS, we are glad to see you here.
We are looking for contributors, come and join us, write me an email on durisvk2@gmail.com or post something on a http://eoss.wz.sk .