accessing request parameters inside paginator partial - zend-framework

1)how do i access the search $keyword inside the paginator partial to create search freindly urls ? clearly, passing the keyword as $this->view->paginator->keyword doesnt work.
2) currently, the search button's name is also send as param . for eg when searching for 'a' the url becomes http://localhost/search/index/search/a/submit//page/2. any way to stop this ?
Search Form:
<form id="search" method="get" action="/search">
<input id="searchfield" onClick="this.value=''" type="text" name="search" value="Search wallpapers"/>
<input id="searchbutton" type="submit" value="" name="submit"/>
</form>
Action inside searchController:
public function indexAction()
{
$keyword=$this->_request->getParam('search');
$alnumFilter=new Zend_Filter_Alnum();
$dataModel=new Model_data();
$adapter=$dataModel->fetchPaginatorAdapter("title LIKE '%".$keyword."%'", '');
$paginator=new Zend_Paginator($adapter);
$paginator->setItemCountPerPage(18);
$page=$this->_request->getParam('page', 1);
$paginator->setCurrentPageNumber($page);
$this->view->paginator=$paginator;
$this->view->keyword=$keyword;
}
index.phtml (view) file:
partialLoop('wallpaper/table-row.phtml',$this->paginator) ;?>
<div id="page-links">
<?= $this->paginationControl($this->paginator,'Sliding','partials/search-pagination-control.phtml');?>
</div>
search-paginator-control.phtml file:
if ($this->pageCount){
$params=Zend_Controller_Front::getInstance()->getRequest()->getParams();
if(isset($this->previous)){
?>
Previous
<?php
}
else{
?> Previous <?php
}
foreach($this->pagesInRange as $page){
if($page !=$this->current){
?>
<?=$page;?>
<?
}
else{
echo $page;
}
}
if(isset($this->next)){
?>
Next
<?php
}
else{
?> Next <?php
}
}

The paginationControl view helper accepts a 4th parameter, that is a array of parameters, so in your case you could do something like this:
<div id="page-links">
<?= $this->paginationControl($this->paginator,'Sliding','partials/search-pagination-control.phtml', array('keyword' => $this->keyword));?>
</div>
Then you access it inside your pagination using $this->keyword.

Related

Problems with yii2 form action. How to redirect to specify Url when i submit form in yii2 with method get

I am facing problems with yii2 form get method. Here is my form:
<form class="search-form" method="get" action="<?php echo Yii::$app->urlManager->createAbsoluteUrl(['search/index']); ?>" id="search-form">
<div class="row search-box">
<div class="12u search-box-inner">
<input class="search-input" type="text" id="search-query" placeholder="Search" name="search_key" autocomplete="off" >
</div>
</div>
</form>
Here is my SearchController with actionIndex():
public function actionIndex()
{
$request = Yii::$app->request;
$search_key = $request->get('search_key');
return $this->render('index', ['search_key'=>$search_key]);
}
I want to submit to web/index?r=search/index&&search_key='something', but when I submit this form always returns web/index?searchkey='something'.
What need I do?
If you want use a parameter in your SearchController/Index
return $this->render('index', ['search_key'=>$search_key]);
You should declare in function declaration
public function actionIndex($search_key)
in this way you can use the value of $search_key passsed in render call
by your form submit action
<form class="search-form" method="get" action="
<?php echo Yii::$app->urlManager->createAbsoluteUrl(['search/index']); ?>"
id="search-form">
the resulting target is should be
web/index.php?r=search&id=search-form
and for use this get submit in your SearchController/Index
Your actionIndex function should be
public function actionIndex($id)
{
// $id contain the value you assigne in form action
// in you case you should obtain the value 'search-form'
.......
}
First of all, you need to configuring-web-servers correctly.
The url should not include 'web/index'.
Then change your form with ActiveForm
<?php
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>
Your form content here.
<?php ActiveForm::end(); ?>
OR
Just change url.
<?php echo Url::to(['search/index']); ?>

zend2 forms and action attribute

in my EditController I have indexAction :
public function indexAction()
{
$delete = new DeleteForm();
$view = new ViewModel(array('delete' => $delete ));
return $view;
}
and the view
<?php
$formDelete = $this->delete;
$formDelete->prepare();
$formDelete->setAttribute('action', $this->url(NULL, array('controller'=>'Register', 'action' => 'process')));
?>
<?php echo $this->form()->openTag($formDelete);?>
<div class="form-group">
<?=$this->formLabel($formDelete->get('reason')); ?>
<?=$this->formElement($formDelete->get('reason'))?>
<?=$this->formElementErrors($formDelete->get('reason'))?>
</div>
<div class="form-group">
<?=$this->formElement($formDelete->get('delete-button'))?>
</div>
<?php echo $this->form()->closeTag();?>
Despite I have action attribute set to RegisterController and processAction in page source I see always the same action. In setAttribute $this->url I can type whatever I want, in the code source always is
<form method="post" name="deleteForm" enctype="multipart/formdata" action="/App/public/element/edit" id="deleteForm">
The first parameter of the URL helper should be the name of the route you want to use, not NULL. By using NULL, ZF will fall back on using the current route, which I'd guess is not correct in your case.

