1. Go to this page and download the library: Download efabrica/neoforms library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
efabrica / neoforms example snippets
use Efabrica\NeoForms\Build\NeoForm;
use Efabrica\NeoForms\Build\NeoFormFactory;
use Efabrica\NeoForms\Build\NeoFormControl;
use Efabrica\NeoForms\Build\ActiveRowForm;
use Nette\Database\Table\ActiveRow;
use Nette\Application\UI\Template;
class CategoryForm extends ActiveRowForm
{
private NeoFormFactory $formFactory;
private CategoryRepository $repository;
// There is no parent constructor
public function __construct(NeoFormFactory $formFactory, CategoryRepository $repository) {
$this->formFactory = $formFactory;
$this->repository = $repository;
}
/**
* NeoFormControl is attached to presenter and used in template.
*
* @param ActiveRow|null $row Optional ActiveRow parameter to fill the form with data
* @return NeoFormControl
*/
public function create(?ActiveRow $row = null): NeoFormControl
{
$form = $this->formFactory->create();
$form->addText('name', 'Category Name')
->setHtmlAttribute('placeholder', 'Enter category name')
->setRequired('Name is
class CategoryPresenter extends AdminPresenter
{
private CategoryForm $form;
private CategoryRepository $repository;
public function actionCreate(): void
{
$this->addComponent($this->form->create(), 'categoryForm');
}
public function actionUpdate(int $id): void
{
$row = $this->repository->findOneById($id);
if (!$row instanceof \Nette\Database\Table\ActiveRow) {
throw BadRequestException();
}
$this->addComponent($this->form->create($row), 'categoryForm');
}
}
$form->addPassword('password', 'Password')->setOption('description', 'At least 8 characters.');
$form->addText('title', 'Title')->setOption('info', 'This appears on homepage');
$form->addText('title', 'Title')->setOption('readonly', true);
// or
{formRow $form['title'], readonly => true}
// or
$form->setReadonly(true); // to make the entire form readonly
// or
{neoForm yourForm, readonly => true} // to make the entire form readonly
use Efabrica\NeoForms\Build\NeoContainer;
// Create a new collection called "sources"
$form->addCollection('sources', 'Sources', function (NeoContainer $container) {
// Add some fields to the collection
$container->addText('bookTitle', 'Book Title');
$container->addInteger('Year', 'Year');
// Add another collection for authors
$container->addCollection('authors', 'Authors', function (NeoContainer $container) {
$container->addText('author', 'Author');
});
});
protected function onUpdate(NeoForm $form, array $values, ActiveRow $row): void
{
// To process the form, you can get the new state of the collection like this:
$sources = $values['sources'];
// If you want to use the Diff API, you can do something like this:
$diff = $form['sources']->getDiff();
foreach($diff->getAdded() as $newRow) {
$this->sourceRepository->insert($newRow);
}
foreach($diff->getDeleted() as $removedRow) {
$this->sourceRepository->delete($removedRow);
}
foreach($diff->getModified() as $updatedRow) {
$row = $this->sourceRepository->findOneBy($updatedRow->getOldRow());
$row->update($updatedRow->getDiff());
}
}
namespace Your\Namespace\Here;
use Efabrica\NeoForms\Render\Template\NeoFormTemplate;
// ... Import other necessary classes here ...
class Bootstrap4FormTemplate extends NeoFormTemplate
{
// Your template implementation goes here
}
protected function textInput(TextInput $control, array $attrs): Html
{
$el = $control->getControl();
$el->class ??= 'form-control'; // Add Bootstrap class, if no class was specified through ->setHtmlAttribute()
return $this->applyAttrs($el, $attrs);
}
public function formLabel(BaseControl $control, array $attrs): Html
{
$el = $control->getLabel();
$el->class ??= 'col-form-label'; // Add Bootstrap class
$el->class('rror')
->setAttribute('data-bs-toggle', 'tooltip')
->title($control->translate($error))
->addHtml(Html::el('i', 'warning')->class('material-icons-round'))
);
}
// Customize label rendering as needed
return $this->applyAttrs($el, $attrs);
}
protected function button(Button $control, array $attrs): Html
{
$el = $control->getControl();
$el->class ??= 'btn btn-primary'; // Add Bootstrap 4 classes
$icon = $control->getOption('icon');
if (is_string($icon) && trim($icon) !== '') {
$el->insert(0, Html::el('i')->class("fa fa-$icon")); // Add an icon if available
}
// Customize button rendering as needed
return $this->applyAttrs($el, $attrs);
}
use Your\Namespace\Here\BootstrapFormTemplate;
use Efabrica\NeoForms\Build\NeoForm;
// Instantiate your custom template
$template = new Bootstrap4FormTemplate();
// Create a NeoForm instance and set the custom template
$form = new NeoForm();
$form->setTemplate($template);
// Render your form
echo $form;
html
>{formInput $form['category'], data-select2 => true}
>
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.