Handle two different forms in a single page - forms

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
}

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']); ?>

facebook status on web with picture

Finally i got this done with the likes and statuses but I would like to ask for help if it is possible somehow to transfer also the pictures on web.
thank you
<?php
$facebook_page_id = '**********';
$access_token = '************';
$number_of_posts = 2;
$json_content = file_get_contents('http://graph.facebook.com/'.$facebook_page_id.'/feed?access_token='.$access_token);
$raw_data = json_decode($json_content);
$i=0;
foreach ($raw_data->data as $feed ) {
$feed_id = explode("_", $feed->id);
if (!empty($feed->message)) {
$i++;
if ($i<($number_of_posts+1)) {
?>
<div class=”fb_update”>
<?php if (!empty($feed->likes->count)) { ?>
<div class=”fb_likes”>
<?php echo $feed->likes->count; ?>
</div>
<?php } ?>
<?php if (!empty($feed->created_time)) { ?>
<div class=”fb_date”>
<?php echo date('F j, Y', strtotime($feed->created_time)); ?>
</div>
<?php } ?>
<div class=”fb_message”>
<?php echo $feed->message; ?>
</div>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
Yes, $feed->picture will get you the URL to an image for a particular item, if it exists.
You should really look at using cURL via the PHP SDK for this instead of file_get_contents(). It is more robust.

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.

accessing request parameters inside paginator partial

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.

Rendering only the <form> tag

Is there any way that i can render ONLY the start <form> tag of a Zend_Form object?
print $this->registerForm->renderForm();
renders <form></form>, and i only need <form>
Edit:
After Asleys possible solution i wrote this for My_Form class
public function renderFormOpen() {
return str_replace('</form>', '', $this->renderForm());
}
public function renderFormClose() {
return '</form>';
}
Still looking for at ZF way of doing thins, even though i don't think there is any - after going through the code in the ZF library.
You could write an custom form-decorator that uses a custom view-helper that only renders the open form tag. But I think this would be overkill.
Just "hardcode" the form-tags and fill the attributes with the data provided by the form-variable in your view.
<!--in your view-template -->
<form action="<?php echo $this->form->getAction() ?>"
enctype="<?php echo $this->form->getEnctype() ?>"
method="<?php echo $this->form->getMethod() ?>"
id="<?php echo $this->form->getId() ?>"
class="<?php echo $this->form->getAttrib('class') ?>" >
<!--in case your products are represented as elements -->
<?php foreach ($this->form->getElements() as $element): ?>
<?php echo $element ?>
<?php endforeach; ?>
<!--in case your products are represented as displayGroups -->
<?php foreach ($this->form->getDisplayGroups() as $displayGroup): ?>
<?php echo $displayGroup ?>
<?php endforeach; ?>
<!--in case your products are represented as subforms -->
<?php foreach ($this->form->getSubforms() as $subform): ?>
<?php echo $subform ?>
<?php endforeach; ?>
<!--in case your products are rendered by a view helper -->
<?php foreach ($this->products as $product): ?>
<?php echo $this->renderProduct($product) ?>
<?php endforeach; ?>
</form>
Just for fun the overkill way
// Get your products form
$form = new Form_Products();
// Add custom prefix path
$form->addPrefixPath('Foobar_Form_Decorator', 'Foobar/Form/Decorator', 'decorator');
// Set OnlyOpenTagForm-ViewHelper for FormDecorator
$form->getDecorator('Form')->setHelper('OnlyOpenTagForm');
// copy Zend/View/Helper/Form to Foobar/Form/Decorato/OnlyOpenTagForm.php
// In OnlyOpenTagForm.php
// replace Zend_View_Helper_Form with Foobar_View_Helper_OnlyOpenTagForm
// replace method "form" with onlyOpenTagForm"
// replace
if (false !== $content) {
$xhtml .= $content
. '</form>';
}
// with:
if (false !== $content) {
$xhtml .= $content;
}
Done! - The Java-Guys will love it ;)
You can render just the open form tag by passing false to the form decorator like so:
<?php echo $this->form->renderForm(false) ?>
Which will output something like:
<form id="post" enctype="multipart/form-data" method="post" action="/post">
Additonally you can pass a string to the form decorator to be enclosed by the form tags like so:
<?php echo $this->form->renderForm('Some Text') ?>
Which outputs something like:
<form id="post" enctype="multipart/form-data" method="post" action="/simchas/post">Some Text</form>
Hope this helps...
You could do something like this:
echo $this->form->getDecorator('Form')->setElement($this->form)->render(false);