remove persistent parameters from form render

Notice: This thread is very old.
mkoula
Backer | 57
+
0
-

I have some presenter where I have some persistent attribute and form:

class SearchPresenter extends BasePresenter {
    /** @persistent */
    public $search;
...
    protected function createComponentSearchForm()
    {
        $form = new Nette\Forms\Form('searchForm');

My problem is that the persistent attribute is generated as hidden field in the form. Is there any easy way how to disable it in the form?

japlavaren
Member | 404
+
+1
-

hi,

reason, why you have hidden input inside search form is simple – you have form with get method. parameters in query are not sending with form (all after ? in url is ignoring by browser).
only posibility how to send extra get parameters is create hidden input with value of this parameter. nette is doing this for you automaticaly

to your question, you can set action for your form and say, what parameters you do not want to use

<?php
$form->setAction($this->link('this', array(
	'search' => NULL, // remove persistent parameter
));
?>

hope I helped to you and my english was understable for you. ;)

Last edited by japlavaren (2014-08-16 22:26)

Majkl578
Moderator | 1364
+
+1
-

Proper solution is to use form via post, then redirect in the handler and set persistent parameter value.

japlavaren
Member | 404
+
0
-

i think post form is not good for search form. you can not bookmark results. you should redirect to proper get url after post request but why create new redirection

Last edited by japlavaren (2014-08-16 22:42)

Majkl578
Moderator | 1364
+
0
-

i think post form is not good for search form

Bullshit.

Proper solution is this:

protected function createComponentSearchForm()
{
	$form = new \Nette\Application\Form();

	$form->addText('search')
		->setRequired();
	$form->addSubmit('submitter', 'Search');

	$form->onSuccess[] = function (Form $form) {
		$this->redirect('this', ['search' => $form->getValues()->search]);
	};

	return $form;
}

you can not bookmark results

What? Why wouldn't you?

but why create new redirection

To have a clean URL. Otherwise you would have also a button there and possibly also other mess.
Also you are abusing forms behavior to update persistent parameters.

mkoula
Backer | 57
+
0
-

Actually it's not a problem of the form action, but Nette put hidden elements into the form, which represents these persistent attributes. I have this parameter as GET in the URL, because it' not mandatory.

The form is generated and at the end there's div with hidden fields, where is the persistent attribute:

<form action="/my-action-url/" method="get" id="frm-searchForm">
....
<div>
    <input type="hidden" name="search" value="xxx">
</div>
</form>

I would need to somehow get this div with hidden fields off the form…

Last edited by mkoula (2014-08-16 23:00)

japlavaren
Member | 404
+
0
-
<?php
$form->setAction($this->link('this', array(
    'search' => NULL, // remove persistent parameter
));
?>
japlavaren
Member | 404
+
0
-
  • to clean url make autoCanonize on presenter automatically (remove all empty values)
  • if you have or if you have not submit button name in url query does not change anything

Last edited by japlavaren (2014-08-16 23:03)

Tomáš Votruba
Moderator | 1114
+
0
-

@japlavaren I agree with @Majkl578. Moreover, you add one hack to make the other hack work.

There is also Post/Redirect/Get pattern you should use dealing with forms

@mkoula Use example by @Majkl578, it's perfect.

japlavaren
Member | 404
+
0
-

i still do not agree. post method is for submiting data to process but no for search. everywhere is uset get for searching

voda
Member | 561
+
0
-

Try replacing the persistent attribute with an action parameter:

class SearchPresenter extends BasePresenter {

    public function renderDefault($search = '') {}

    protected function createComponentSearchForm()
    {
        $form = new Nette\Forms\Form('searchForm');
voda
Member | 561
+
0
-

I aggree with @japlavaren, using post-redirect-get for a form, that doesn't modify any data is a little pointless. Re-submitting a search form (hitting F5) doesn't do any harm.

voda
Member | 561
+
0
-

@Majkl578:

Otherwise you would have also a button there

If you use a button without a name attribute, than there is no pollution in the url.

hejdav
Member | 50
+
0
-

The problem will occur, when you need rid of persistent parameter (or just common parameter expected by action method e.g. actionDefault($param)) in action presenter life-part. Handlers are processed until after that life-part so you can't find out if that form was or wasn't submitted and you have unwanted parameter present in this actionDefault() or similar method.

In this case, the only solution is probably changing the form action. Moreover form action can be changed only after form is attached to presenter. My solution is to make a form, that monitors Presenter and after it is attached, it calls onAttached event and then set form action:

<?php
class MyForm extends UI\Form {
	public $onAttached = [];

	public function __construct() {
		parent::__construct();
		$this->monitor(Presenter::class);
	}

	protected function attached($obj) {
		parent::attached($obj);
		if ($obj instance of Presenter) {
			$this->onAttached($this);
		}
	}
}

class MyPresenter extends UI\Presenter {
	/** @persistent */
	public $persistentParam;

	protected function createComponentMyForm() {
		$form = new MyForm;
		// ...
		$form->onAttached[] = function (Form $form) {
			$form->setAction($this->link('this', ['persistentParam' => null]));
		};
	}
}
?>

Last edited by hejdav (2015-06-16 08:59)