Zend Form getValues() doesn't work - php

I'm trying to create simple form with Zend, I need to use this form in most part, so I create the default form then in controller i modify it for the occurrence with private function. But I have two problems:
the form getValues() doesn't take the value of text element.
I put render at the end of the form action, but it doesn't render to the right page.
The form consists of a text field and the sumbit button
Here is the code of my controller:
That is for customize the form
private function getSearchForm($action = '', $name, $type, $placeholder)
{
$urlHelper = $this->_helper->getHelper('url');
$this->_searchForm = new Application_Form_Admin_Search_Search();
$this->_searchForm->setName($name);
$text = $this->_searchForm->getElement('ricerca');
$text->setLabel('Ricerca '.$type);
$text->setName($type);
$text->setAttrib('placeholder', $placeholder);
$this->_searchForm->setAction($urlHelper->url(array(
'controller' => 'admin',
'action' => $action),
'default'
));
return $this->_searchForm;
}
there are the actions:
public function pneumaticoAction()
{
$this->_searchForm = $this->getSearchForm('pneumaticosearch', 'search', 'pneumatico', 'Ricerca per: modello, marchio o codice');
$this->view->searchForm = $this->_searchForm;
}
public function pneumaticosearchAction()
{
if (!$this->getRequest()->isPost()) {
$this->_helper->redirector('index', 'public');
}
$form=$this->_searchForm;
if (!$form->isValid($this->getRequest()->getPost())) {
$this->render('pneumatico');
}
$values = $form->getValues();
$this->view->assign(array(
"pneumatici" => $this->_modelAdmin->searchPneumatici($values['pneumatico'])
));
$this->render('pneumatico');
}

First question, whenever you get routed to pneumaticosearch action, you do not set $this->_searchForm but you have it as:
$form=$this->_searchForm;
Should be something like this:
$form = $this->getSearchForm('pneumaticosearch', 'search', 'pneumatico', 'Ricerca per: modello, marchio o codice');
And the second question. When you run render, it is similar to pass $this->view parameters to .phtml. I don't see your view files, but I guess you need to set view first:
$this->view->searchForm = $form

Related

Redirect to NEW Action with prefilled values in EasyAdmin 3

I am currently trying to add a Clone action to my EmployeeCrudController.
The action should redirect to the Action::NEW view and have some prefilled values.
However I can't figure out how to prefill this form.
Here is where how I define my action within the EmployeeCrudController:
public function configureActions(Actions $actions): Actions
{
$cloneAction = Action::new('Clone', '')
->setIcon('fas fa-clone')
->linkToCrudAction('cloneAction');
return $actions->add(Crud::PAGE_INDEX, $cloneAction);
}
And this is how my cloneAction looks like, which currently redirects to the Action::NEW as expected but without prefilled values:
public function cloneAction(AdminContext $context): RedirectResponse
{
$id = $context->getRequest()->query->get('entityId');
$entity = $this->getDoctrine()->getRepository(Employee::class)->find($id);
$clone = new Employee();
$entity->copyProperties($clone);
$clone->setFirstname('');
$clone->setLastname('');
$clone->setEmail('');
$routeBuilder = $this->get(CrudUrlGenerator::class);
$url = $routeBuilder->build([
'Employee_lastname' => 'test',
'Employee[teamMembershipts][]' => $clone->getTeamMemberships(),
])
->setController(EmployeeCrudController::class)
->setAction(Action::NEW)
->generateUrl()
;
return $this->redirect($url);
}
You can set the value of a field in easyAdmin using the option data.
$builder->add('Employee_lastname', null, ['data' => $clone->getTeamMemberships()]);
If your field has multiple options, you can use the choices and choices_value.

Passing data from controller function to controller function before redirect

