dev-master
9999999-dev
The Requires
The Development Requires
1.0
1.0.0.0
The Requires
The Development Requires
To create elements, you access the appropriate method for that element type. Alternatively you can use the underlying add()
or addElement(ElementInteface $element)
methods., (*1)
``` php text('username'); $form->password('password'); $form->password('confirm_password'); $form->submit('submit_button', 'Click to Continue'); ``` ### Getting Elements Getting the element when you make it ``` php text('username'); ``` Get the element at a later date ``` php $form->text('username'); $usernameElement = $form->get('username'); ``` ### Creating an element and setting attributes on it ``` php text('username') ->attr('class', 'username-field') ->attr('id', 'username-field'); ``` ### Creating an element and setting its value ``` php text('username')->setValue($userEntity->getUsername()); ``` ### Rendering Elements Each element object has a ``__toString()`` method aliased to ``render()`` so you can just echo the objects to render them Controller Code ``` php render('....', compact('form')); ?>, (*2)
Template Code ``` php <div class="username-container"> <?= $form->getElement('username'); ?> </div>
When toArray()
is called on your entity, you will have a data key for username
which will match the name of the text field added named username
.
``` php
<?php
$form = new Form();
$form->text('username');, (*3)
$entity = new UserEntity($userHelper->getByID($userID)); $form->bind($entity->toArray());, (*4)
### Creating custom elements As long as your element imeple,ented ``ElementInteface`` then you can add it to the form. ``` php <?php $element = new CustomElement(); $element->setValue($someValue); $element->attr('id', 'custom-element'); $form->addElement($element);
An example of your custom element ``` php <?php, (*5)
use PPI\Form\Element\ElementInterface;, (*6)
class CustomElement implements ElementInterface { protected $type = 'CustomElement';, (*7)
// .. implement the methods in ElementInterface
} ```, (*8)