I tried following code: if the user's info to login is not correct redirect the user to the loin page again with the a message:
if (!$info_is_correct){
$_SESSION['err_msg'] = 'your login info is not correct';
return redirect('http://localhost:8000/user/login');
}
this is what I do in the login page(view) to echo the error msg:
#isset($_SESSION['err_msg'])
<div class="alert alert-danger mt-2" style="font-family:IRANSans;font-size:16px ;">
<p>
<strong>خطا !</strong>
{{$_SESSION['err_msg']}}
<?php unset($_SESSION['err_msg']) ?>
</p>
</div>
#endisset
where is the problem? and What is the best way to send data to view with redirecting?
Use the with() method:
return redirect('http://localhost:8000/user/login')->with('err_msg', 'your login info is not correct');
And if a view:
#if (session('err_msg'))
<div>{{ session('err_msg') }}</div>
#endif
You can do like this to send your error messages with use inputs
return redirect('http://localhost:8000/user/login')->with('err_msg', 'Your error message')->withInput();
Source : Laravel Redirects, Laravel Validate
Note : You can also work with Error Messages like this
Since you are using laravel the validations can be done by making use of validators and simply return
redirect()->back()->withErrors($validator)
and from the view you can have access to an $errors variable which will hold all the errors,
and you can loop through errors variable and print out the errors
from your login view
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Set session value in laravel with session method, be sure to use because it is global and set session value for whole site.
Also remove http://localhost:8000 to work this route in both development and production.
if (!$info_is_correct){
session('err_msg', 'your login info is not correct');
return redirect('/user/login');
}
And get value form session as session('key')
{{session('err_msg')}}
Related
I want to show a success message once the email is sent after redirected to same page. Here is my code :
return redirect('currentshowreport?showid='.$show_id)->with('success','Email sent successfully');
I am getting redirected to the specified page but the success message is not getting displayed. How to achieve this?
Try this:
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
I have a route post called "postContact" and when the post is success I redirect to the same contact.blade.php where place the post:
<form action="{{ route('post_contact) }}"></form>
I want to see a msg if the post have success, but the Input::get('some data') not working to me.
this is my controller resume:
public function postContact($params) {
//if successful
$msg_params = array(
'msg_type' => 'success',
'msg_text' => 'my text to show',
);
return redirect()->back()->withInput($msg_params);
}
But in the contact.blade.php this not working:
#if(isset($msg_type))
<div class="alert">{{ $msg_text }}</div>
#endif
The variables not exits...
I don´t want use flash data here, because contact.blade is a module of another external app laravel and can't share sessions.
What is happening here?
Because doing a redirect, those variables are set in session. So you may try:
#if(Session::has('msg_type'))
<div class="alert">{{ Session::get('msg_text') }}</div>
#endif
If you don't want to use session variables, then you can use route parameters. You can get the parameters using the Request facade:
#if(Request::has('msg_type'))
<div class="alert">{{ Request::get('msg_text') }}</div>
#endif
If you're redirecting back to the page, Laravel will store the data in the Session. So you need to enter this to show the data:
#if(Session::has('msg_type'))
<div class="alert">
{{ Session::get('msg_text') }}
</div>
#endif
Hope this works!
Controller
return redirect()->back()->with('success', ['your message,here']);
Blade:
#if (\Session::has('success'))
<div class="alert alert-success">
<ul>
<li>{!! \Session::get('success') !!}</li>
</ul>
</div>
#endif
I'm trying to get a success message back to my home page on laravel.
return redirect()->back()->withSuccess('IT WORKS!');
For some reason the variable $success doesn't get any value after running this code.
The code I'm using to display the succes message:
#if (!empty($success))
<h1>{{$success}}</h1>
#endif
I have added the home and newsletter page to the web middleware group in routes.php like this:
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', function () {
return view('home');
});
Route::post('/newsletter/subscribe','NewsletterController#subscribe');
});
Does anyone have any idea why this doesn't seem to work?
You should remove web middleware from routes.php. Adding web middleware manually causes session and request related problems in Laravel 5.2.27 and higher.
If it didn't help (still, keep routes.php without web middleware), you can try little bit different approach:
return redirect()->back()->with('message', 'IT WORKS!');
Displaying message if it exists:
#if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
#endif
you can use this :
return redirect()->back()->withSuccess('IT WORKS!');
and use this in your view :
#if(session('success'))
<h1>{{session('success')}}</h1>
#endif
Controller:
return redirect()->route('subscriptions.index')->withSuccess(['Success Message here!']);
Blade
#if (session()->has('success'))
<div class="alert alert-success">
#if(is_array(session('success')))
<ul>
#foreach (session('success') as $message)
<li>{{ $message }}</li>
#endforeach
</ul>
#else
{{ session('success') }}
#endif
</div>
#endif
You can always save this part as separate blade file and include it easily.
fore example:
<div class="row">
<div class="col-md-6">
#include('admin.system.success')
<div class="box box-widget">
You can simply use back() function to redirect no need to use redirect()->back() make sure you are using 5.2 or greater than 5.2 version.
You can replace your code to below code.
return back()->with('message', 'WORKS!');
In the view file replace below code.
#if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
#endif
For more detail, you can read here
back() is just a helper function. It's doing the same thing as redirect()->back()
One way to do that is sending the message in the session like this:
Controller:
return redirect()->back()->with('success', 'IT WORKS!');
View:
#if (session()->has('success'))
<h1>{{ session('success') }}</h1>
#endif
And other way to do that is just creating the session and put the text in the view directly:
Controller:
return redirect()->back()->with('success', true);
View:
#if (session()->has('success'))
<h1>IT WORKS!</h1>
#endif
You can check the full documentation here: Redirecting With Flashed Session Data
I hope it is very helpful, regards.
All of the above are correct, but try this straight one-liner:
{{session()->has('message') ? session()->get('message') : ''}}
In Controller
return redirect()->route('company')->with('update', 'Content has been updated successfully!');
In view
#if (session('update'))
<div class="alert alert-success alert-dismissable custom-success-box" style="margin: 15px;">
×
<strong> {{ session('update') }} </strong>
</div>
#endif
You can use laravel MessageBag to add our own messages to existing messages.
To use MessageBag you need to use:
use Illuminate\Support\MessageBag;
In the controller:
MessageBag $message_bag
$message_bag->add('message', trans('auth.confirmation-success'));
return redirect('login')->withSuccess($message_bag);
Hope it will help some one.
Adi
in Controller:
`return redirect()->route('car.index')->withSuccess('Bein ajoute')`;
In view
#if(Session::get('success'))
<div class="alert alert-success">
{{session::get('success')}}
</div>
#endif
I'm creating a plugin for October CMS that has a frontend form (component) with
{{ form_open({ request: 'onSubmitForm' }) }}
In the plugin there is a function onSubmitForm() with a validator.
If the validator fails I want to redirect to the page the form input came from ($this->page->url) but send the validator messages ($validator->messages()) and the original input of the form (post()) with it.
I've tried:
if ($validator->fails()) {
return Redirect::to($this->page->url)->withErrors($validator->messages())->withInput(post());
}
and if I put {{ errors }} on the page i do get a message
Object of class Illuminate\Support\ViewErrorBag could not be converted
to string
which I then fixed by using:
{% for error in errors.all() %}
<li>{{ error }}</li>
{% endfor %}
and {{ errors.first('name') }}
but the {{ input }} doesn't even return an error.
Am I doing the redirecting wrong? Or does it have to do with how Twig and Blade are so completely different? Is there a way to prefill old input values and error messages?
You can output the old input value with this twig
{{form_value('my_input_name')}}
october cms form doc
As you can read in the very top you can "translate" php exemple to twig by changing "method name" to lower case and "::" to _
exemple:
// PHP
<?= Form::open(..) ?>
// Twig
{{ form_open(...) }}
hope it will help.
this is my regular code for blade
#if (count($errors) > 0)
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>{{trans('messages.sorry')}}</strong>
<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Instead of redirecting to the same page where the form was submitted from, just use Ajax! You can do in couple ways.
instead of form_open, use form_ajax.
Here is an example:
form_ajax('onSubmitForm', { model: user, class: 'whateverclass', id: 'idoftheform', success:'doSomethingInJS(textStatus, data)'})
in PHP, you can return your error message as
return ["ErrorMsg" => 'this is some error']
Then in JS you can display it:
function whateverClass (textStatus, data){
alert (data.ErrorMsg);
$('#alertHolderDiv').html(data.ErrorMsg);
}
another way would be to use a partial! And once you throw an error you can use "update" in ajax command to update the partial. The partial will be overwritten with the new message.
I have a form. When user submits the form and gets error, I show it like this:
Register Controller
return View::make('theme-admin.user_add')->with('error_msg', validation->errors->first());
register.blade.php
#if($error_msg !== null)
<div class="alert red hideit max">
<div class="left">
<span class="red-icon"></span>
<span class="alert-text">{{ $error_msg }}</span> <-- Error message is visible here.
</div>
<div class="right">
<a class="close-red"></a>
</div>
</div>
#endif
//
Actual HTML Form
//
However, I want to move that error div into a blade file. (error.blade.php) and I want to call it with parameters when there is an error.
It will look like this.
NEW register.blade.php
{{ MESSAGE_CONTENT }} //This should be replaced with error.blade.php dynamically
//
Actual HTML Form
//
MESSAGE_CONTENT will be included via error.blade.php
error.blade.php
<div class="alert red hideit max">
<div class="left">
<span class="red-icon"></span>
<span class="alert-text">{{ $message }}</span> <-- Error message is visible here.
</div>
<div class="right">
<a class="close-red"></a>
</div>
</div>
Let's say form failed and I got some errors. I will load error.blade.php so messages will get RED background etc.
Something like this;
return View::make('theme-admin.user_add')->with(message_content', (Load error.blade.php here))->with('message', $validation->errors->first();
If the form succeeds, I'll just load success.blade.php in messages area and messages will look with GREEN background.
return View::make('theme-admin.user_add')->with(message_content', (Load success.blade.php here))->with('message', 'You successfully registered');
You probably got the logic.
How can I do this?
Ps. Image example: http://i.imgur.com/QExAiuA.png
A clean solution may be to have a simple alert object with a type and msg.
//in controller
$alert->type = 'error'; // or 'success'
$alert->class = 'red'; // or 'green'
$alert->msg = $validation->errors->first(); // or 'You successfully registered'
return View::make('theme-admin.user_add')->with('alert', $alert);
//register.blade.php
#include('alert')
//Actual HTML Form
//alert.blade.php
#if(isset($alert))
<div class="alert {{$alert->class}} hideit max">
<div class="left">
<span class="red-icon"></span>
<span class="alert-text">{{ $alert->msg }}</span>
</div>
<div class="right">
<a class="close-{{$alert->class}}"></a>
</div>
</div>
#endif
You should create your view (View::make()) in a route defined for GET, and then handle your form input in your POST route:
//routes.php
Route::get('admin/adduser', array('as' => 'adduser', 'do' => function()
{
return View::make('theme-admin.user_add');
}));
//route for handling form input
Route::post('register', array('before' => 'csrf', function()
{
$rules = array(
//your vailidation rules here..
);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails())
{
//re-populate form if invalid values, so add flashing input by with_input()
//also pass errors by using with_errors
return Redirect::to('adduser')->with_input()->with_errors($validation);
}
else {
//use the named route we defined in Route:get above
return Redirect:to('adduser')->with('message', 'Successfully added the new user!');
}
}));
There is no need to create new views for dislaying error and success messages. If you want to seperate your success and error into their own blade templates then you could use the following in your adduser.blade.php:
//adduser.blade.php
#if($errors->has())
#include('errormsg'); //use {{ $errors->first() }} or #foreach($errors->all() as $message) to print error message(s)
#else
#include('success'); //use {{ $message }} to print success
#endif
However you can also use sections in your view, and instead put both the success and error message inside the same view:
//feedback.blade.php
#section('error')
<em class="error">{{ $errors->first() }}</em>
#endsection
#section('success')
<em class="success">{{ $message }}</em>
#endsection
//adduser.blade.php
#include('feedback');
#if($errors->has())
#yield('error');
#else
#yield('success');
#endif
Hope that helps.
I found the solution myself. I needed Nested Views feature.
$view = View::make('home');
$view->nest('content', 'orders', array('orders' => $orders));
Refer to Laravel documentation for more information.