postLink generates invalid code at CakePHP 2.3 - cakephp

I am using CakePHP 2.3
I have two environments where I have my web application. At the testing environment with the exact same version of the application (all files are the same) I am having a problem with the Form->postLink method.
It shows this error on the Javascript console:
Uncaught TypeError: Object # has no method 'submit' users:119
onclick
Comparing the resulting HTML from both environments I can notice that the attributes name and id generated by this method are repeated more than once in the same page (which shouldn't be like that).
This is the code use to generate those post links:
foreach($users as $user){
$delete = $this->Form->postLink(__('Delete'), array('action' => 'delete', $user['user_id']), __('Are you sure you want to delete %s?', $user['user_id']));
}
This is the problematic generated HTML with repeated values for id and name as you can see:
<!-- link 1 -->
<form action="delete/1/" name="post_51e8019d095f1" id="post_51e8019d095f1" style="display:none;" method="post">
<input type="hidden" name="_method" value="POST"/>
</form>
Delete
<!-- link 2 -->
<form action="delete/2/" name="post_51e8019d095f1" id="post_51e8019d095f1" style="display:none;" method="post">
<input type="hidden" name="_method" value="POST"/>
</form>
Delete
Why is this happening?
Could it be related with the configuration of the web server somehow? I don't see another explanation for it...
Thanks.

The problem was caused by a bug in IIS 7.0.6000.16386 and the PHP function uniqid as pointed out here.
I am using a slightly different version in both environments ( IIS 7.0.6000.16386 vs IIS 7.5.7600.16385) and that was the cause of the problem.
In order to solve it I modified the file lib/Cake/View/Helper/FormHelper.php chaning the line $formName = uniqid('post_'); inside the postLink function to:
$formName = uniqid('post_', true);
That adds more entropy and as the documentation says:
If set to TRUE, uniqid() will add additional entropy (using the combined linear congruential generator) at the end of the return value, which increases the likelihood that the result will be unique.
Update
Ended up having to add one more change due to problems with javascript in forms.
I added one more line after $formName = uniqid('post_', true); so it looks like:
$formName = uniqid('post_', true);
$formName = str_replace('.', '', $formName);

Related

Custom error pages are not showing

I have a custom error page for 500 Server Errors in CakePHP 3.
My src\Template\Layout\error.ctp template file has been adjusted to have my default error template in it. And then I've also updated my view file at src\Template\Error\error500.ctp. I even added this line, $this->layout = 'error'; as described in the docs.
This is what my error view looks like:
<?php
use Cake\Core\Configure;
use Cake\Error\Debugger;
$this->layout = 'error';
$this->Html->addCrumb('Error');
?>
<section class="content">
<div class="error-page">
<h2 class="headline text-red">500</h2>
<div class="error-content">
<h3><i class="fa fa-warning text-red"></i> Oops! Something went wrong.</h3>
<p>
We will work on fixing that right away.
Meanwhile, you may return to dashboard or try using the search form.
</p>
</div>
</div>
</section>
However, it still shows the default CakePHP error page.
What am I missing?
Just turn off the debug in app.php like
'debug' => false
As it written in cookbook
Each framework layer exception has its own view file located in the core templates but you really don’t need to bother customizing them as they are used only during development. With debug turned off all framework layer exceptions are converted to InternalErrorException
For detailed info about ExceptionRenderer visit here

cakephp custom flash layout error

i have looked at the documentatioin of cakephp 2.5.4 about custom flash messages and some other tutorials on how to make this but its not working
i have error.ctp and success.ctp under elements folder
error.ctp
<div id="error-flash">
<?php echo h($message); ?>
</div>
success.ctp
<div id="success-flash">
<?php echo h($message); ?>
</div>
and in controller i call them like this
$this->Session->setFlash(__(' file successfully uploaded.','success',array('class'=>"flash_msg_ok")));
$this->Session->setFlash(__('Upload Failed','error',
array("class" => "flash_msg_error")));
my chrome developer tools still shows the default flash generated.
its not rendering the element or applying the specified classes
i've also tried this
$this->Session->setFlash('success','default',array('class'=> 'success'))
still nothing.
of note is that am using custom layout and css for the page.
what am i missing here?
There are a couple of issues..
First, you've accidentally put the other function variables inside the translation string. You can fix the call as follows (note closing bracket moved to end of message string):
$this->Session->setFlash(__(' file successfully uploaded.'),'success',array('class'=>"flash_msg_ok"));
$this->Session->setFlash(__('Upload Failed'),'error',
array("class" => "flash_msg_error"));
Second, you'll need to add the class to your custom error element file (the cakephp docs neglect to mention this!). For example:
<div id="error-flash" class="<?php echo $class; ?>">
<?php echo h($message); ?>
</div>
Hope that helps!

Protractor test failing in firefox (unable to find id) but works in ie/chrome

I have a Protractor test that is running successfully against both IE and chrome, but when i run it against firefox an error is returned claiming that it can not find the element on the page - any ideas?
The HTML looks as follows - once the customer types letters into the box it attempts to provide options:
<form data-ng-submit="submit()" data-ng-show="getCurrentTab()===1" class="navbar-form navbar-left" role="search" data-ng-controller="RouteTabController">
<div class="form-group">
<b>FROM</b>
<div ba-auto-complete field="from" class="autoCompleteDirective" id="fromRoute"/>
</form>
The relevant code in the test is as follows:
var actions = browser.actions();
actions.mouseMove(element(by.id('fromRoute')));
actions.click();
actions.sendKeys("ABC");
On a side note I am using that as when I tried the following it claimed the element did not have focus in both chrome and firefox:
element(by.id('fromRoute')).click();
element(by.id('fromRoute')).sendKeys('ABC');
The answer (I'm ashamed to admit) was that my chrome driver was out of date. When I updated that then firefox worked ok

AppError 404 equivalent in cake 2.0

I've recently upgraded my 1.3 cake app to 2.0, and I'm trying to redo my app_error code.
With Cake 1.3, it was simply a case of creating an app_error.php file, putting it in my app root, and overriding the built-in error404() and missingController() actions.
Here is my old 1.3 /app/app_error.php file: http://pastebin.com/beWZD9PJ
It had some code that kicked in when someone arrived at the site with a predefined 'alias' URL, and then it redirected them accordingly.
I just need this to work in Cake2.0, and I see the manual is telling me its all changed, but I can't find a specific case like this. Can anyone help me out, so the error404 code kicks in?
Many thanks
4xx and 5xx errors in CakePHP 2.0 ar now exceptions, thus you need to customize your Exception renderer or handler. Read from this section to the bottom http://book.cakephp.org/2.0/en/development/exceptions.html#exception-renderer
You can throw a 404 exception with the following code in your controller in Cake 2:
throw new NotFoundException(__('Your error text here'));
Then, you just need to have APP/View/Errors/error400.ctp present with something relevant to display. Cake includes this by default:
<h2><?php echo $name; ?></h2>
<p class="error">
<strong><?php echo __d('cake', 'Error'); ?>: </strong>
<?php printf(
__d('cake', 'The requested address %s was not found on this server.'),
"<strong>'{$url}'</strong>"
); ?>
</p>
<?php
if (Configure::read('debug') > 0 ):
echo $this->element('exception_stack_trace');
endif;
?>
It's simple.
With cakephp 2, you have to add this function in your AppController:
public function appError($error) {
$this->redirect('/');
}
Here, for example we redirect all errors to index page.

Grails AppEngine file upload using GAEVFS

I'm working on Grails web application and need to upload files.
I have a form (simplified here):
<g:form action="save" method="post" enctype="multipart/form-data">
<input type="file" id="image" name="image" />
<input class="save" type="submit" value="Create" />
</g:form>
and in Controller code (I know that this should not be in controller but this is just to make it work and than will be designed better):
def save = {
GaeVFS.setRootPath( servletContext.getRealPath( "/" ) );
FileSystemManager fsManager = GaeVFS.getManager();
FileObject tmpFolder = fsManager.resolveFile( "gae://WEB-INF/upload_files" );
if ( !tmpFolder.exists() ) {
tmpFolder.createFolder();
}
//I NEED CODE HERE TO SAVE THE IMAGE IN THE BIGTABLE VIA GAEVFS
}
So I have two problems:
A. When save create button is pressed a get exception since it tries to use Apache Commons FileUpload that tries to save to file system.
How do I disable it?
Exception is:
java.lang.NoClassDefFoundError: java.rmi.server.UID is a restricted class. Please see the Google App Engine developer's guide for more details.
at com.google.appengine.tools.development.agent.runtime.Runtime.reject(Runtime.java:51)
at org.apache.commons.fileupload.disk.DiskFileItem.(DiskFileItem.java:103)
at org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:196)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:358)
B. I need the code example to save image via gaevfs
I have seen example in GaeVfsServlet but I still don't know how exactly it should look in my case. Any kind of help is welcome.
GaeVfsServlet url: http://code.google.com/p/gaevfs/source/browse/trunk/src/com/newatlanta/appengine/servlet/GaeVfsServlet.java
I came across the very same problem and I am using a grails plugin called ajaxuploader to get around the issue - the file that gets uploaded is available to your controller as an inputstream object and doesnt have to use the commons file upload API at all.
http://www.grails.org/plugin/ajax-uploader

Resources