the custom error message doesn't displaying the error,it only shows blank page instead error message
here is my controller :
public function daftar(){
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('nama','Nama','required');
$this->form_validation->set_rules('pass','Password','required');
$this->form_validation->set_rules('passconf','PasswordConf','required|matches[pass]');
if($this->form_validation->run()==FALSE){
$this->form_validation->set_message('passconf','the password doesnt match');
}
else{
redirect('lowongan/daftar_employer');
}
}
}
You need to escape the apostrophe in the word "doesn't".
$this->form_validation->set_message('passconf','the password doesn\'t match');
write a custom callback function to set your own error message
callback_custom_error
$this->form_validation->set_rules('passconf','PasswordConf','required|matches[pass]|callback_custom_error');
public function custom_error($str)
{
if($this->form_validation->run()==FALSE)
{
$this->form_validation->set_message('passconf','your custom message');
return FALSE;
}
else
{
return TRUE;
}
}
Related
I have one controller ADD. I want this controller to manipulate three pages: add_customer page, add_project page and add_post_page.In this case add_project page works perfectly. But add_customer and add_post pages have errors:
Failed to load resource: the server responded with a status of 500 (Internal Server Error).
I think the problem is in my coditions here:
public function index(){
if($this->uri->segment(3)=='add_customer'){
$this->add_customer();
}
else if($this->uri->segment(3)=='add_post'){
$this->add_post();
}
else{
$this->add_project();
}
}
This is my full controller:
class ADD extends MX_Controller {
public $mname, $tag, $tpl;
function __construct()
{
$this->mname=strtolower(get_class());
$this->tag=strtoupper($this->mname);
}
public function index(){
if($this->uri->segment(3)=='add_customer'){
$this->add_customer();
}
else if($this->uri->segment(3)=='add_post'){
$this->add_post();
}
else{
$this->add_project();
}
}
public function add_project()
{
include APPPATH."language/".LANG.".php";
$this->load->model($this->mname.'/add_project_model');
$model='add_project_model';
$this->$model->index($this->mname);
$a['IsEnabled']=$LANGUAGE['IsEnabled'];
$a['Submit']=$LANGUAGE['Submit'];
$a['Cancel']=$LANGUAGE['Cancel'];
$a['Reset']=$LANGUAGE['Reset'];
$a['Name']=$LANGUAGE['Name'];
$a['SelectCustomer']=$LANGUAGE['SelectCustomer'];
$a['Project Name']=$LANGUAGE['ProjectName'];
$a['Manager']=$LANGUAGE['Manager'];
$a['Customer']=$LANGUAGE['Customer'];
$userGROUP = $this->session->userdata('_userGROUP');
if ($userGROUP=='Administrator')
$a['AddManager']='<button type="button" class="btn btn-warning" onclick="AddNewManager()">+</button>';
else
$a['AddManager']='';
$this->tp->assign($a);
$this->tp->parse('CONTENT', $this->mname.'/add_project.tpl');
}
public function add_customer()
{
include APPPATH."language/".LANG.".php";
$userGROUP = $this->session->userdata('_userGROUP');
if($userGROUP!=='Administrator')
{
show_404('page');
exit;
}
$this->load->model($this->mname.'/add_customer_model');
$model='add_customer_model';
$this->$model->index($this->mname);
$a['IsEnabled']=$LANGUAGE['IsEnabled'];
$a['Submit']=$LANGUAGE['Submit'];
$a['Cancel']=$LANGUAGE['Cancel'];
$a['Reset']=$LANGUAGE['Reset'];
$a['Name']=$LANGUAGE['Name'];
$a['Project Name']=$LANGUAGE['CustomerName'];
$a['Customer Name']=$LANGUAGE['Customer Name'];
$this->tp->assign($a);
$this->tp->parse('CONTENT', $this->mname.'/add_customer.tpl');
}
public function add_post()
{
include APPPATH."language/".LANG.".php";
$userGROUP = $this->session->userdata('_userGROUP');
if($userGROUP=='Engineer')
{
show_404('page');
exit;
}
$this->load->model($this->mname.'/add_post_model');
$model='add_post_model';
$this->$model->index($this->mname);
$a['IsEnabled']=$LANGUAGE['IsEnabled'];
$a['Submit']=$LANGUAGE['Submit'];
$a['Cancel']=$LANGUAGE['Cancel'];
$a['Reset']=$LANGUAGE['Reset'];
$a['Activity Name']=$LANGUAGE['Activity Name'];
$a['SalaryHour']=$LANGUAGE['SalaryHour'];
$this->tp->assign($a);
$this->tp->parse('CONTENT', $this->mname.'/add_post.tpl');
}
}
How can I resolve this problem?
Just Give it a Try, Hope it works :
public function index(){
if($this->uri->segment(3)=='add_customer'){
redirect(base_url().'/Add/add_customer');
//$this->add_customer();
}
else if($this->uri->segment(3)=='add_post'){
redirect(base_url().'/Add/add_post');
//$this->add_post();
}
else{
redirect(base_url().'/Add/add_project');
//$this->add_project();
}
}
Your index function is never reached
This would reach it : http://project.dev/add
By default the following urls hit the appropriate function.
http://project.dev/add/add_customer hit public function add_customer()
http://project.dev/add/add_post hit public function add_post()
If you are getting errors check out the various error in the specific function and make sure you have error reporting turned on
I want to have a function that validate an object.
In case of a false result I want to know the reason why my object isn't valid.
This is a sample algorithm:
public bool ValidObject(MyObjectClass obj)
{
if(obj==null)
{
throw Exception("obj cannot be null");
return false;
}
if(obj.A=='x'){
throw Exception("obj cannot be x");
return false;
}
....
....
// other validations
....
...
if(obj.Students.Count()==100)
{
throw Exception("some error message");
return false;
}
return true;
}
If I call my function:
if(ValidateObject(this.Obj))
{
InsertIntoDB(this.Obj);
}
else
{
//do something
}
I will get an error if my object is invalid. But I dont't want this.
But maybe in some cases I want to know the reason why my object is invalid.
I don't want to make to functions that do the same but one of them throw exceptions. I don't think is a right approach.
So, my first thought was to make the previous function private and to create another public function:
public bool ValidObject2(MyObjectClass obj, bool withErrors=false)
{
try
{
return ValidObject(obj);
}
catch(Exception ex)
{
if(withErrors)
{
throw ex;
}
return false;
}
}
What do you think? Do you have other ideas ?
The Rails way:
Have a method save which saves to the DB, and, in case of validation errors, just holds on to them without raising an exception.
Provide a getter validation_errors which returns a list of the errors in the previous save.
Make a trivial method save_with_validation which first calls save and then raises an exception if and only if validation_errors returns a non-empty set of errors. (Depending on your language, you might be able to get away with a shorter name like save!)
In Magento 1.7, whenever I go through the checkout process as guest at the end of the onepage checkout process I get the Magento error:
Customer email is required
Having used xDebug to profile the problem the code I have in my observer which is executed on the sales_order_place_after observer I have a function called afterOrderPlaced()
public function afterOrderPlaced($observer)
{
$organisation_id = Mage::getSingleton('customer/session')->getOrganisationId(); #$organisation_id = 25679;
$this->_order = $observer->getEvent()->getOrder();
$this->_order->setOrganisationId($organisation_id)->save();
// Customer stuff
$this->_customer_id = $this->_order->getCustomerId();
$this->_customer = $this->_order->getCustomer();
// problem on the next line below #PROBLEM HERE#
$this->_customer->setOrganisationId($organisation_id)->save();
}
The issue is on the last line of the function - for some reason it doesn't seem to like the save() on the customer object. This goes into the core files in Mage\Core\Model\Resource\Transaction.php on line 161 within the save() - see below:
public function save()
{
$this->_startTransaction();
$error = false;
try {
foreach ($this->_objects as $object) {
$object->save();
}
} catch (Exception $e) {
$error = $e;
}
if ($error === false) {
try {
$this->_runCallbacks();
} catch (Exception $e) {
$error = $e; ## ERROR IS HAPPENING HERE?! ##
}
}
if ($error) {
$this->_rollbackTransaction();
throw $error;
} else {
$this->_commitTransaction();
}
return $this;
}
Can anyone indicate what my problem maybe within my observer and why it doesn't seem to like to save the custom organisation_id to the customer object?
For guest user why are you trying to save organisation id in customer profile, there will no customer profile to save because Magento doesn't create customer in customer entity for guest users. You need to add condition to your function above to resolve the problem
if(!$order->getCustomerIsGuest()){
// Customer stuff
$this->_customer_id = $this->_order->getCustomerId();
$this->_customer = $this->_order->getCustomer();
// problem on the next line below #PROBLEM HERE#
$this->_customer->setOrganisationId($organisation_id)->save();
}
Hope the above helps
Cheers
S
I am trying to make the ajax request more user friendly. It is a scenario that using a token which can be reuse in a period of time. However in time that a user has inactive for a certain time and tried to use the form, user will observe that the ajax not working but donno why. What I'm gonna do here is to display the message.
Using my testing code, doing return new CakeResponse seems to be return true in blackhole and therefore the $result is true although the User edit should not be triggered
public function beforeFilter() {
$this->Security->blackHoleCallback = 'blackhole';
}
public function blackhole($type) {
if ($this->request->is('ajax')) {
$this->log('blackhole here','debug');
return new CakeResponse(array('body'=> json_encode(array('data'=>'The request has been blackholed')),'status'=>500));
}else{
$this->log('blackhole there','debug');
return new CakeResponse(array('body'=> json_encode(array('data'=>'The request has been blackholed')),'status'=>500));
}
}
in the userapp
public function edit() {
$userId=$this->Auth->user('id');
if (empty($this->request->data)) {
$this->request->data = $this->User->read(null, $userId);
}
if ($this->request->is('ajax')) {
if($result = $this->{$this->modelClass}->edit($userId, $this->request->data)){
$this->Session->write('Auth', $this->User->read(null, $userId));
return new CakeResponse(array('body'=> json_encode(array('data'=>'saved')),'status'=>200));
}else{
return new CakeResponse(array('body'=> json_encode(array('data'=>'error')),'status'=>500));
}
}
}
Progress 1:
Unsolved, perhaps needed to use event handler or exception, though it will be somehow complicated . Still thinking while refining other plugin feature.
I have a view that design view like :
Here i impose some entry validation when click save button I want to display me error message in my expecting display region. How can it possible?
My Controller Action is :
[HttpPost]
public ActionResult Save(COA_ChartsOfAccount oCOA_ChartsOfAccount)
{
try
{
if (this.ValidateInput(oCOA_ChartsOfAccount))
{
COA_ChartsOfAccount oParent = new COA_ChartsOfAccount();
oParent = oParent.Get(oCOA_ChartsOfAccount.ParentHeadID);
if (oCOA_ChartsOfAccount.IsChild)
{
oCOA_ChartsOfAccount.ParentHeadID = oParent.AccountHeadID;
}
else
{
oCOA_ChartsOfAccount.ParentHeadID = oParent.ParentHeadID;
}
oCOA_ChartsOfAccount = oCOA_ChartsOfAccount.Save();
return RedirectToAction("RefreshList");
}
return View(oCOA_ChartsOfAccount);
}
catch (Exception ex)
{
return View(oCOA_ChartsOfAccount);
}
}
Note : I want to make common partial view for error message display. (Like exception error message, validation message, all kind of user notification message)
With your current set up
To display an error message
In your controller:
catch (Exception ex)
{
TempData["message"] = "Custom Error Messge";
return View(oCOA_ChartsOfAccount);
}
In your view:
<div style="color: red;font-weight:900;">#TempData["message"]</div>