How to post single perameter in zend framwork? - zend-framework

I have api controller and it logout method
public function logoutAction()
{
if(!$this->getRequest()->isPost())
{
$response= $this->getResponse();
$response->setHttpResponseCode(406);
return $this->_helper->json->sendJson(array('status'=>'false'));
}
else
{
$data=$this->_request->getParams();
}
}
When i posting id with some value(assume we take id= 5) then $data['id'] value showing wrong value 5id=5 instead of only 5.
But when i posting multiple parameter with value like id=5,name=abc etc then id value display true value 5.I need to post only one id.help please ...thanks

Related

Laravel persist $request->flash old() session

So, I've build in functionality which redirects the logged in user to a pincode screen after 10 minutes of inactivity, when the user enters the correct pincode he or she is redirected to the page where he or she was navigating to.. So far so good but...
Imagine the user is filling out a form and waits ten minutes before hitting the submit button and is then redirected to the pincode page, after inputting the correct pincode he is on the form again but all data on it is gone.
What I want is to remember all the filled data, I've tried it via the old() method but that only persists for one request.
PincodeCheck Middleware
if($difference_in_minutes > config('pincode.lifetime')) {
return redirect()->route('pincode')->withInput();
}
PincodeController
public function index(Request $request)
{
// $request->old() is holding the values
return view('pincode.index');
}
public function unlock(Request $request)
{
// This is the after submit function, $request->old() is empty here
if(Hash::check($request->pincode, auth()->user()->pincode) == true) {
$request->session()->put('pincode-timestamp', date('U'));
$path = config('app.homeroute');
if($request->session()->has('intended.get.path')) {
$path = $request->session()->get('intended.get.path');
}
return redirect($path);
}
return redirect()->route('pincode');
}

MVC How To Pass Url Values as Well as a Model From Action to a View

