Yii: Same route but only GET works after confirming there's no controller filter - yii

My urlManager rules: (basically the one comes default)
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',`
My controller:
class SiteController extends Controller {
public function actionSubscribe() {
echo 'gdg';
die();
}
}
My view:
<form method="POST" action="<?php echo $this->createUrl('site/subscribe'); ?>" style="display: inline;">
<input style="margin: 0 18px 0 6px;" type="text" value="e-mail"/>
</form>
When I access it using the url http://localhost/site/subscribe directly it works, but when I type something in the text field and push my enter button to post the form it says The system is unable to find the requested action "error".
I'm very certain that it has something to do with my form. I have so far no problem using active form but for this form I don't have a model and I don't want to use form builder. Any help?

"When I access it using the url http://*/site/subscribe directly it works"
=> You execute subscribe action with GET method.
"but when I type something in the text field and push my enter button to post the form it says The system is unable to find the requested action "error"."
=> You execute subscribe action with POST method.
=> There are some errors and I think Yii try to handle error with your default configure:
return array(
......
'components'=>array(
'errorHandler'=>array(
'errorAction'=>'site/error',
),
),
);
However, Yii can't find error action with your SiteController so it throws The system is unable to find the requested action "error". You can add error action to see information about errors like:
public function actionError()
{
if($error=Yii::app()->errorHandler->error){
$this->render('error', $error);
}
}
More info: The above error variable is an array with the following fields:
code: the HTTP status code (e.g. 403, 500);
type: the error type (e.g. CHttpException, PHP Error);
message: the error message;
file: the name of the PHP script file where the error occurs;
line: the line number of the code where the error occurs;
trace: the call stack of the error;
source: the context source code where the error occurs.

Related

How to show custom notifications or error message at admin panel for prestashop 1.7.7.7?

​
Hi, 
I've been trying a lot of options to manage an exception and show an error at admin panel but nothing seems to work.
I'm at the postProcess method of a custom module. After the user sends a csv file through a form and the data is checked (everything works fine here), if an exception occurs I need to show a message, stop and redirect to the same page. 
I've tried this: 
this->get('session')->getFlashBag()->add('error',$msg);
Tools::redirectAdmin('index.php?controller='.$controller.'&token='.$token);
this: 
header("HTTP/1.0 400 Bad Request");
die(json_encode(array( 'error' => array($this->l(' Error') ))));
(that one works but shows a blank page with the message, not the message inside the admin panel) 
also this: 
$this->context->smarty->assign(array(
'token' => Tools::getAdminTokenLite('AdminModules'),
'errors' => $this->errors
));
$this->setTemplate('ExcelProcess.tpl');
and {$errors|var_dump} at the tpl displays null...
... and many other options. 
I can't find anything either about backoffice custom notifications at the PS docs, only about front custom notifications.
Any clue? 
 
Thanks a lot! 
Miguel
PostProces code: https://drive.google.com/file/d/175nhUPDlzi6T8rZjjE8Desnzq-mtYzNQ/view?usp=sharing
​Tpl code: https://drive.google.com/file/d/17EONOCJ60L4Gp_GidzvwCQwMyTrRXapF/view?usp=sharing
Adding an error in postProcess() can be achieved by setting
$this->errors[] = $this->l('My error');
or
$this->context->controller->errors[] = $this->l('My error');
during your form submission checks.
Form will be rendered with your error messages into a red box.
If you want to show an alert without reloading the page instead, you'll have to perform an AJAX call to your AdminController, get back a JSON response and render your error message as a result of the execution of the call.
See offical docs

x-editable render html error response

We have a system where customer information is editable in-line.
When someone puts in an email that already exists, I want to return the error message:
Email already exists. <a href='/find-duplicates/id'>Click here to find possible duplicates of this customer</a>
I would like the user to be able to click on the link when s/he sees the error message. The error message is very easy to send; it's rendering the html that's the problem.
Trying to display same kind of link in x-editable field error as #iateadonut.
For anyone wanting to display html in x-editable errors, assuming you have the error with html sent back from server with response status code different from 500 (400 maybe) try :
$(function() {
$('#your_field_id').editable({
error: function(response, newValue) {
if(response.status === 500) {
return 'Service unavailable. Please try later.';
} else {
var error = $.parseHTML( response.responseText )
$(".editable-error-block").html(error)
}
},
});
})
Mostly html parsing response error and injecting it inside x-editable error block.
Found in x-editable doc, options.

yii custom error pages like 404, 403, 500

