can't pass parameter from view to controller - zend-framework

Hi all I am currently working with zend 2.3. and while listing students I am trying to make link that allows to show detales about selected person. My problem is that I can't pass selected id to the action in module's controller. Here is my view's code that coresponds to link:
<a href="<?php echo $this->url('admin' ,
array( 'controller'=>'Admin', 'action'=>'viewstudent', 'ID' => $student->ID))."/admin/viewstudent";?>">detales</a>
and the action in controller
public function viewstudentAction ()
{
$id = (int) $this->params()->fromRoute('ID', 0);
echo $id;
//echo var_dump($id);
return new ViewModel(array(
'students' => $this->getStudentTable()->viewstudent($id),
));
}
Then I var_dump $id variable it shows 0 So what is the correct way to do this?
I have edited the a href's code like this
<a href="<?php echo $this->url('admin/default' ,
array( 'controller'=>'Admin', 'action'=>'viewstudent', 'id' => $student->ID));?>">Просмотр</a>
and it resolves to this:
Просмотр
url is
http://localhost:8080/disability/public/admin/Admin/viewstudent
The url is
http://localhost:8080/disability/public/admin/Admin/viewstudent
Problem is the same id didn't pass

I have found the solution to the problem. I passed the parameter using query here is how it looks in view file
<a href="<?php echo $this->url('admin/default',
array('controller' => 'Admin',
'action'=>'viewstudent'
),
array('query' =>
array('id' => $student->ID)
)
);
?>">view</a>
And then using $this->params retrieve it in my controller file
$id = $this->params()->fromQuery('id');

Related

cakePHP 3 link inside email view

I have an issue build a link inside an email view, it doesn't out put the URL i'm passing from the controller for some reason..
the URL string that i'm passing to the view is missing the http://domain prefix
some action in usersController:
$email = new Email(['gmail','transport'=>'gmail']);
$email->template('recover', 'default')
->emailFormat('html')
->viewVars([
'username' => $user['username'],
'url' => '/users/resetpwd/u/'.$user['username'].'/t/'.$user['token']
])
->from(['mailer#domain.com' => 'My Site'])
->to('example#domain.com')
->subject('Password recovery')
->send()
recover.ctp:
<p>Welcome <b><?= $username; ?></b>,<br/>
you requested a password change.<br/>
To set a new password, please: <?php echo $this->Html->link('Click Here', $url, ['_full' => true,'escape' => true]); ?></p>
<?= $this->Html->image('banniere.gif', array('fullBase' => true));?><br/>
<p>This email was sent from My Site</p>
If you want to ensure that the URL is always a "full" one, then the proper solution here is Router::url(). That way you could even declare the URL in array format (which should normally be the preferred style anyways) without breaking things.
use Cake\Routing\Router;
// ...
<?= $this->Html->link('Click Here', Router::url($url, true), ['escape' => true]); ?>

How to populate zend form data of multi array input fields

Hi I have the following form in zend
<?php
/**
* Admin/modules/admin/forms/TransportRoute.php
* #uses TransportRoute Admission Form
*/
class Admin_Form_TransportRoute extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$stopageDetailsForm = new Zend_Form_SubForm();
$stopageDetailsForm->setElementsBelongTo('transport_route_stopage');
$sd_stopage = $this->CreateElement('text','stopage')
->setAttribs(array('placeholder'=>'Stopage Name', 'mendatory'=>'true'))
->setRequired(true)
->addFilter(new Zend_Filter_StringTrim())
->setDecorators(array( array('ViewHelper') ))
->setIsArray(true)
->addValidators(array(
array('NotEmpty', true, array('messages' => 'Please enter Stopage Name')),
array('stringLength',true,array(1, 6, 'messages'=> 'Stopage Name must be 2 to 40 characters long.'))
));
$sd_stopage_fee = $this->CreateElement('text','stopage_fee')
->setAttribs(array('placeholder'=>'Route Fee', 'mendatory'=>'true'))
->setRequired(true)
->addFilter(new Zend_Filter_StringTrim())
->setDecorators(array( array('ViewHelper') ))
->setIsArray(true)
->addValidators(array(
array('NotEmpty', true, array('messages' => 'Please enter Stopage Fee')),
));
$stopageDetailsForm->addElements ( array (
$sd_stopage,
$sd_stopage_fee,
) );
$this->addSubForm($stopageDetailsForm, 'transport_route_stopage');
//all sub form end here
$id = $this->CreateElement('hidden','id')
->setDecorators(array( array('ViewHelper') ));
$this->addElement($id);
$this->setDecorators(
array(
'PrepareElements',
array('viewScript'))
);
}
}
This is absolutely working fine when I render this form as below:
<div class="row-fluid stopage_block">
<div class="span5">
<?php echo $stopageDetail->stopage;?>
</div>
<div class="span4">
<?php echo $stopageDetail->stopage_fee;?>
</div>
</div>
But at the time of adding a record, I make clones of the div of class "stopage_block" and save them in the database. Now all my concerns are how to populate all the values by using a foreach loop that were inserted through clones of the div.
I have the following arrays
array('stopage' => 'India','stopage_fee' => 5000);
array('stopage' => 'US','stopage_fee' => 50000);
array('stopage' => 'Nepal','stopage_fee' => 2000);
How to populate back these values in my current form by using any loop or something else.
Thanks.
There is a method getSubForms in Zend_Form, you can use it.
Also, I would recommend you to take a look at the following article http://framework.zend.com/manual/1.11/en/zend.form.advanced.html. I guess it's exactly what you are looking for.

Show 404 error in fuelphp pagination

