So, validation errors show up in my add action, but not in my edit action. Here are the snippets from my controller:
Here I get validation error messages as expected:
public function add() {
if ($this->request->is('post')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please try again.'));
}
}
$this->set('clients', $this->User->Client->find('list'));
}
But not here:
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->User->read(null, $id);
unset($this->request->data['User']['password']);
}
$this->set('user', $this->User->read());
$this->set('clients', $this->User->Client->find('list'));
}
If i recall correctly, using the read() call after a failed save will clear the validation errors.
there.. i found it http://book.cakephp.org/1.3/view/1017/Retrieving-Your-Data#read-1029
Is the setFlash('user could not be saved') message firing?
What's the ouput of debug($this->User->validationErrors) - add it after the setFlash fail message
Does your post array contain all the fields it needs to save? Do you have a required field in your $validate that you don't have in your post array? (Cake will register the error, but won't be able to display it unless you have the field in your form). Or another validate rule that is failing but you aren't displaying the field in your form?
Related
I have google OATH setup via socialite (only for within our organisation) and everything is working fine.
One thing I'd like to try and do is catch this "error" and get redirected back to our login page with a custom message telling the user that they do not belong to our organisation.
In principle this works fine, they can just hit the back button... but for fluidity and design, I'd like to catch this and redirect back to our home page.
Is this even possible? If so, how would you recommend I go about it?
public function show()
{
return view('auth.login');
}
public function redirectToProvider($driver)
{
if( ! $this->isProviderAllowed($driver) ) {
return $this->sendFailedResponse("{$driver} is not currently supported");
}
try {
return Socialite::driver($driver)->redirect();
} catch (Exception $e) {
return $this->sendFailedResponse($e->getMessage());
}
}
public function handleProviderCallback( $driver )
{
try {
$user = Socialite::driver($driver)->user();
} catch (Exception $e) {
return $this->sendFailedResponse($e->getMessage());
}
// check for email in returned user
return empty( $user->email )
? redirect()->intended('/login?failed=1')
: $this->loginOrCreateAccount($user, $driver);
}
protected function sendSuccessResponse()
{
return redirect()->intended('/');
}
protected function sendFailedResponse($msg = null)
{
return redirect()->intended('/login?failedResponse='.$msg);
}
protected function loginOrCreateAccount($providerUser, $driver)
{
// check for already has account
$user = User::where('email', $providerUser->getEmail())->first();
// if user
if( $user ) {
// update the avatar and provider that might have changed
$user->update([
'avatar' => $providerUser->avatar,
'provider' => $driver,
'provider_id' => $providerUser->id,
'access_token' => $providerUser->token
]);
} else {
return redirect()->intended('/login?noUser=1');
}
// login the user
Auth::login($user, true);
return $this->sendSuccessResponse();
}
private function isProviderAllowed($driver)
{
return in_array($driver, $this->providers) && config()->has("services.{$driver}");
}
I loose my form data, if model->save function fails. My form is based on two different models, because they are related to each other. The controller function is actionCreate.
public function actionCreate()
{
$model = new Model1();
if ($model->load(Yii::$app->request->post())) {
//try to save Model1 in the first step
$transaction = Yii::$app->db->beginTransaction();
try {
$model->save();
$new_model1_id = $model->id; //id of new inserted company
// write in model2 second step
$model2 = new Model2();
$model2->example1 = $example1;
$model2->example2 = $example2;
if ($model2->save()) {
$transaction->commit();
return $this->redirect(['index']);
} else {
$transaction->rollBack();
Yii::$app->session->setFlash('error', 'Error!');
//return $this->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);// return to last page
return $this->goBack((!empty(Yii::$app->request->referrer) ? Yii::$app->request->referrer : null));
};
} catch (Exception $e) {
$transaction->rollBack();
Yii::$app->session->setFlash('error', 'Error!');
//return $this->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);// return to last page
return $this->goBack((!empty(Yii::$app->request->referrer) ? Yii::$app->request->referrer : null));
}
}
return $this->render('create', [
'model' => $model,
]);
}
I'am trying to go back to the last page, when model saving has error.:
return $this->goBack((!empty(Yii::$app->request->referrer) ? Yii::$app->request->referrer : null));
But this is not working. How can I stay on the page respectively coming back to the page without loosing all the existing form entries?
According to the recommendation of Yupic,..Just render the page again, don't use goBack() or any redirect. Allow your code to get to line with return $this->render(...
I want to refresh the session of an user I am editing so he doesn't need to log-out to have the new details, such as a role for example.
My edit function is:
public function edit($id = null)
{
if (!$id) {
$this->Flash->error(__('User not found.'));
}
$user = $this->Users->get($id);
if ($this->request->is(['post', 'put'])) {
$this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('User has been updated.'));
return $this->redirect(['action' => 'edit/' . $id]);
}
$this->Flash->error(__('Unable to update User details.'));
}
$this->set(compact('user'));
}
How can I accomplish this? Thanks.
You can try something like this:
public function edit($id = null)
{
if (!$id) {
$this->Flash->error(__('User not found.'));
}
$user = $this->Users->get($id);
if ($this->request->is(['post', 'put'])) {
$this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('User has been updated.'));
$this->request->session()->write([Auth.YourUserVariable => $user]);
return $this->redirect(['action' => 'edit/' . $id]);
}
$this->Flash->error(__('Unable to update User details.'));
}
$this->set(compact('user'));
}
Then when your user uptade his/her profile if it save the user it will overide session, are you using Auth ok? To see details from user you can try this:
debug($this->request->session()->read('Auth'));exit;
Or something like this ;)
I create postSignIn Method and want to verified :
email, password, verifiedFlag
First there was no problem for create postSignIn Method, like :
public function postSignIn(){
if(Auth::attempt(array('email' => Input::get('email'),'password' => Input::get('password'),'verifiedFlag'=>1))){
return Redirect::route('home-view');
}
else{
return "Email/Password wrong or Your Account not verified by Admin";
}
}
But now I try to make it more user friendly by Separate Alert for
Account not Verified, and
Email/Password Wrong
and now I try to make it like this:
if(Auth::attempt(array('nim' => Input::get('nim'),'password' => Input::get('password')))){
Auth::logout();
if(Auth::attempt(array('nim' => Input::get('nim'),'password' => Input::get('password'),'verified' => 1))){
return Redirect::route('home-view');
}
else{
return "Your Account not verfied. Please wait until admin verified your account or contact your admin";
}
}
else{
return "NIM/Password wrong";
}
there was no problem, but I think I need other solution so Auth don't need to Login(Attempt) Twice
You can use the validate method. This would work:
public function postSignIn(){
if(Auth::attempt(array('email' => Input::get('email'),'password' => Input::get('password'),'verifiedFlag'=>1))){
return Redirect::route('home-view');
}
elseif(Auth::validate(array('email' => Input::get('email'),'password' => Input::get('password')))){
return "Your Account not verified by Admin";
}
else
{
return "Email/Password wrong";
}
}
Filters are the way to go. It's easy and clean to solve this problem, see my example below.
if user is inactive at any point it will logout user,
you can redirect user with Session flash message, your login code works as it is.
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::guest('login');
}
}
else
{
// If the user is not active any more, immidiately log out.
if(Auth::check() && !Auth::user()->verifiedFlag)
{
Auth::logout();
Session::flash('message','Your account is not active, please contact your administrator to active your account');
// redirect to login page
return Redirect::to('/');
}
}
});
Hello i create website in laravel but i facing one problem. The problem is that when user is not log in and user type www.test.com/notifications that time showing error like this
ErrorException (E_UNKNOWN)
Undefined variable: messages (View: /home/test/app/views/message-page.blade.php)
But i want to when user is not log in and enter www.test.com/notifications so user automatic redirect to index page. Please help me i very confuse.
I using the some code in base controller is as follows:
public function checkLoggedIn(){
if(Auth::user()->check()){
return;
}
else {
return Redirect::to("/");
}
}
You should do it this way:
public function checkLoggedIn(){
if (!Auth::check()) {
return Redirect::to("/");
}
return true;
}
However I assume you want to use this function in another controller so then you should do it this way:
$result = $this->checkLoggedIn();
if ($result !== true) {
return $result;
}
to make redirection.
But Laravel have filters so you can easily check if user is logged.
You can just use in your routes.php:
Route::group(
['before' => 'auth'],
function () {
// here you put all paths that requires user authentication
}
);
And you can adjust your filter in app/filters for example:
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::to('/');
}
}
});