I need to display user friendly error message in the view I am in, and wondering what will be the best solution. I can display and error page using error controller but this is not what i want to achieve. I need to handle all custom error messages in any model and display an error in the view you are in. For example:
I am in "user" controller. When creating new user, the PHP model code checks if same user name exist, if exist I want to display a message in the view or maybe have something like this in header: echo $error; which display any error message I have set to be displayed from any model if occurred.
Example error message in model:
if ($p0 > 0) {
$IsValid = false;
log::LOG_USER_ERROR("This user already exist!", $username);
exit("This user already exist! </br> ");
}
This code write the error in a log file successfully, however how do I display the error message in the same view I am in? exit() displays the message in a blank page. I need to display it as block in red in the same view and design.
exit() terminates the current script, so the code for your View is not executed.
Instead, part of your View should be an area to display messages. Then you can put the error message in a variable (probably an array of messages) that the View displays to the user in that area.
Related
The problem
I created a constraint in my SQL database to prevent duplicate entries. The initial laravel form submission works fine. I get back the correct message. When I try to throw a duplicate entry, the app, as expected, throws an error.
This is my Controller. A successful form submission does throw the correct error:
$contact->save();
return redirect('contactus.php')->with('status', 'We received your message. We will get back to you soon.');
return back()->withErrors(['Your message may be a duplicate. Did you refresh the page? We blocked that submission. If you feel this was in error, e-mail us or call us.']);
Question
How do I display that error on the HTML screen? Instead of having the page display the following?
Basically the contact form submits information into the database using laravel. When successful, it displays a success message by redirecting it. When not successful, (because of a SQL Unique constraint blocking duplicate entries) so far I've managed to make it throw a SQL error.
How do I display a custom message, like "post not successful, duplicate entry" in that case?
You can do it by using try catch and query exception:
try {
$contact->save();
return redirect('contactus.php')->with('status', 'We received your message. We will get back to you soon.');
} catch(\Illuminate\Database\QueryException $e){
$errorCode = $e->errorInfo[1];
if($errorCode == '1062'){
return back()->with('error', 'Your message may be a duplicate. Did you refresh the page? We blocked that submission. If you feel this was in error, e-mail us or call us.');
}
else{
return back()->with('error', $e->getMessage());
}
}
or another way you can find/check the data first, if already exist just send the error. example:
$contact = Contact::where('email',$request->email)->first();
if($contact)
{
return back()->with('error', 'Your message may be a duplicate. Did you refresh the page? We blocked that submission. If you feel this was in error, e-mail us or call us.');
}
dont forget to get the error on the form view using like below:
<script>
#if(session()->has('error'))
alert('{{session()->get('error')}}')
#endif
</script>
I have multiple files and input text in my form. My code is :
if(formValidationIsGood) {
if(customeCheckFileErrorIsEmpty) {
//StartProcess
}
else {
//I load my view with my array which contains the different error related to the files.
}
else {
//I load my view with the form error (set up by : $this->form_validation->set_rules('variable', 'Variable', 'required');) But if there is an error for the files I cant display it.
}
With this code I can't show the form error and the files error at the same time.
For example the user, is uploading an csv file instead of a pdf file, and he forgot to write into a text input, the form error will be displayed but not the file error and vice versa.
Obviously I am using the helper file provide by code igniter.
Since you want to display error messages for either A OR B, you can try this:
if (!formValidationIsGood || !customeCheckFileErrorIsEmpty)
{
// load the view and display whatever errors you found
}
else
{
// form validation IS good and the files error is empty
}
The if clause above will evaluate to true if either formValidationIsGood ISN'T true (the ! prefix is key) OR the customeCheckFileErrorIsEmpty ISN't true (which would mean there is an error)
On every page of the site I have a a newsletter signup box. If there is an error, it shows using the following:
<?php if($errors->newsletter->first('emailAddress')) : ?>
<span class="input-error-notification">{{$errors->newsletter->first('emailAddress');}}</span>
<?php endif; ?>
Which works fine, for the most part. However, as soon as I have a page that requires other errors I get the error Undefined property: Illuminate\Support\MessageBag::$newsletter.
In a customer registration controller I have the following:
$message = new Illuminate\Support\MessageBag;
$message->add('codeError', 'Invalid confirmationCode code. Please click the link in the email we sent you.');
return View::make('components.message')->withErrors($message);
This gives me the error. I know I am overwriting the default global MessageBag, which I don't want to do.
I also get the error if I do not create a new MessageBag.
Anybody have any ideas what can be done here? I can't find anything relating to this.
What is the best way to send your messages like error messages from one php page to other php page.
I do not want to use implode function, also i do not want messages to be displayed in address bar.
Using this code
$pageurl.= '?errors[]=' . implode('&errors[]=', array_map('urlencode', $errors));
My error messages generated by entering incorrect information by user got displayed in address bar, which is something i do not want.
Kindly help.
Use session data. It is stored on the server and kept between page loads.
Page that determines errors:
<?php
session_start():
// something happens here to cause errors
$_SESSION['my_error'] = array(
'Field 1 is incorrect',
'Field 3 is incorrect'
);
// whatever happens here to send user to next page
Page that displays errors:
<?php
session_start();
// check for set errors
if (isset($_SESSION['my_error']) && !empty($_SESSION['my_error']))
{
foreach ($_SESSION['my_error'] as $error)
{
echo $error.'<br>';
}
// unset them if not needed anymore
unset($_SESSION['my_error'];
}
I am trying to display an error message on CodeIgniter error page. I am trying this:
Controller/entries.php
public function show_entry()
{
$id = $this->uri->segment(3);
if ($id !== FALSE)
{
..
}
else
{
log_message('error', 'The post ID is missing.');
}
Shouldnt this display my error message 'The post ID is missing' on the CodeIgniter's default 404 error message ie. "The page you requested was not found."
No. The logging class is for writing messages to a log file (ideally somewhere that the user can't read it as it can have information on the inner-workings of your cite). It is something which is really beneficial to you above all.
To display a custom error message, you'll need to either use show_error or, probably more likely in this case, show_404 (both methods documented here).