I am using codeigniter I have an edit page which shows me all information of a vacancy.
The (Vacancy) controller method to load this view looks like this, it makes sure all data is preloaded.
public function editVacancy($vacancyid)
{
$this->is_logged_in();
$this->load->model('vacancy/vacancies_model');
// Passing Variables
$data['title'] = 'Titletest';
$data['class'] = 'vacancy';
$orgadminuserid = $this->vacancies_model->getOrgAdminUserId($vacancyid);
if ((!is_null($orgadminuserid)) && ($this->auth_user_id == $orgadminuserid[0]->user_id)) {
$data['vacancyid'] = $vacancyid;
$data['vacancy'] = $this->vacancies_model->get($vacancyid);
$data['test'] = $this->session->flashdata('feedbackdata');
$partials = array('head' => '_master/header/head', 'navigation' => '_master/header/navigation_dashboard', 'content' => 'dashboard/vacancy/edit_vacancy', 'footer' => '_master/footer/footer');
$this->template->load('_master/master', $partials, $data);
}
}
In this view i have different forms for updating different sections.
Every form submit goes to a different method in my 'Vacancy' controller.
public function saveGeneralInfo()
{
$this->is_logged_in();
$this->load->model('vacancy/vacancies_model');
$vacancyid = $this->input->post('vacancyid');
$vacancyUpdateData = $this->vacancies_model->get($vacancyid);
$result = $this->vacancies_model->update($vacancyid, $vacancyUpdateData);
if ($result) {
$feedbackdata = array(
'type' => 'alert-success',
'icon' => 'fa-check-circle',
'title' => 'Success!',
'text' => 'De algemene vacature gegevens zijn geupdate.'
);
$this->session->set_flashdata('feedbackdata', $feedbackdata);
redirect("dashboard/vacancy/editVacancy/" . $vacancyid);
}
}
}
Where I indicated in my code "//HERE ..." is where I would want the feedback message parameter to pass on to my 'main controller method' which loads the view with the prefilled data. (editVacancy).
Is there a clean way to do this?
EDIT:
I tried using flashdata, i updated the code to have the flashdata inserted.
However when i do a var_dump($test); in my view, it remains null.
EDIT 2:
I noticed when i put my $_SESSION in a variable in my editVacancy controller method (which is being redirected to) and var_dump it in my view that this does not contain the ci_vars with the flashdata in.
Instead of using set_header you can use simple redirect function for it.
redirect("dashboard/vacancy/editVacancy/".$vacancyid);

Call another action from one action of the same controller

I am new to YII. How to call an action from another action of the same controller.
Suppose I am in action A. I need to call action B with two parameters. My controller name is Master Controller. How will I perform this. Please say me a solution. Thanks in advance.
My controller:
class NimsoftController extends Controller
{
public function actionStore_action($action,$data_id)
{
$model= new NimsoftUserLoginAction;
$model->user_id=Yii::app()->user->id;
$model->action=$action;//"Deleted";
$model->affected_data_id=$data_id;//"22";
$model->save();
}
public function actionSearch($id)
{
$cust_id = $id;
$criteria = new CDbCriteria();
$criteria->condition = "host_customer_id = '$cust_id'";
$details = NimsoftHost::model()->find($criteria);
$criteria2 = new CDbCriteria();
$criteria2->condition = "cust_id= '$details->host_customer_id'";
$name = MasterCustomers::model()->find($criteria2);
$dataProvider = new CActiveDataProvider('NimsoftHost', array(
'criteria' => $criteria,
'pagination'=>array('pageSize'=>40)
));
$model = new NimsoftHost();
$this->actionStore_action(array('action'=>'search', 'data_id'=>$cust_id));
$this->render('index',array(
'dataProvider' => $dataProvider,
'details' => $details,
'name' => $name->cust_name,
'model' => $model
));
}
}
You can simply call the function directly, or,
$this->actionMyCustomAction(); exit;
you can perform a redirect to the action:
$this->redirect('anotherControllerName/myCustomAction');
or this if you are in the same controller:
$this->redirect('myCustomAction');
UPDATE:
You are passing the variables wrongly:
$this->actionStore_action(array('action'=>'search', 'data_id'=>$cust_id));
It should be two separate variables:
$this->actionStore_action('search', $cust_id);
This has NOTHING to do with Yii. It's only basic function calling.
$this->forward('site/contact');
In case of forward your URL will not be change and you cannot send extra parameter in this.
If you want to send extra parameter then use redirect.
$this->redirect(Yii::app()->createUrl('site/contact', array('id' => '22')));
Hope this will help you..
You can do it this way also:-
Now you want to call action Name1 inside action Name2.
public function actionName1($param1,$param2) {
//your code...
}
public function actionName2() {
$this->redirect(array('name1','param1'=>'param1','param2'=>'param2'));
}
In case of forward your URL will not be change and you cannot send extra parameter in this.
$this->redirect('contact');