I'm trying to have separate files for all my error messages. (404, 403, 500 etc) so i can have custom designs for them. If possible i don't want the header and footer to be included in my error pages too. Right now i have this, in my SiteController.php and put error404.php into my views/site/ folder
public function actionError()
{
$error = Yii::app()->errorHandler->error;
switch($error['code'])
{
case 404:
$this->render('error404', array('error' => $error));
break;
.......
.......
}
}
i was wondering if there is a better way? or if Yii has way to handle this that i'm missing.
i read this page http://www.yiiframework.com/doc/guide/1.1/en/topics.error
and it says something about putting files into /protected/views/system but i don't quite understand Yii's documentation.
As you read Yii will look for files in /protected/views/system (after looking in themes if you have any)
You do not need to write any fresh actions all you have to do is create a folder system in the views directory and create files named errorXXX.php XXX being the error code.
The default page looks like this you modify this as you wish and save it in /protected/views/system
<body>
<h1>Error <?php echo $data['code']; ?></h1>
<h2><?php echo nl2br(CHtml::encode($data['message'])); ?></h2>
<p>
The above error occurred when the Web server was processing your request.
</p>
<p>
If you think this is a server error, please contact <?php echo $data['admin']; ?>.
</p>
<p>
Thank you.
</p>
<div class="version">
<?php echo date('Y-m-d H:i:s',$data['time']) .' '. $data['version']; ?>
</div>
</body>
You will have access to following attributes in your $data array
code: the HTTP status code (e.g. 403, 500);
type: the error type (e.g. CHttpException, PHP Error);
message: the error message;
file: the name of the PHP script file where the error occurs;
line: the line number of the code where the error occurs;
trace: the call stack of the error;
source: the context source code where the error occurs.
The alternative technique is how you created an action in SiteController,however to activate this you need to change your main config file and route errors to this action:
return array(
......
'components'=>array(
'errorHandler'=>array(
'errorAction'=>'site/error',
),
),
);
If you wish to only skin your errors then this is not necessary, if you want to do more complex stuff like on error log to a DB, send a email to the admin etc then it is good to have your own action with additional logic.

can't display error page layouts in yii

I am able to render my specified error pages in my modules, but I can't get error layouts outside of my modules.
It's quite strange, in my main config I have:
'errorHandler'=>array(
'errorAction'=> '//site/error',
),
I tried to see if it is pointed to the right file by changing it to //site/contact, and see if it renders the contact page if error. And it does show.
So the path is correct, then how come it's not showing the error page? It shows the default white page with exception error. The exception page is what im expecting, but how it's formatted is not.
public function actionError()
{
$this->layout = '/layouts/main';
if($error=Yii::app()->errorHandler->error)
{
if(Yii::app()->request->isAjaxRequest)
echo $error['message'];
else
$this->render('error', array('error'=>$error));
}
}
Your user role doesn't have rights to access actionError() in controller.
In your site controller, You can see a function called public function accessRules().
Check error is added for some user role, or it's for admin only.
If error not found, then add it for user roles.
Example:
public function accessRules()
{
return array(
array('allow',
'actions' => array('index', 'view', 'error'),
'users' => array('#')
),
..
..
..
}
You need to create a errorXXX in views/system. The XXX stand for the error code, in this case most likely a 403. For more information see this page on the Yii site.

Can't change default error messages in CakePHP 2.3

In my CakePHP project, I want to display error messages but it's not working.
When I'm submitting, I want it to display an error message like: "This field is required", but by default it's showing "Please fill out this field". I changed the message, but it's not changing from the core.
My View, Model, and Controller codes are as following:
add_district.ctp:
<div class="pg_title txtLeft">Add District</div>
<?php echo $this->Form->create('Admins', array('action' => 'add_district'));?>
<table>
<tbody>
<tr>
<td><label>District Name<span class="red required">*</span></label></td>
<td><?php echo $this->Form->input('District.district_name',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px;')); ?></td>
</tr>
<tr>
<td colspan="2" align="right" style="padding-right: 113px;"><input type="reset" value="Reset"> | <?php echo $this->Form->submit('ADD', array('div' => false));?></td>
</tr>
</tbody>
</table>
<?php print $this->Form->end();?>
<div class="clear"></div>
Model : District.php
<?php
App::uses('AppModel', 'Model');
/**
* Admin Login Model
*
*/
class District extends AppModel
{
public $name='District';
public $usetables='districts';
public $validate = array(
'district_name' => array(
'rule' => 'notEmpty',
'allowEmpty' => false,
'message' => 'This field is required'));
}
?>
And my controller Code is (AdminController.php):
public function add_district()
{
$this->layout='common';
$this->District->create();
$this->District->set($this->data);
if(empty($this->data) == false)
{
if($this->District->save($this->data))
{
$this->Session->setFlash('District Added Successfully.');
$this->redirect('add_district');
}
}
else
{
$this->set('errors', $this->District->invalidFields());
}
}
That message "Please fill out this field" is from the browser itself, not from CakePHP.
So Firefox and Chrome (and possibly other browsers?) automatically check for fields marked as required, and then when you submit a form, before even sending that form data to the server, if a required field is not filled in, the browser will give you that default popup message: "Please fill out this field".
This is the browsers default behaviour, and has nothing to do with your code. If you'd like to check that your Cake error message is working OK, then try in Safari (which, as of version 6.0.2, doesn't seem to have that 'auto detect required fields' feature). In Safari, you should get your own CakePHP error message back.
You can also prevent error checking in the browser by passing the autovalidate attribute as false for the form itself - see Disable validation of HTML5 form elements
Actually CakePHP is triggering this by adding the HTML5 required="required" attribute via the FormHelper. Previous versions of CakePHP did not do this. You can prevent this by adding 'required' => false to your form input atributes array if you wish.
The problem is the browser validation.
On Chrome, at least, if input fields have been set to 'required' you are going to be frustrated. It seems to work without a problem on Safari.
Even if you add 'required'=>false from the Model, it still might not work.
What I did to get it working was on the View, I added 'required'=>'false'
e.g. I have a Users View and an add.ctp
echo $this->Form->input('fname', array('label' => 'First Name','required'=>'false'));
after that it all worked out. but I had to do it on all input fields for the cake validation to work.
In add_district.ctp write
<?php echo $this->Form->create('Admins', array('action' => 'add_district', 'novalidate' => true));?>
I think this will solve your problem.