I don't have any errors, nor on client or server. To give you more context I'm creating a login form, I'm trying to prevent the user from authenticate himself if the text he typed on the input fields are corrects. If it's not correct, the user should not be redirected to the route (which has the name "authenticate").
there are 3 routes: login and home which are pages, and authenticate that's redirecting to a controller which got all the functions related to the login.
AuthController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AuthController extends Controller
{
public function login() {
return view('auth.login');
}
public function authenticate(Request $request)
{
$request->validate([
'email' => 'required|email',
'password' => 'required'
]);
}
}
Login view
<form action="{{ route('authenticate') }}" method="post">
#csrf
<input type="email" name="email">
<input type="password" name="password">
<button class="submit">Log</button>
</form>
All the routes
Route::get('home', HomeController::class)
->name('home');
Route::get('login', [AuthController::class, 'login'])
->name('login');
Route::get('authenticate', [AuthController::class, 'authenticate'])
->name('authenticate');
So basically what I'm trying to do is to check if the text the user has typed into the email field is an email, and if all the fields have text in it, because everything is required. But it seems like it's not the case, even if the fields are empty the log in button redirects to the authenticate page. Did I do something wrong? If I didn't provide enough information I would be glad to provide more, thanks for reading :) have a nice day
add required field to your input tag
#if(Session::has('success'))
<div class="alert alert-success alert-dismissible">
{{Session::get('success')}}
</div>
#elseif(Session::has('failed'))
<div class="alert alert-danger alert-dismissible">
{{Session::get('failed')}}
</div>
#endif
<div class="form-group">
<label for="email" class="form-label">Email</label>
<input type="email" name="email" class="form-control" value="{{old('email')}}" required />
{!!$errors->first("email", "<span class='text-danger'>:message</span>")!!}
</div>
Try this...
Related
I have 2 register and login form. The one is used to user and others is used to admin role.
In user role, I using from laravel authentication, is good and work well.
But, the problem is when I create custom register from admin role, its can't work well.
It can't store to database, when I check using echo function, it's not print anything just refresh the page.
Could you help me, what is wrong ???
this is my route
Route::get('/adminregister', 'Auth\LoginController#formreg')->name('admin-reg');
this is my controller
namespace App\Http\Controllers\Admin\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
class AregisterController extends Controller
{
use RegistersUsers;
public function __construct()
{
// $this->middleware('guest');
}
public function create(Request $request)
{
$this->validate(request(),[
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
if ($request('confirmpassword') == $request('password')){
$user = User::create(request(['name','email' ,'password','is_admin' => True, ]));
// return redirect()->route('admin-login')->with('status', 'Successfully create account');;
}
else {
return redirect()->route('admin-reg')->with('status', 'Confirm Password not match');;
}
}
}
In this controller, fisrt I want to check the password will confirmation password then store it to database.
this is my view page
<form action ="{{ route('user-create') }}" method="POST" enctype="multipart/form-data" >
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<div class="row mb-3">
<div class="col-md-12">
<div class="form-floating mb-2 mb-md-0">
<input class="form-control" id="inputFirstName" type="text" placeholder="Enter your first name" name="name" />
<label for="inputFirstName">Name </label>
</div>
</div>
</div>
<div class="form-floating mb-3">
<input class="form-control" id="inputEmail" type="email" placeholder="name#example.com" name="email"/>
<label for="inputEmail">Email address</label>
</div>
<div class="row mb-3">
<div class="col-md-6">
<div class="form-floating mb-3 mb-md-0">
<input class="form-control" id="inputPassword" type="password" placeholder="Create a password" name="password" />
<label for="inputPassword">Password</label>
</div>
</div>
<div class="col-md-6">
<div class="form-floating mb-3 mb-md-0">
<input class="form-control" id="inputPasswordConfirm" type="password" placeholder="Confirm password" name="confirmpassword" />
<label for="inputPasswordConfirm">Confirm Password</label>
</div>
</div>
</div>
<div class="mt-4 mb-0">
<div class="d-grid"><button type="submit" class="btn btn-primary">Create account</button></div>
</div>
</form>
Do you have any suggestion of how to fix it? It cant store anything in database, and when I check it using " echo " there is nothing :)
Thank you
The confirmed rule is looking for a field name {field}_confirmation. So if you are trying to confirm the 'password' input it would be looking for a field named password_confirmation. So that input in the form needs to be changed to the name password_confirmation.
You won't need to compare the 2 password fields that are submitted since the confirmed rule has done that for you already (that is what it is for; to confirm that they match).
The Request class is not callable, $request(...). That will throw an error since it isn't callable (there is no __invoke method defined on it to make it callable).
To create the User you can get the fields you need easily with the only method:
$request->only('name', 'email', 'password')
You can add your is_admin value to the array returned from the only call:
User::create($request->only('name', 'email', 'password') + ['is_admin' => true])
You will have to make sure that the is_admin field is "fillable" on the Model.
In general you don't need to be calling request() any where in this method since you have a Request instance injected as $request already.
Also, your form isn't handling a file upload so you don't need enctype="multipart/form-data".
I have an issue with my login form where when I press the login button, the web app just redirects me to the same page. This is first time I'm making a custom login form so it's a little complex for me as I used laravel/ui in my first project. I think the problem is that the model cannot directly access the users table that's in the db. I've linked the user model to the controller but that doesn't help. If anyone can take their time to show me how to connect the login form to the users table or tell me what's wrong with my code. Here's the code that I've written so far for the login.
Login Form
<form method="post" action="/login" enctype="multipart/form-data">
#csrf
<div class="col-md-12 mt-md-0 mt-3">
<label for="email" id="email">Email</label>
<input type="email" name="email" value="{{ old('email') }}" class="form-control" placeholder="E-mail" required>
#error('email')<p class="text-danger"><small>{{$message}}</small></p> #enderror
</div>
<div class="col-md-12 mt-md-0 mt-3">
<label for="password" id="password">Password</label>
<input type="password" name="password" class="form-control" placeholder="Password"required>
#error('password')<p class="text-danger"><small>{{$message}}</small></p> #enderror
</div>
<div class="col-md-12 text-center">
<button type="submit" class="btn btn-primary mb-4 w-50 py-2 fw-bold" >Login</button>
</div>
</form>
Routes
Route::get('login', [SessionsController::class, 'create'])->middleware('guest');
Route::post('login', [SessionsController::class, 'store'])->middleware('guest');
SessionsController
public function store()
{
$attributes = request()->validate([
'email' => 'required|email',
'password' => 'required'
]);
if (! auth()->attempt($attributes)) {
throw ValidationException::withMessages([
'email' => 'Your provided credentials could not be verified.'
]);
}
session()->regenerate();
return redirect('/')->with('success', 'Welcome');
}
I think the problem is that this form is not retrieving the data from the db that's being stored when the user registers an account.
I have verified emails are sending via mailgun with a public function mail using a get request with no data on the page. Trying to send an email from a contact page with a POST request simply refreshes the page with no errors but email does not send.
I have set the .env and config.mail / config.services and successfully send mail using a get request in web.php.
First I used Terminal php artisan make:mail. Then I created two routes for a contact page (GET / POST), created a contact view (works), send the data to PagesController.php, run a validation of the three inputs, pass the data to Mail::send. On click of submit button the page refreshes with no errors (validation not working) and email doesn't send.
PagesController.php:
Namespace(
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
use App\Mail\Welcome;
use Mail;
class PagesController extends Controller
{
public function getContact(){
return view('pages/contact');
}
public function postContact(Request $request){
$this->validate($request,[
'email' => 'required|email',
'subject' => 'min:3',
'message' => 'min: 3'
]);
$data = array(
'email' => $request->email,
'subject' => $request->subject,
'bodyMessage' => $request->message
);
\Mail::send('email.Test', $data, function($message) use ($data){
$message->from($data['email']);
$message->to('visionquest.jesse#gmail.com');
$message->subject($data['subject']);
});
}
Web.php:
Route::get('contact', 'PagesController#getContact');
Route::post('contact', 'PagesController#postContact');
Test.blade.php:
<html>
<h1> Email from: {{ $email }}</h1>
<p> {{ $subject }} </p>
<br><br>
<hr>
<p>{{ $bodyMessage}}</p>
contact.blade.php:
#extends('main')
#section('title', '| Contact')
#section('content')
<div class="row">
<div class="col-md-12">
<h1>Contact Me</h1>
<hr>
<form acion="{{ url('contact') }}">
{{ csrf_field() }}
<div class="form-group">
<label name="email">Email:</label>
<input id="email" name="email" class="form-control">
</div>
<div class="form-group">
<label name="subject">Subject:</label>
<input id="subject" name="subject" class="form-control">
</div>
<div class="form-group">
<label name="message">Message:</label>
<textarea id="message" name="message" class="form- control">Type your message here...</textarea>
</div>
<input type="submit" value="Send message" class="btn btn-success">
</form>
</div>
</div>
#endsection
All I want to do is pull the information from the form and send it via email to a set address. I have been working on this for over week and now that I am not getting errors I have no idea what to do.
You can add this above the form in your blade file. You would be able to see the validation errors if any :
#if ($errors->any())
#foreach ($errors->all() as $error)
<div>{{$error}}</div>
#endforeach
#endif
Also change :
<form acion="{{ url('contact') }}">
To
<form action="{{ url('contact') }}" method="POST">
put some method on your form : method="POST" because once you will submit it, it will route to the GET route not the POST route since you did not declare a method on your form
I am getting an error in laravel 5.2
when I access it using the storeMessage function on my Controller
This is my ERROR STATEMENT:
ErrorException in 6e12af76911d23528f6e0ea587ccab13391eacf9.php line 37:
Undefined variable: thisPage (View: E:\xampp\htdocs\beasiswa\resources\views\layouts\partials\sidebar.blade.php) (View: E:\xampp\htdocs\beasiswa\resources\views\layouts\partials\sidebar.blade.php) (View: E:\xampp\htdocs\beasiswa\resources\views\layouts\partials\sidebar.blade.php)
And this is my code:
View Code
<?php $thisPage = "jenisbeasiswa"; ?>
#extends('layouts.app')
#section('htmlheader_title')
Portal Beasiswa
#endsection
#section('contentheader_title')
Broadcast Beasiswa via Telegram Messenger
#endsection
#section('main-content')
<form action="{{ url('/send-message') }}" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea name="message" id="message" class="form-control" placeholder="Enter your query" rows="10"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
#endsection
and this is my Controller Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Telegram\Bot\FileUpload\InputFile;
use Telegram\Bot\Laravel\Facades\Telegram;
class TelegramBotController extends Controller
{
public function updatedActivity()
{
$activity = Telegram::getUpdates();
dd($activity);
}
public function sendMessage()
{
return view('beasiswa.telegram');
}
public function storeMessage(Request $request)
{
$request->validate([
'email' => 'required|email',
'message' => 'required'
]);
$text = "[HEADLINE] Informasi Portal Beasiswa\n"
. "<b>Email Address: </b>\n"
. "$request->email\n"
. "<b>Message: </b>\n"
. $request->message;
Telegram::sendMessage([
'chat_id' => env('TELEGRAM_CHANNEL_ID', ''),
'parse_mode' => 'HTML',
'text' => $text
]);
return redirect()->back();
}
}
Apparently sidebar.blade.php is referring to the $thisPage variable (which is declared at the top of your view code). My guess is, you are including the sidebar in the layouts.app template. But as you extend your layouts.app the $thisPage variable will get rendered in the sidebar first before the declaration comes in your view code. And thus, it will be undefined in the Sidebar.
So, you should try another approach. Why do you even want to set the $thisPage variable? Maybe you can pass it in from the controller?
I am creating a CMS App where the user should register first.
I created a RegisterController where I define index() method which returns the view register.blade.php!
When the user clicks Register button, the request should pass to create() method alongside /register URL and create a user.
Since, I already defined that /register should open register.blade.php, then how can I run another method to create a user under the same URL?
I also don't want to use php artisan make:controller RegisterController --resource.
RegisterController.php
<?php
namespace App\Http\Controllers\Authentication;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RegisterController extends Controller
{
public function index() {
return view('auth.register');
}
protected function validator(array $data) {
return $data->validate([
'fname' => 'required|string|max:255',
'lame' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6',
]);
}
protected function create(Request $request) {
$this->validator($request->all)->validate();
}
}
register.blade.php
#extends('master')
#section('title')
Register - CMS APP
#endsection
#section('styles')
<link rel="stylesheet" href="{{ asset('css/auth.css') }}">
#endsection
#section('register')
<form class="form-signin" method="post" accept-charset="utf8" action="/register/create">
#csrf
<img class="mb-4" src="http://logo.kenh.net/logo/bootstrap-4.svg.png" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">Please Register</h1>
<label for="inputFirstName" class="sr-only">First Name</label>
<input name="fname" type="string" id="inputFirstName" class="form-control" placeholder="First Name" autofocus value="{{old('fname')}}">
#if($errors->has('fname'))
<div class="alert-danger">
<p>First Name is required</p>
</div>
#endif
<label for="inputLastName" class="sr-only">Last Name</label>
<input name="lname" type="string" id="inputLastName" class="form-control" placeholder="Last Name" autofocus value="{{old('lname')}}">
#if($errors->has('lname'))
<div class="alert-danger">
<p>Last Name is required</p>
</div>
#endif
<label for="inputEmail" class="sr-only">Email address</label>
<input name="email" type="email" id="inputEmail" class="form-control" placeholder="Email address" autofocus value="{{old('email')}}">
#if($errors->has('email'))
<div class="alert-danger">
<p>Email is required</p>
</div>
#endif
<label for="inputPassword" class="sr-only">Password</label>
<input name="password" type="password" id="inputPassword" class="form-control" placeholder="Password">
#if($errors->has('password'))
<div class="alert-danger">
<p>Password is required</p>
</div>
#endif
<button class="btn btn-lg btn-success btn-block" type="submit">Register</button>
<br>
Login
</form>
#endsection
Routes(web.php):
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('register', 'Authentication\RegisterController#index');
Route::get('/login', function() {
return view('auth.login');
})->name('login');
Why not use Laravel Auth which will build the auth database, views and logic for you?
php artisan make:auth
https://laravel.com/docs/5.6/authentication
EDIT:
Ok, so If I understand correctly, you want your post and get pointing to the same route url?
Route::match(array('GET','POST'),'/register', 'RegisterController#index');
{
//
});
You can try the routing in this form:
Route::get('/user', 'UserController#index');
This might work for you.
Also, don't forget to go through the official documentation of Laravel regarding Routing.
https://laravel.com/docs/5.6/routing
This will help you alot.
The form is sending a post request to a certain route which in this case is /register but you have not supplied that route yet.
Route::post('register', 'Authentication\RegisterController#create');
Additionally, your form action can just be action="register"