how do you get a variable from a post address in zendframework 1 - zend-framework

I am still new to Zend Framework and confused about a few concepts.
I have built a POST form and attached a unique Id to the URL at the end of the form. I now want to collect that Id when the form is submitted but I am unclear how to do that
I will show you want I have done:
Below is the function that renders the form from my controller page to the view. You will note that I have fed into the parameter, for the form, a return Action address with the ID
$action = "{$this->view->baseUrl()}/sample-manager/process-price/{$sampleId}";
$this->view->Form = $model= $this->_model->createForm($action);
The function to receive the post is below. However, I want to collect the Id that should have come back with the post return values, but I have no idea where to find it or how to attach it.
public function processPriceAction()
{
$this->requirePost();
if($this->_model->processTieredPriceForm($this->view->form, $this->getRequest()->getPost()))
{
$this->_helper->FlashMessenger('Changes saved');
return $this->_redirect("/product-ecommerce/{$this->_model->getProduct()->id}");
}
else
{
return $this->render('index');
}
}
In summary, when a post is returned, does the return address come with the post in Zend Framework?

Could you not supply the id into the construction of the form and assign it to a hidden element? For example, in your controller:
$action = "{$this->view->baseUrl()}/sample-manager/process-price";
$this->view->Form = $model= $this->_model->createForm($action, $sampleId);
In your form model (not provided so best guess here):
$sampleId = new Zend_Form_Element_Hidden('sampleId');
$sampleId->setValue($sampleId);
$form->addElement($sampleId);
Then once the form is posted, you should be able to get the sample id in your controller in the standard way:
$sampleId = $this->getParam('sampleId');

The answer depends a bit on how your routing is setup. If you're using the default setup, after the action name the default route allows for key/value pairs of additional data. So, you might have more luck with a URL like this:
{$this->view->baseUrl()}/sample-manager/process-price/id/{$sampleId}
That'll put your sampleId in a named parameter called 'id', which you can access in your controller action with $this->_getParam('id').

Related

Find the context id inside a block in Moodle

How to get the context and context id in Moodle 2.9.1.
Currently I am in a block : Question Paper
In form submission action page I need the context id. I don't know how to get the context is inside a block (or module). My code is look like this:
question_action.php
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/locallib.php');
global $DB, $CFG;
require_once("$CFG->libdir/resourcelib.php");
if(isset($_GET['id'])){
$cid = $_GET['id'];} //course id
if(isset($_GET['poolid'])){
$paper= $_GET['paper'];} //question paper id
How I find the context and context id here..
Inside the block get_content() function, you can get the contextid from $this->context->id.
If you have an extra PHP page within your block, you will need to make sure that any links have some sort of identifier added as a parameter (that could be the courseid, the blockid or the contextid).
Assuming all your links have the courseid at the end of them (probably the most sensible choice), on the page itself you can write:
$courseid = required_param('id', PARAM_INT); // Do not use $_GET directly.
$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST); // Optional, but you often need the course object.
$context = context_course::instance($courseid);
$contextid = $context->id;

How to send and access a parameter/value upon Form Submission to next controller

I am new to Laravel.
I have a controller method which retrieves data from request, creates a model instance
and saves it to database.
It then redirects to controller with a value (user name)
return redirect()->action('SignupController#confirm' , $username);
Route for confirm is :
Route::get('confirm/{user}' , 'SignupController#confirm');
In 'confirm' method i retrieved value from variable and passed it to view
public function confirm($username)
{
return view('auth.confirm')->with('username' , $username);
}
Confirm view present a form to confirm account and upon succesful submission post to route :
Route::post('confirm' , 'SignupController#confirmCode');
In 'confirmCode' i want to access that value which i actully fetched from user input
and passed down the line.
But i am unable to do it , i even tried post route with a wild card
Route::post('confirm/{user}' , 'SignupController#confirmCode');
and tried to access it similarly as before , but it is not passed along when form submits as i get nothing when i
tried to look for it using var_dump($username).
Error is :
Missing argument 1 for App\Http\Controllers\SignupController::confirmCode()
By the way, temporarily i am using hidden field to do the job which is not a good idea obviously.
I know i am missing something. Looking forward for some advice. Thanks in advance.
You can simply use Session::flash or if it's more than one redirect then you can use Session::put.
use Session; // at the top between class and namespace
public function confirm($username)
{
Session::flash('username', $username);
// Session::put('username',$username);
return view('auth.confirm')->with('username' , $username);
}
public function confirmCode() {
dd(Session::get('username'));
}