I want to use pagination in FuelPHP, I'm using this code:
$config = array(
'pagination_url' => 'http://localhost/live/public/index.php/admin/accounts/index/',
'total_items' => Model_Account::count(),
'per_page' => 10,
'uri_segment' => 3,
// or if you prefer pagination by query string
//'uri_segment' => 'page',
);
$pagination = Pagination::forge('mypagination', $config);
$data['example_data'] = Model_Account::query()
->rows_offset($pagination->per_page)
->rows_limit($pagination->offset)
->get();
// we pass the object, it will be rendered when echo'd in the view
$data['pagination'] = $pagination;
and here's the view:
<?php echo Pagination::instance('mypagination')->previous(); ?>
<?php echo Pagination::instance('mypagination')->render(); ?>
<?php echo Pagination::instance('mypagination')->next(); ?>
<?php echo Pagination::instance('mypagination')->last(); ?>
But when I try to click on any pagination in frontend then it sends error: 404 page not found
You will need to have a route set up so fuel knows how to map the URI admin/accounts/index/:page to the correct controller.
Something like the below should work in your routes.php config file. (Might need tweaking depending on your app)
'admin/accounts/index/:page' => 'admin/accounts/index'

Load Form from module into custom page template

I have successfully added my own form (from the same module) into my custom template, but now I wish to load the taxonomy add term form (used by ubercart I think for product categories in the catalog vocab) into my template.
I have gotten this far with my module - filename simpleadmin.module
/**
* #file
* A module to simplify the admin by replacing add/edit node pages
*/
function simpleadmin_menu() {
$items['admin/products/categories/add'] = array(
'title' => 'Add Category',
'page callback' => 'simpleadmin_category_add',
'access arguments' => array('access administration pages'),
'menu_name' => 'menu-store',
);
return $items;
}
function simpleadmin_category_add() {
module_load_include('inc', 'taxonomy', 'taxonomy.admin');
$output = drupal_get_form('taxonomy_form_term');
return theme('simpleadmin_category_add', array('categoryform' => $output));
}
function simpleadmin_theme() {
return array(
'simpleadmin_category_add' => array(
'template' => 'simpleadmin-template',
'variables' => array('categoryform' => NULL),
'render element' => 'form',
),
);
}
?>
And as for the theme file itself - filename simpleadmin-template.tpl.php, only very simple at the moment until I get the form to load into it:
<div>
This is the form template ABOVE the form
</div>
<?php
dpm($categoryform);
print drupal_render($categoryform);
?>
<div>
This is the form template BELOW the form
</div>
Its telling me that it is
Trying to get property of non-object in taxonomy_form_term()
and throwing up an error. Should I be using node_add() and passing the nodetype?
To render a taxonomy term form, the function should be able to know the vocabulary to which it belongs to. Otherwise how would it know which form to show? I think this is the proper way to do it.
module_load_include('inc', 'taxonomy', 'taxonomy.admin');
if ($vocabulary = taxonomy_vocabulary_machine_name_load('vocabulary_name')) {
$form = drupal_get_form('taxonomy_form_term', $vocabulary);
return theme('simpleadmin_category_add', array('categoryform' => $form));
}
To redirect your form use hook_form_alter
function yourmodule_form_alter(&$form, &$form_state, $form_id) {
//get your vocabulary id or use print_r or dpm for proper validation
if($form_id == 'taxonomy_form_term' && $form['#vocabulary']['vid'] = '7' ){
$form['#submit'][] = 'onix_sections_form_submit';
}
}
function yourmodule_form_submit($form, &$form_state) {
$form_state['redirect'] = 'user';
}

Problem passing URL variables in post form submission with CakePHP FormHelper

I'm writing my first CakePHP application and am just writing the second part of a password reset form where a user has received an email containing a link to the site and when they click it they're asked to enter and confirm a new password.
The url of the page is like this:
/users/reset_password_confirm/23f9a5d7d1a2c952c01afacbefaba41a26062b17
The view is like:
<?php echo $form->create('User', array('action' => 'reset_password_confirm')); ?>
<?php
echo $form->input('password', array('label' => 'Password'));
echo $form->input('confirm_password', array('type' => 'password', 'label' => 'Confirm password'));
echo $form->hidden('static_hash');
?>
<?php echo $form->end('Reset password'); ?>
However this produces a form like:
<form id="UserResetPasswordConfirmForm" method="post" action="/users/reset_password_confirm/8">
The problem is the user id (8 in this case) is being added to the form action. It's not really a problem here, but when I want to pass through the hash to my controller:
function reset_password_confirm($static_hash=null) {
// function body
}
$static_hash is now populated with 8 rather than the hash from the URL.
I know I could sort this out by creating the form tag myself rather than using $form->create but is there a more cakey way of doing this?
$form->create('User', array('action' => '…', 'id' => false));
Just explicitly set params you don't want passed to null or false. This is unfortunately a case where Cake tries to be a little too intelligent for its own good. ;o)
You could probably also do something like this to POST to the same URL again:
$form->create('User', $this->here);
How about passing it as a parameter instead of form data :
<?php
echo $form->create('User', array('action' => 'reset_password_confirm', $static_hash));
echo $form->input('password', array('label' => 'Password'));
echo $form->input('confirm_password', array('type' => 'password', 'label' => 'Confirm password'));
echo $form->end('Reset password');
?>
and in the controller :
function reset_password_confirm($static_hash = null) {
// Check if form is submitted
if (!empty($this->data)) {
// if it submitted then do your logic
} else {
$this->set('static_hash', $static_hash); // Else, pass the hash to the view, so it can be passed again when form is submitted
}
}
Hope this help :)