Better using custom form controls

Notice: This thread is very old.
Budry
Member | 88
+
0
-

Hi,

I need very often use custom form controls (datepicker, antispam or some next for example) but if I can use in form I “must” create custom base form and extends Nette\Application\UI\Form or use a similiar code for register custom control (it is only example code, code is from a very old project and I'm not sure if is possible use in latest versions)

/**
 * @param string $name
 * @param string $class
 */
private function addExtension($name, $class)
{
	\Nette\Forms\Container::extensionMethod($name, function (\Nette\Forms\Container $container, $name, $label = null) use ($class) {
        return $container[$name] = new $class($label);
    });
}

It is bad in my opition. More better will be add method for add custom form controls.

Example:

$form = new Form();
// or some better name for method
$form->addControl('name', new MyInput('label', /*... some next arguments..*/))
    ->....

I would like heard your opinions about this…

Last edited by Budry (2014-05-02 01:23)

David Matějka
Moderator | 6445
+
0
-

you can do

$form['name'] = new MyInput
//or
$form->addComponent(new MyInput, 'name');
Budry
Member | 88
+
0
-

Well, thanks I didn't know it. I'm sorry for useless topic

Budry
Member | 88
+
0
-

But if I use ->addComponent I don't use

$form->addComponent(new MyInput(), 'name')
	->addRule(...);

If exist method which returns instance of control would it be better or do you mean not?

Last edited by Budry (2014-05-02 09:35)