Symfony2 - Multiple forms in one action

I implemented a page to create an instance of an entity and a user related to this. My problem is to bind the request after the submit.
Now i have this :
$formA = $this->createForm(new \MyApp\ABundle\Form\AddObjectForm());
$formB = $this->createForm(new \MyApp\UserBundle\Form\AddUserForm());
if ($request->getMethod() == 'POST')
{
$formA->bindRequest($request);
$formB->bindRequest($request);
if ($formA->isValid() && $formB->isValid())
{
}
// ...
}
With formA and formB extends AbstractType. But, naturally, $formA->isValid() returns false. How can I do to "cut" the request for example ?
If your forms are related and need to be processed and validated at once, consider using embedded forms. Otherwise, use a separate action for each form.
If you need to provide a select field to choose a user from existing ones, consider using an entity type field.
I know that has been a long time since the answer, but maybe if someone is looking for it this could be helpfull.
I've got two forms: user_form and company_form, and the submit has take place in the same function of the controller. I can know wich form has been submit with the follow code:
if ($request->getMethod() == 'POST') {
$data = $request->request->all();
if (isset($data['user_form'])) //This if means that the user_form has been submit.
{
The company_form will pass through the else.

Zend Form MutliCheckbox Validate Number of Checked Items

I have a Zend Form with a MutliCheckbox element.
I would like to validate the number of checked items, i.e. verify that exactly 3 items are checked.
Can I do it with any current validates or do I have to write my own?
Thanks.
You will have to write your own, but that's quite simple. There is a second optional argument on the isValid() method that gives you access to all the form values, and enables this way to validate against multiple inputs.
class MyValidator extends Zend_Validate_Abstract {
public function isValid($value, $formData = null){
//you can access to all the form values in the $formData, and check/count
//the values of your multicheckbox
//this is the super-quick way, but you could also add error messages
return $isValid;
}
}
and then add it to your element
$myElement->addValidator( new MyValidator());

Render action to HTML email in Zend Framework

I have an action which is rendering some content via a layout.
I actually want to send this output in an email. What is the best way to achieve this in the Zend Framework?
I know I need to use the Zend_Mail component to send the email, but I'm unclear about how to attach the output of my action to Zend_Mail.
I've done some reading on the ContextSwitch action helper, and I think that might be appropriate, but I'm still not convinced.
I'm still new to Zend Framework. I'm used to using techniques like output buffering to capture output, which I don't think is the correct way to do this in Zend.
From your controller:
// do this if you're not using the default layout
$this->_helper->layout()->disableLayout();
$this->view->data = $items;
$htmlString = $this->view->render('foo/bar.phtml');
If you're doing this from a class that's not an instance of Zend_Controller_Action, you may have to create an instance of a Zend_view first, to do this:
$view = new Zend_view();
// you have to explicitly define the path to the template you're using
$view->setScriptPath(array($pathToTemplate));
$view->data = $data;
$htmlString = $view->render('foo/bar.phtml');
public static function sendMail($data = array(), $template = ''){
$html = new Zend_View();
$html->setScriptPath(APPLICATION_PATH . '/modules/default/views');
// assign valeues
if(count($data['Assigni'])){
foreach($data['Assigni'] as $assign){
$html->assign($assign['key'], $assign['value']);
}
}
// create mail object
$mail = new Zend_Mail('utf-8');
// render view //'scripts/newsletter/emailtemplate.phtml'
$bodyText = $html->render($template);
$mail->addTo($data['To']);
$mail->setSubject($data['Subject']);
$mail->setFrom($data['From'], $data['FromName']);
$mail->setBodyHtml($bodyText);
$mail->send();
}
when you dispatch the action, you can catch the event in postDispatch() method of plugin, that you can dynamically add to the stack from desired action. In that you recieve the contents of response by
//in action
//...some php code
Zend_Controller_Front::getInstance()->registerPlugin(new My_Plugin());
//in plugin
$htmlCode = $this->_response->getBody();
I can't give you a super-detailed answer, but if you want the full output (including the layout), I think you want to write your email function as an Action helper, and insert it at the PostDispatch hook of the Zend_Controller_Action->dispatch() loop.
See http://nethands.de/download/zenddispatch_en.pdf for the full Zend Framework Dispatch Process Overview.
If you don't need the layout included in your email content, then you could do this at many points, including by the use of a context switch, as you mentioned.