Handle two different forms in a single page

So I want to handle two forms on one single page, and I've got some problems to do it; I've got a module named 'contact', here is the indexSucces.php
<div id="registerForm">
<form action="<?php echo url_for('contact'); ?>" method="post" id="loginForm">
<?php foreach ($loginForm as $widget): ?>
<?php if(!$widget->isHidden()) { ?>
<?php echo $widget->renderRow(); ?>
<?php } else { ?>
<?php echo $widget->render() ?>
<?php } ?>
<?php endforeach; ?>
<br/><input type="submit" id="loginSubmit" class="btn primary" value="<?php echo __('Envoyer'); ?>" />
</form>
</div>
<div id="registerForm">
<form action="<?php echo url_for('contact'); ?>" method="post" id="registerForm">
<?php foreach ($registerForm as $widget): ?>
<?php if(!$widget->isHidden()) { ?>
<?php echo $widget->renderRow(); ?>
<?php } else { ?>
<?php echo $widget->render() ?>
<?php } ?>
<?php endforeach; ?>
<br/><input type="submit" id="registerSubmit" class="btn primary" value="<?php echo __('Envoyer'); ?>" />
</form>
</div>
Is it possible to handle that in a single action page ? I need to know the way on how to it, I'm stuck since yesterday. For exemple, when I want to submit the first form, it wants to submit the second form with.
Someone can provide me an exemple ? I can post my sh** action page if you want to see how I tried to handle that.
Thanks in advance !
EDIT:
I made this code according to your advice :
public function executeIndex(sfWebRequest $request)
{
$this->loginForm = new loginForm();
$this->registerForm = new registerForm();
$this->processForm($request, $this->loginForm, $this->registerForm);
}
public function processForm(sfWebRequest $request, sfForm $loginForm, sfForm $registerForm)
{
if($request->hasParameter('login'))
{
if($request->isMethod('post'))
{
$loginForm->bind($request->getParameter($loginForm->getName()));
if($loginForm->isValid())
{
$this->redirect('payement');
}
}
} elseif ($request->hasParameter('register'))
{
if($request->isMethod('post'))
{
$registerForm->bind($request->getParameter($registerForm->getName()));
if($registerForm->isValid())
{
$this->redirect('payement');
}
}
}
}
The registerForm is submitting correctly, I can see the errors I've made in the form etc, and if it's correct, it is correctly redirected. But when I submit the loginForm, here is the error :
Argument 1 passed to sfForm::bind() must be an array, string given : on this line
$loginForm->bind($request->getParameter($loginForm->getName()));
Why ?
Yes, you can.
Add a name attribute to your buttons (i.e. login and register), and modify your action to:
if ($request->hasParameter("login")) {
// handle login
}
elseif ($request->hasParameter("register")) {
// handle register
}

Zend Framework Custom Forms with viewScript

