Zend Action viewRenderer() is POST-ing my form - php

I am trying to send a parameter from the indexAction to the editAction using the viewRender function. The problem is when the editAction is called it causes my $form to think it has been posted.
public funciton indexAction(){
...
if(isset($_POST['edit'])){
$this->_helper->viewRenderer('edit');
$this->editAction($thingINeed);
}
...
}
public function editAction($thingINeed){
...
if($form->posted){
var_dump('FORM POSTED');
}
...
}
"FORM POSTED" is printed immediately even though I have not posted the form yet. I'm not sure why the form $form->posted is set to true on the initial render. Does anyone have an idea of why this is or a work around?

You should check your form like this:
$form = new MyForm();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
echo 'success';
exit;
} else {
$form->populate($formData);
}
}
$this->view->form = $form;

I'm not sure about what you want to obtain, but in order to communicate value between two action, it should be better to use _getParam and _setParam method :
public funciton indexAction(){
...
if(isset($_POST['edit'])){
$this->_setParam( 'posted', true );
$this->_helper->viewRenderer('edit');
//$this->editAction($thingINeed);
// It should be better to use Action stack helper to route correctly your action :
Zend_Controller_Action_HelperBroker::getStaticHelper( 'actionStack' )->actionToStack( 'edit' );
} else {
$this->_setParam( 'posted', false );
}
...
}
// param $thingINeed is not "needed" anymore
public function editAction(){
...
if( true == $this->_getParam( 'posted' ) {
var_dump('FORM POSTED');
}
...
}

Related

How to print out text in beforeAction in Yii2

How to print out data within function beforeAction? I want to make some verification before each action in a controller, therefore if some condition occurs in beforeAction I should print out data and prevent further execution, for example, JSON:
[
status: "error",
msg: "access denied"
]
I try to even inner redirect to another controller, but it doesn't work.
public function beforeAction($action)
{
$request = Yii::$app->request;
if ( ! checkByToken($request->get('token')) && $this->getRoute() != 'web/abonent/token_error') {
\Yii::$app->runAction('web/abonent/token_error');
return true;
}
return parent::beforeAction($action); // TODO: Change the autogenerated stub
}
But maybe there an another concept of doing so. I just need to check the condition before any actions and print our result or let the action execute.
To prevent further execution:
public function beforeAction($action) {
return false; // key point
}
To print out data within beforeAction:
public function beforeAction($action) {
// set response format = json:
Yii::$app->response->format = Response::FORMAT_JSON;
// then, set the response data:
Yii::$app->response->data = [
'status' => 'error',
'msg' => 'access denied'
];
return false;
}
I think will be better
public function beforeAction($action)
{
$request = Yii::$app->request;
if ( ! checkByToken($request->get('token')) && $this->getRoute() != 'web/abonent/token_error') {
$action = 'error';
}
return parent::beforeAction($action); // TODO: Change the autogenerated stub
}
Action name must be 'actionError'

zend framework 3 tutorial

I am trying a Zend framework 3 tutorial and am getting stuck in "editing" a function in the in-depth part (Blog case).
When trying to edit a blog message, the editing form doesn't show the original message. It seems that the original message couldn't be bound to the form.
I copied all the sample code. I don't know what is wrong with it. By the way, my add and delete function work fine.
can anyone help me with it?
The editAction method from the tutorial:
public function editAction()
{
$id = $this->params()->fromRoute('id');
if (! $id) {
return $this->redirect()->toRoute('blog');
}
try {
$post = $this->repository->findPost($id);
} catch (InvalidArgumentException $ex) {
return $this->redirect()->toRoute('blog');
}
$this->form->bind($post);
$viewModel = new ViewModel(['form' => $this->form]);
$request = $this->getRequest();
if (! $request->isPost()) {
return $viewModel;
}
$this->form->setData($request->getPost());
if (! $this->form->isValid()) {
return $viewModel;
}
$post = $this->command->updatePost($post);
return $this->redirect()->toRoute(
'blog/detail',
['id' => $post->getId()]
);
}
Edit this code:
if (! $request->isPost()) {
foreach($this->form->getMessages() as $message){
$this->flashMessenger()->addErrorMessage($message['message']);
}
}
In your view:
<?php echo $this->flashMessenger()->renderCurrent('error', ['options go here...']); ?>

Validation for Add screen not working but it working in edit screen with same code

I am learning Zend framework and currently I have created add, update , delete functionality for country name and continent name and it is working perfectly.
I have set validation by
$name->setRequired('true');
and
$continent->setRequired('true');
in my form.php.
Validation is working in edit form but it return error 'An error occurred' and 'Application error' in add form.
Below is my controller code:
for Add:
/*Add Record into Database*/
public function addAction()
{
$form =new Application_Form_Add();
$form->submit->setlabel('Add Country');
$this->view->form = $form;
if($this->getRequest()->ispost())
{
$formData = $this->getRequest()->getpost();
if($form->isvalid($formData))
{
$file = new Application_Model_Country();
$name = $form->getvalue('name');
$continent = $form->getvalue('continent');
$file->addCountry($name, $continent);
$this->_helper->redirector('index');
}
else
{
$this->populate($formData);
}
}
}
for Edit:
/*Edit Record into Database*/
public function editAction()
{
$form = new Application_Form_Edit();
$form->submit->setlabel('Edit Country');
$this->view->form = $form;
if($this->getRequest()->ispost())
{
$formData = $this->getRequest()->getpost();
if($form->isvalid($formData))
{
$id = $form->getvalue('country_id');
$name = $form->getvalue('name');
$continent = $form->getvalue('continent');
$file = new Application_Model_Country();
$file->updateCountry($id,$name,$continent);
$this->_helper->redirector('index');
}
else
{
$form->populate($formData);
}
}
else
{
$id = $this->getRequest()->getparam('country_id');
if($id >0)
{
$formData = $this->getRequest()->getpost();
$file = new Application_Model_Country();
$files = $file->fetchRow('country_id='.$id);
$form->populate($files->toArray());
}
}
}
Both code are same, then why validation not working in add form?
You need to change following code in the addAction logic:
instead of:
$this->populate($formData);
use
$form->populate($formData);
The reason is $this means Action object in this context and you have correctly used $form object in EditAction so it is working properly, so it is kind of silly typing mistake.
PS: you should also use proper case in method names like isPost, isValidate etc. otherwise may get errors in Linux environment.

cakephp form action link

i am developing with cakephp (2.4.7) and i have a problem with a form action link.
I'm having a usersController with edit action.
public function edit($id = null, $slug = null) {
if (!$id) {
throw new NotFoundException(__('Invalid User'));
}
$user = $this->User->findById($id);
if (!$user) {
throw new NotFoundException(__('Invalid User'));
}
if ($this->request->is(array('post', 'put'))) {
// Do stuff here
}
// Fill the form
if (!$this->request->data) {
$this->request->data = $user;
}
}
with this code the form ($this->create->('User')); in the edit view get filled correctly. But i have another form in the edit view.
Like:
echo $this->Form->create(null, array(
'url' => array('controller' => 'useraddresses', 'action' => 'add')
));
echo $this->Form->input('searchvalue');
echo $this->Form->hidden('country');
echo $this->Form->hidden('city');
echo $this->Form->end('save');
When i click the send button from this form, the page links to /useraddresses/add/2 (2 is the id of the user)
I have debuged the form with firebug and in the action parameter is also /useraddresses/add/2.
How can i get arround this? I will to send the form to /useraddresses/add without any parameters.
If i delete this piece of code in my edit action, the action link is correctly but my first form does not get filled.
// Fill the form
if (!$this->request->data) {
$this->request->data = $user;
}
Use following
if(empty($this->data) )
{
if (!$this->request->data) {
$this->request->data = $user;
}
}
Instead of ur
if (!$this->request->data) {
$this->request->data = $user;
}

How can I redirect to the same url?

I'm trying to figure out how to make redirect to the same url after processing form in silex:
public function someAction(Application $app)
{
$form = ... // building form
if ('POST' === $app['request']->getMethod()) {
$form->bindRequest($app['request']);
if ($form->isValid())
{
$url = $app['url_generator']->generate(
$app['request']->get('_route'),
$app['request']->get('_route_params')
);
return $app->redirect($url);
}
}
return $app['twig']->render(
'form.html.twig',
array(
'form' => $form->createView()
)
);
}
It's possible in Symfony, but it's not working here. (Of course, i can always redirect to something like $url?success)
UPD: There's everything correct with $url. The point is that if you are trying to redirect to exactly the same url, it won't work.
The Request class has a getRequestUri() method. You can use that like this:
return $app->redirect($request->getRequestUri());
Sorry to answer your question with another question, but why would you want to redirect to the same page? The logic for your route should simply display your view after processing the form.
public function someAction(Application $app)
{
$form = ... // building form
if ('POST' === $app['request']->getMethod()) {
$form->bindRequest($app['request']);
if ($form->isValid())
{
$url = $app['url_generator']->generate(
$app['request']->get('_route'),
$app['request']->get('_route_params')
);
//return $app->redirect($url);
// just remove the return here and you're all set!
}
}
return $app['twig']->render(
'form.html.twig',
array(
'form' => $form->createView()
)
);
}

Categories