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.
Related
I'm attempting to validate a users login attempt and inform them that
Their username is wrong or
their password is wrong (because I personally hate with a blind fury when a website doesn't inform me WHICH it is but that's beside the point).
I've read a lot of SO posts on this issue but the ones I've found are years old and I'm dealing with CodeIgniter 3.0.1.
This is the code that I have in place. I'm using Eclipse PDT to as my IDE and I like it quite a bit (but that's getting off track) so I've been able to step through the execution and watch as it just fails completely.
IF (!$this->User->login( //Testing shows this works fine - the SWITCH statement gets executed as it should and the BADUSERNAME case is followed through.
addslashes(strtolower($this->input->post('username', TRUE))),
addslashes($this->input->post('password', TRUE)),
$this->getIP())){
SWITCH($this->User->ID){
CASE 'BADUSERNAME':
$this->session->set_flashdata('user_msg', 'Invalid Username');
BREAK;
CASE 'BADPASSWORD':
$this->session->set_flashdata('user_msg', 'Invalid Password');
BREAK;
CASE 'ALREADYLOGGEDIN':
$this->session->set_flashdata('user_msg', 'You are logged in elsewhere.');
BREAK;
DEFAULT:
$this->session->set_flashdata('user_msg', 'Something has gone terribly wrong. Please try logging in again.');
BREAK;
}
redirect(base_url());
}
Then a bit further down I load the header, body, and footer views - The body is where the error message should be displayed but it's not..
<div id="contentarea">
<div class="container">
<?PHP
ECHO $this->session->flashdata('show_validation') ? validation_errors() : '';
$error = $this->session->flashdata('user_msg'); //This is where it's supposed to get it...
IF ($error) //And this is where it's supposed to show it...
ECHO "<div class='error'>$error</div>";
?> //But the value is wiped so it only ever grabs NULL.
I've followed the path of execution after calling the redirect after setting the flash data and I've noticed that after the redirect finishes it's chain of execution, it calls exit;.
Then everything loads again from the index.php file, and when Session finally pops up... the value 'user_msg' is nowhere to be found.
So clearly I'm doing something wrong here - what am I doing wrong here? Will the flash_data only persist until that redirect is called? Even the session_data values (calling $this->session->value = 'some arbitrary user message' fails to persist).
How can I persist the message for the next time the body element is loaded so that it can tell the user "Hey, didn't find you" or "Hey, your password wasn't right"?
EDIT 1
So it turns out I do not need to redirect for what I am doing as POSTing (submitting the user name and password) handles that for me.
I'm going to leave the question here for anyone else who may need it answered though - perhaps the answer is simply that Flash data just doesn't survive a redirect?
Flashed data is only available for the next http request, if you reload the page a second time, data is gone.
To persist data in the session, you want to set the variable in the session.
Codeigniter
Adding Session Data
Let’s say a particular user logs into your site. Once authenticated, you could add their username and e-mail address to the session, making that data globally available to you without having to run a database query when you need it.
You can simply assign data to the $_SESSION array, as with any other variable. Or as a property of $this->session.
Alternatively, the old method of assigning it as “userdata” is also available. That however passing an array containing your new data to the set_userdata() method:
$this->session->set_userdata($array);
$this->session->set_userdata('username', 'username is wrong');
in the view
$this -> session ->userdata('username');
or
$this ->session -> username;
Reference Session Library Codeigniter.
hope this help.
All you have to do is use $this->session->keep_flashdata('user_msg') with $this->session->unset_userdata('user_msg')
here is the solution (view file)
<?php
$error = $this->session->flashdata('user_msg');
if (isset($error)) {
echo '<div class="error">' . $error . '</div>';
$this->session->unset_userdata('user_msg');
}
?>
After that in your controller construct function (In that controller where you redirecting)
public function __construct() {
parent::__construct();
//.....
$this->session->keep_flashdata('user_msg');
}
I had same problem and this works. do not forget to clear cache when try or try in different browser
You can use codeigniter's flashdata to display errors separately.
This is what I usually use.
Controller:
$errors = array();
foreach ($this->input->post() as $key => $value){
$errors[$key] = form_error($key);
};
$response['errors'] = array_filter($errors);
$this->session->set_flashdata($response['errors']);
redirect('your-page', 'refresh');
And the to display the errors use
<?php echo $this->session->flashdata('field_name'); ?>
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.
Usually I used Zend Form's messages in the following way:
Code in form:
$element = new Zend_Form_Element_Text('form_resource_type');
$validator = new Zend_Validate_NotEmpty();
$validator->setMessages(
array('isEmpty' => 'Please choose type of resource')
);
$element->addValidator($validator);
$element->setRequired(true);
$this->addElement($element);
Code in view:
<?php foreach($subForm->getElementsAndSubFormsOrdered() as $element):?>
<?php echo $element?>
<?php foreach($element->getMessages() as $errorMsg):?>
<?php echo $this->escape($errorMsg);?>
<?php endforeach;?>
<?php endforeach;?>
So, for outputting error messages I used getMessages() function. But right now under certain circumstances (in case of special combination of fields' values) I need to mark element as invalid and add custom error message. I tried to use addError($message) function, but it adds message to _errorMessages property, while getMessages output _messages Zend_Form_Element property.
I didn't find function of adding messages to the _messages property. How can I do this? Or I should not work with this property directly and change a way of outputting error messages in view?
UPD:
I use Zend Framework 1.12
Since you are accessing the error messages from the form element. Then you can try to set message in the element by using the following statement in the controller:
$form->getElement('elementName')->addErrorMessage('custom Message');
You will then be able to print the message in your way.
You can use markAsError() for marking an element as invalid Custom Error Messages
I think this will do the trick for you
if($error)
{
$element->addErrorMessage('Custom Error');
$element->markAsError();
}
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 currently have a list of users in my mysql database. One of the columns is "type". I am trying to display certain data if type is equal to admin. If type is equal to anything else, it should just echo an error message.
Unfortunately, I have tried multiple methods but it just does not seem to be working out for me. Can anyone help me get this to work properly?
This is what I have, but obviously I am doing something wrong....
<?php
$usertype = $_SESSION['type'];
if ($usertype == "admin" ){
?>
admin stuff only goes here
<?
}
else
{
echo "not priveleged usertype";
}
?>
EDIT:
The following code works when displaying via username, however, I need content displayed by usertype, not the username.
<?php
if($_SESSION['user']['username'] == "oneoftheadminusernames" )
{
?>
Each page has to start with
<?php
#session_start();
?>
otherwise, php does not "see" the sessions contents. So that's probably it.
The # prevents the php error: A session has already been started... by the way.
Now, every page that uses the session must have this directive at the top.
At least, in a quick example, that reproduces your error perfectly.
If you are saving each logged in users type field in $_SESSION['type'] variable than the code you are writing is correct. Or if you are storing type in another variable than you that variable to check.
i have an idea like add a field EnableFlag in the table. if enablee flag is set to 1 consider it as a admin else as a User;