I am looking for a way to preserve a url parameter after posting through a form. For example my GET method takes a string "type" and uses that to determine the type of report to render in the View. The url looks like this:
http://mysite/Reports/Report?type=1
[HttpGet]
public ActionResult Report(string type)
{
var model = new ReportsModel()
{
Report = ReportList.Find(o => o.ReportType == type)
};
return View(model);
}
The View has a form that has start/end date filters used to determine the date range of the date to be displayed for the type of report:
#using (Html.BeginForm("Report", "Reports"))
{
Report.ReportName
#Html.HiddenFor(o => o.Report.ReportType)
#Html.EditorFor(o => o.Report.StartDate )<br/>
#Html.EditorFor(o => o.Report.EndDate )<br/>
<button id="reports">Report</button>
}
The above form posts to an action that gets report data from the database based on the specified report type, start/end dates, and returns back to the view.
[HttpPost]
public ActionResult Report(GenericReportsModel model)
{
switch (model.Report.ReportType)
{
case ReportType.ReportType1:
model.Result = ReportRepository.GetReport<ReportType1>(model.StartDate, model.EndDate);
break;
case ReportType.ReportType2:
model.Result = ReportRepository.GetReport<ReportType2>(model.StartDate, model.EndDate);
break;
}
return View(model);
}
The problem is that after the post, the "type" parameter is lost from the url.
Before the post: http://mysite/Reports/Report?type=1
After the post: http://mysite/Reports/Report
I need to be able to do something like this (which doesn't work):
return View(model, new {ReportType = model.ReportType);
How can I preserve the type parameter in the url after the post, in case someone wants to copy and paste the url to send to someone else?
You need to update Html.BeginForm and your HttpPost version of Report method.
#using(Html.BeginForm("Report", "Report", "YourController", new { type = model.ReportType})
{
// I am assuming that model.ReportType == type argument
// in your HttpGet Report action
// The rest of the form goes here
}
Your action should look like:
[HttpPost]
public ActionResult Report(string type, GenericReportsModel model)
{
switch (model.Report.ReportType)
{
case ReportType.ReportType1:
model.Result = ReportRepository.GetReport<ReportType1>(model.StartDate, model.EndDate);
break;
case ReportType.ReportType2:
model.Result = ReportRepository.GetReport<ReportType2>(model.StartDate, model.EndDate);
break;
}
return View(model);
}
If type is not equal to model.ReportType then you should create a ViewModel that contains the values from your GenericsReportModel and this other Report type.

If form validation fails

I want to redirect people to a certain page if the form validations fails. However, I can’t quite figure out how.
If i redirect people at the REDIRECT HERE comment below, it also redirects when it loads up the form and causes a endless loop.
public function create() {
$this->form_validation->set_rules('email_adress', 'E-mail', 'required|valid_email|is_unique[users.email_adress]');
if ($this->form_validation->run() !== FALSE) {
// PASSED
}
else {
// REDIRECT HERE
}
$this->load->view('user_register_view');
}
How can I achieve this?
You would redirect like so:
redirect('insert_URI_here');
Or if you are passing it to another method in the same controller you could just do :
$this->method_name();
However, if you just drop that in your else statement, user_register_view will not load because $this->form_validation->run() will return false either on validation error or non submission of the form.
What you will have to do is add another check to look for validation errors. If validation has failed and there are no validation errors, then your form hasn't been submitted.
So you could do something like this:
public function create() {
$this->form_validation->set_rules('email_adress', 'E-mail', 'required|valid_email|is_unique[users.email_adress]');
if(($this->form_validation->run() == FALSE) && ($this->form_validation->error_string() == ''));
//form not submitted yet
$this->load->view('user_register_view');
else if ($this->form_validation->run()) {
// PASSED
} else {
//Validation errors
redirect('insert_URI_here');
}
}
You will have to play around with it. I think $this->form_validation->error_string() should return an empty string if not submitted, but it might be a null value or a false (sorry can't remember off the top of my head).

another approach to returning some thing to browser in mvc with ajax call instead of using response.write

i have one section in my mvc 2.0 project which doing some processes and after each, return some messages (string) with response.write(). and this messages returned to browser with bad format. i want to return messages to one specific HTML div and add each to end of contents of div tag. now how do this?
this event after each procces raised and message returned to browser.
public void OnProgressEvent(System.Object source, CustomEventArgs customEventArgs)
{
if (customEventArgs.Level > 5)
{
Response.Write(customEventArgs.Message + "<br />");
Response.Flush();
}
}
jQuery is your friend here. If you make an ajax call to say an ActionResult, then you can either return a json object or a partial view.
My preference is to return a partial view and then replace the contents of the div with the resultant html.
So;
public ActionResult jQueryTagDelete(string SomeParametersMaybe)
{
return PartialView("TagList", tags.OrderBy(x => x.keyword1));
}
And you jQuery code;
function deleteTag(tagName) {
$.post("/Admin/jQueryTagDelete", { tag: tagName }, function(RETURNED_HTML) {
document.getElementById("divTags").innerHTML = RETURNED_HTML;
});
}

Symfony form values missing

I was writing a simple login form, everything works fine (validation etc.) but I can't get the values, there's my code:
public function executeIndex(sfWebRequest $request)
{
$this->getUser()->clearCredentials();
$this->getUser()->setAuthenticated(false);
$this->form = new LoginForm();
if ($request->isMethod('post') && $request->hasParameter('login')) {
$this->form->bind($request->getParameter('login'));
if ($this->form->isValid()) {
$this->getUser()->setAuthenticated(true);
$this->getUser()->addCredential('user');
$this->login = $this->form->getValue('login');
}
}
}
$this->login is NULL. Now I checked almost everything, the form is valid, isBound() is true, count() returns 3, I can see the values in my request:
parameterHolder:
action: index
login: { login: foo, password: foo, _csrf_token: 53ebddee1883d7e3d6575d6fb1707a15 }
module: login
BUT getValues() returns NULL, getValue('login') etc. returns NULL as well. How can it be?
And no, I don't want to use sfGuard-Plugins ;)
What about trying something like this
$form['value_name']->getValue()
Is it still NULL?
Also is it possible that you created a custom post validator?
Callback validation must return values back to caller:
return $values;