I am having some problems working out how to use custom forms in Zend Framework.
I have followed various guides but none seem to work. Nothing at all gets rendered.
Here is the bits of code that I am trying to use (All code below is in the default module). I have simplified the code to a single input for the test.
applications/forms/One/Nametest.php
class Application_Form_One_Nametest extends Zend_Form {
public function init() {
$this->setMethod('post');
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Box Name')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Submit Message');
$submit->setAttrib('id', 'submitbutton');
$submit->setAttrib('class', 'bluebutton');
$this->addElements(array($name, $submit));
}
}
application/views/scripts/one/formlayout.phtml
<form action="<?= $this->escape($this->form->getAction()) ?>" method="<?= $this->escape($this->form->getMethod()) ?>">
<p>
Please provide us the following information so we can know more about
you.
</p>
<? echo $this->element->name ?>
<? echo $this->element->submit ?>
</form>
application/controllers/IndexController.php
public function formtestAction() {
$form = new Application_Form_One_Nametest();
$form->setDecorators(array(array('ViewScript', array('viewScript' => 'one/formlayout.phtml'))));
$this->view->form = $form;
}
application/views/scripts/index/formtest.phtml
<h1>Formtest</h1>
<?
echo $this->form;
?>
The above code does not throw any errors or render any part of formlayout.phtml including the form tags or text between the p tags.
Can anybody tell me what I might be doing wrong?
I think the problem is your form element's decorator. You should set the decorator to ViewHelper and Error only. It works for me at least.
Here is the code I used and it should work
applications/forms/Form.php
class Application_Form_Form extends Zend_Form {
public function loadDefaultDecorators() {
$this->setDecorators(
array(
array(
'ViewScript',
array(
'viewScript' => 'index/formlayout.phtml',
)
)
)
);
}
public function init() {
$this->setAction('/action');
$this->setMethod('post');
$this->addElement('text', 'name', array(
'decorators' => array('ViewHelper', 'Errors')
));
}
}
application/views/scripts/index/formlayout.phtml
<form action="<?php echo $this->element->getAction(); ?>" method="<?php echo $this->element->getMethod(); ?>">
<div>
<label for="name">Box Name</label>
<?php echo $this->element->name; ?>
</div>
<input type="submit" value="Submit Message" id="submitbutton" class="bluebutton">
</form>
application/views/scripts/index/index.phtml
<!-- application/views/scripts/index/index.phtml -->
<?php echo $this -> form; ?>
application/controllers/IndexController.php
public function indexAction() {
$form = new Application_Form_Form();
$this -> view -> form = $form;
}
Here is a very simple example to get you going adapted from this article.
The form:-
class Application_Form_Test extends Zend_Form
{
public function init()
{
$this->setMethod('POST');
$this->setAction('/');
$text = new Zend_Form_Element_Text('testText');
$submit = new Zend_Form_Element_Submit('submit');
$this->setDecorators(
array(
array('ViewScript', array('viewScript' => '_form_test.phtml'))
)
);
$this->addElements(array($text, $submit));
$this->setElementDecorators(array('ViewHelper'));
}
}
The order in which setDecorators(), addElements() and setElementDecorators() are called is very important here.
The view script _form_test.phtml can be called anything you like, but it needs to be in /views/scripts so that it can be found by the renderer.
/views/scripts/_form_test.phtml would look something like this:-
<form id="contact" action="<?php echo $this->element->getAction(); ?>"
method="<?php echo $this->element->getMethod(); ?>">
<p>
Text<br />
<?php echo $this->element->testText; ?>
</p>
<p>
<?php echo $this->element->submit ?>
</p>
</form>
You instantiate the form, pass it to the view and render it as usual. The output from this example looks like this:-
<form id='contact' action='/' method='post'>
<p>
Text<br />
<input type="text" name="testText" id="testText" value=""></p>
<p>
<input type="submit" name="submit" id="submit" value="submit"></p>
</form>
That should be enough to get you started creating your own forms.
Usually, if you don't see anything on the screen it means that some sort of error happened.
Maybe you have errors turned off or something, maybe not. I'm just trying to give you ideas.
The only things I could spot where the following.
In the below code, you have to still specify the form when trying to print out the elements.
<form>
action="<?php $this->escape($this->element->getAction()) ?>"
method="<?php $this->escape($this->element->getMethod()) ?>" >
<p>
Please provide us the following information so we can know more about
you.
</p>
<?php echo $this->element->getElement( 'name' ); ?>
<?php echo $this->element->getElement( 'submit' ) ?>
</form>
As vascowhite's code shows, once you are inside the viewscript, the variable with the form is called element. The viewscript decorator uses a partial to do the rendering and thus it creates its own scope within the viewscript with different variable names.
So, although in your original view it was called $form, in the viewscript you'll have to call it element.
Also, maybe it was copy/paste haste, but you used <? ?> tags instead of <?= ?> or <?php ?> tags. Maybe that caused some error that is beyond parsing and that's why you got no output.

Zend_Form: when print elements of form in view- form tag dod't created

Problem:
When print elements of form in view, form tag don't created
My View:
<?php
/****** print elements and inser label:: have to be done in this way for integrate cushycms ********/
echo $this->form->empty;
?>
<label>Ad Title</label>
<?php
echo $this->form->adtitle;
?>
<label></label>
<?php echo $this->form->adbody; ?>
MY Form (part of the code):
class MyForm extends Zend_Form
{
function init(){
$empty = new Zend_Form_Element_Hidden("empty");
$empty->removeDecorator('Label');
$title = new Zend_Form_Element('adtitle');
$title->removeDecorator('Label');
$title//->setLabel('Ad Title')
->setRequired('true')
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty')
->setAttrib('MAXLENGTH',100)
->setAttrib('Size',106);
$title->getValidator('NotEmpty')
->setMessage('Company Name can not be empty');
$body = new Zend_Form_Element_Textarea('adbody');
$body->removeDecorator('Label');
}
}
The html that I get (form tag not exist):
<dd id="empty-element">
<input type="hidden" name="empty" value="" id="empty"></dd> <label>Ad Title</label>
<dd id="adtitle-element">
<input type="text" name="adtitle" id="adtitle" value="" MAXLENGTH="100" Size="106"></dd><label></label>
<dd id="adbody-element">
<textarea name="adbody" id="adbody" onKeyDown="javascript:limitText(this.form.countdown,400)" onKeyUp="javascript:limitText(this.form.countdown,400)" rows="24" cols="80"></textarea></dd> <label>chras left (maximum 400): </label>
Thank you very much
I think you have to add the form tag by your self.
<form action="<?= $this->escape($this->form->getAction() ?>"
method="<?= $this->escape($this->form->getMethod() ?>"
>
Or use
echo $this->form;