Need a better example for contributte/forms-bootstrap

Cupara
Member | 90
+
0
-

I'm trying to implement contributte/forms-bootstrap so I can style the forms better than what they are. The problem I'm running into is that the guides @Infanticide0 directed me to are incomplete as I learn new frameworks by seeing example usage especially when it comes to using composer. If anyone could please provide me with a better detailed step-by-step guide on implementing contributte/forms-bootstrap I would greatly appreciate it.

Infanticide0
Member | 103
+
0
-

@Cupara

You use BootstrapForm just like Nette Forms because

class BootstrapForm extends Form

example:

public function createComponentSignUpForm(): BootstrapForm
		{
			$form = $this->formFactory->create(); // returns new BootstrapForm instance with some predefined configuration (V5, ajax, renderMode)

			$form->addText("name", "name")
				->setRequired("enter your name");

			$form->addEmail("email", "E-mail")
				->setRequired("enter email");

			$passwordInput = $form->addPassword("password", "Password")
					->setRequired("enter password");

			$form->addPassword("password2", "password")
				->setRequired("enter password")
				->addRule(Nette\Forms\Form::EQUAL, "Passwords must be the same", $passwordInput);

			$form->addSubmit("signup", "Sign up")
				->setHtmlAttribute("class", "btn btn-primary");

			$form->onSuccess[] = [$this, "onSignUp"];

			return $form;
		}

Last edited by Infanticide0 (2023-12-28 12:21)

Cupara
Member | 90
+
0
-

Gotcha, thanks. I'll give it a try.

Cupara
Member | 90
+
0
-

@Infanticide0 I have this line of code in my newly created Forms.php model file $newsId = $this->getParameter('newsId'); and it states that getParameter is an unknown method, what should I be using instead to grab the newsId that gets passed with the form?

Infanticide0
Member | 103
+
0
-

getParameter is Presenter method, you should pass that value to your model.

Cupara
Member | 90
+
0
-

I thought so.

Cupara
Member | 90
+
0
-

@Infanticide0 So I have a problem, I can't figure out why $this->template->addFilter section of code doesn't seem to work.

    public function renderShow(): void
    {
        $this->template->contact = $this->facade
            ->createComponentContactForm();
        $this->template->addFilter('formPair', function ($control) {
            $renderer = $control->form->renderer;
            $renderer->attachForm($control->form);

            return $renderer->renderPair($control);
        });
    }

And in my template is this:

{block content}
    <div class="p-1">
        <form>
            {$form['name']|formPair|noescape}
            {$form['email']|formPair|noescape}
            {$form['message']|formPair|noescape}
            {$form['submit']|formPair|noescape}
        </form>
    </div>
{/block}

This is the error LogicException: Filter 'formPair' is not defined.

Infanticide0
Member | 103
+
0
-

How do you use this?

$this->template->contact = $this->facade->createComponentContactForm();

Create component in presenter (it's lazy loaded when needed), your facade should be rather ContactFormControlFactory creating new instance of ContactFormControl.
https://doc.nette.org/…n/components#…

You can render forms in many ways

{control controlname}
or
{form formcontrolname}...{/form}
or
<form n:name="controlname">
	<label n:name="inputname"/>
	<input n:name="inputname">
	or
	{input inputname}
</form>

https://doc.nette.org/…ms/rendering

Last edited by Infanticide0 (2023-12-29 04:47)

Cupara
Member | 90
+
0
-

Got the form all figured out, thanks.