Add Custom Row action in Prestashop ModuleAdminController

I want to add a download button for each row in moduleadmincontroller helper.
I tried to add it by using the following code on RenderList function. But it is not working.
$this->addRowAction('download');
Kindly let me know if I can add custom action for each row and how to process it.
as you know the actions is the default array that have default value array('view', 'edit', 'delete', 'duplicate'); and you can use this but if you want add new action you should use some function.for example you can go to your_prestashop/controllers/admin/AdminRequestSqlController.php
this class add new action with 'export' name
$this->addRowAction('export');
then for create link for this action it is using the displayExportLink() function as you can see in bellow code
public function displayExportLink($token, $id)
{
$tpl = $this->createTemplate('list_action_export.tpl');
$tpl->assign(array(
'href' => self::$currentIndex.'&token='.$this->token.'&
'.$this->identifier.'='.$id.'&export'.$this->table.'=1',
'action' => $this->l('Export')
));
return $tpl->fetch();
}
and then you can get your new action with the initProcess() function or initcontent() function and do something lik download
public function initProcess()
{
parent::initProcess();
if (Tools::getValue('export'.$this->table))
{
$this->display = 'export';
$this->action = 'export';
}
}

How to pass variable from action to form

I am sure I am going about this the wrong way, but I need to unset an array key from one of my choices in a sfWidgetFormChoice. The only way to get that variable to the Form is from the action. Here's what I have:
Action:
$id = $request->getParameter('id');
$deleteForm = new UserDeleteForm();
$choices = array();
$choices = $deleteForm->getWidgetSchema('user')->getAttribute('choices');
unset($choices[$id]); //I obviously don't want the user to be able to transfer to the user being deleted
$this->deleteForm = $deleteForm;
Form:
$users = Doctrine_Core::getTable('sfGuardUser')->getAllCorpUsers()->execute();
$names = array();
foreach($users as $userValue){
$names[$userValue->getId()] = $userValue->getProfile()->getFullName();
};
// unset($names[$id]); //this works, but I can't figure out how to get $id here.
$this->widgetSchema['user'] = new sfWidgetFormChoice(array(
'choices' => $names
));
$this->validatorSchema['user'] = new sfValidatorChoice(array(
'required' => true,
'choices' => $names
));
Understanding forms and actions:
Usually we will setup a form with fields, print it in a html page and fill the form with data. Pressing the submit form button will send all the data to a method defined in your form action html attribute.
The method will receive and get a $request , with a lot of parameters and also the form with the data. Those values will be processed in the action.
Lets look how it exactly works in symfony:
Define and Setup a symfony form, like the one you have shown above.
Print the form and in the action parameter point to the submit method
which will receive the request:
<form action="currentModuleName/update"
Symfony will automatically send the request to the action.class.php
of your module, and will look for and send the data to the function
executeUpdate
public function executeUpdate(sfWebRequest $request){ //...
$this->form = new TestForm($doctrine_record_found);
$this->processForm($request, $this->form); }
After some checks, symfony will process the form and set a result
template.
processForm(sfWebRequest $request, sfForm $form)
{ ... } $this->setTemplate('edit');
In the processForm of your module action.class.php, you should process all the received values (request) also with the form:
protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid())
{
$formValues = $this->form->getValues();
$Id = $formValues['yourWidgetName'];
}
}
You may check the following link for an example like yours, about how to process a sfWidgetFormChoice.
And now answering to the real question, in order to select the deleted users, add the following code in your action:
//process the form, bind and validate it, then get the values.
$formValues = form->getValues();
$choicesId = $formValues['choices'];
Pass variable from action to the form:
Excuse me if I have not understand your question at all but in case you need to pass some parameters from your action to the form, send the initialization variables in an array to the form constructor:
Pass a variable to a Symfony Form
In your case, get the list of users, delete the user you dont want and send the non deleted users to the form constructor.
You will need to redeclare/overwrite your form again in the configure() function so that you could change the initialization of the form. Copy and paste the same code into the configure() function and comment the line: //parent::setup();
class TbTestForm extends BaseTbTestForm
{
public function configure()
{
//.. copy here the code from BaseTbTestForm
//parent::setup();
$vusers = $this->getOption('array_nondeleted_users');
//now set the widget values with the updated user array.
}
}

Categories