In my laravel project am using js validator, but during form updation , Validation is not working. My form is not submitting, if I did not made any changes. Sometimes it shows error to the email field.
Following is my validation code,
protected $edituserValidationRules = [
'user_name' => 'required',
'email' => 'required|email|max:255|unique:users',
];
Following is the code to pass validation and other things to view
public function viewUsers($id)
{
$users = User::find($id);
$validator = JsValidator::make($this->edituserValidationRules,[],[],'#useredit');
return view('user.update')->with(['validator' => $validator,'users'=> $users]);
}
Following is the code to update that particular user
public function updateUsers(Request $request , $id)
{
$this->validate($request, [
'email' => 'required|email|max:255|unique:users' . $request->id
]);
$postdata = $request->all();
$user = new User;
$user = User::find($id);
$hashedRandomPassword = Hash::make($postdata['password'], [
'rounds' => 12
]);
$api_key = Str::random(16);
$checkApiKey = User::where('api_key', $api_key)->first();
if(!empty($checkApiKey)) {
$api_key = Str::random(16);
}
$user = new User;
$user->name=$postdata['user_name'];
$user->email=$postdata['email'];
$user->password= $hashedRandomPassword;
$user->ip_address=$request->ip();
$user->api_key=$api_key;
if($user->save()){
Session::flash('message', 'User Updated Sucessfully');
Session::flash('msgclass', 'alert-success');
}
return redirect('users');
}
Following is the code in view page
#extends('user.layout.app')
#section('content')
<script src="{{ url('js/user/location.js') }}"></script>
<div class="container-fluid add-location">
<div class="row">
<div class="col-md-12">
<div class="card">
<form method="post" action="{!! route('updateUsers', [$users->id]); !!}" name="useredit" id="useredit" enctype="multipart/form-data" novalidate>
{{ csrf_field() }}
<div class="card-header">
<h4 class="card-title"> Edit User </h4>
</div>
#if(!empty($errors->all()))
<div class="row"> #foreach ($errors->all() as $error)
<div class="col-lg-12">
<div class="alert alert-danger"> <span>{{ $error }}</span> </div>
</div>
#endforeach </div>
#endif
<div class="card-content">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<label class="control-label">Full Name
<star>*</star>
</label>
<input id="user_name" name="user_name" class="controls form-control" type="text" placeholder="User Name" value="{{$users->name}}">
#if ($errors->has('user_name')) <span class="help-block"> {{ $errors->first('user_name') }} </span> #endif </div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="control-label">Email
<star>*</star>
</label>
<input id="email" type="email" class="form-control" name="email" value="{{$users->email}}" placeholder="Email">
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<!-- <div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="control-label">Password
<star>*</star>
</label>
<input id="password" type="password" class="form-control" name="password" value="{{bcrypt($users->password)}}">
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div> -->
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-6">
<div class="col-xs-12 col-sm-12 col-md-12 form-action">
<button type="submit" class="btn btn-fill btn-info">Submit</button>
Cancel </div>
</div>
</form>
</div>
</div>
</div>
</div>
{!! $validator !!}
#endsection
What is the problem here, during adding customers, I am checking for email already exists, same is checking here also.
I don't know what is happening? Please help
try changing this line
$this->validate($request, [
'email' => 'required|email|max:255|unique:users' . $request->id
]);
to
$request->validate([
'email' => 'required|email|max:255|unique:users,email,'.$id
]);
Adding ',id' end of the statement which is used to prevent email exist check for the current id...
$request->validate([
'email' => 'required|email|max:255|unique:users,email,'.$request->id.',id',
]);
Related
I used hyn/tenancy https://tenancy.dev/docs/hyn/5.4 and created an application like this: https://www.seismicpixels.com/creating-a-laravel-saas-framework-part-6/
At the moment I'm having the problem that I'm trying to setup a registration controller for users within the tenant. I tried doing this with a second public function in the registration controller
protected function createuser(array $data)
{
print_r($data);
die();
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
The showRegistrationForm() function is:
/**
* Show the application registration form.
*
* #return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
return view('auth.registeruser');
}
The error I'm facing is (as far as i do understand), says that there's no array, which could be selected as $data . I tried using the die() like above but array $data isn't going through.
I also tried using the standard registration page which is
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Register</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('register.users') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
and here's my web.php where the registration routes are shown, but I don't think the problem is within here because i'm getting an error out of the function, so the routing works?
// Register Routes
Route::get('register', 'Auth\RegisterUserController#showRegistrationForm')->name('register.user');
Route::post('registeruser', 'Auth\RegisterController#createuser')->name('register.users');
I hope somebody can help me with this error (probably on my side 🙈)
Thanks in advance!
Try This:
protected function createuser(Request $request)
{
print_r($request->name);
die();
return User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
}
You are passing the $data as parameter but not definig it in you route:
try this hope it will help you!
Your registers.users should use Auth\RegisterController#register method. createuser is called inline and receives $request->all()
Btw, default name for the function is create(), no createuser (if you don't want to modify register method)
I am having trouble when inputting data and figuring out why my data is not being stored into the database. I have tried to use the resources route and my custom route code, but it seems still nothing works. Clicking submit just seems to refresh the page with no errors to show
And here is my form :
<div class="container">
<div class="row">
<div class="col-md-12">
<nav class="navbar navbar-expand-sm navbar-dark primary justify-content-between">
<div class="navbar-brand text-dark">Create a Story</div>
</nav>
<hr class="mt-3">
<form action="{{route('story.store')}}" method="post">
#csrf
<div class="row">
<div class="col col-lg-6">
<div class="form-group my-3">
<label for="title">Title</label><br>
<input type="text" name="title" id="title" class="w-100 form-control{{ $errors->has('title') ? ' is-invalid' : '' }}" value="{{ old('title') }}" required>
#if ($errors->has('title'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('title') }}</strong></span>
#endif
</div>
</div>
<div class="col col-lg-6">
<div class="form-group my-3">
<label for="category">Category</label><br>
<select name="category" id="category" class="w-100 form-control{{ $errors->has('category') ? ' is-invalid' : '' }}" value="{{ old('category') }}" required>
<option value="Active" selected>Select Category</option>
#foreach ($category as $item)
<option value="{{$item->id}}">{{$item->nama}}</option>
#endforeach
</select>
#if ($errors->has('category'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('category') }}</strong></span>
#endif
</div>
</div>
<div class="col col-lg-6">
<div class="form-group my-3">
<label for="thumbnail">Story Thumbnail <span class="text-muted">(Recomended size is 650 x 350 pixel)</span></label><br>
<input type="file" name="thumbnail" id="thumbnail">
#if ($errors->has('thumbnail'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('thumbnail') }}</strong></span>
#endif
</div>
</div>
<div class="col col-lg-12">
<div class="form-group mt-4">
<textarea id="text-content" name="content" class="w-100 form-control{{$errors->has('content') ? ' is-invalid' : ''}}" id="content" rows="3" value="{{old('content')}}"></textarea>
#if ($errors->has('content'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('content') }}</strong></span>
#endif
</div>
</div>
</div>
<div class="modal-footer pb-0">
<div class="float-right">
<button type="submit" class="btn btn-outline-primary btn-md">Publish Story</button>
</div>
</div>
</form>
</div>
</div>
The store function in controller :
public function store(Request $request)
{
$user = Auth::user();
$request->validate([
'title' => 'required|string',
'category' => 'required',
'thumbnail' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'content' => 'required',
]);
$thumbnailName = time().'.'.request()->thumbnail->getClientOriginalExtension();
$post = new Post;
$post->title = request('title');
$post->category_id = request('category');
$post->thumbnail = request('thumbnail');
$post->content = request('content');
$post->title = request('title');
$post->date = date('d-m-Y');
$post->author_id = $user->id;
$post->save();
$request->thumbnail->move(asset('storage/uploads'), $thumbnailName);
return redirect('me/stories')->with('Succes', 'Story has been published')->with('thumbnail', $thumbnailName);
}
And this is the routes:
Route::get('me/stories', 'PostController#index')->name('story');
Route::get('me/stories/create', 'PostController#create')->name('story.create');
Route::post('me/stories/store', 'PostController#store')->name('story.store');
Route::post('ck/image_upload', 'PostController#imageUpload')->name('ck.upload');
I'm not getting any error messages at all, it's just that the submission button does nothing. Thanks a lot!
Clicking submit just seems to refresh the page with no errors to show
Your Validator is doing a return to last page with the errors.
Add this snippet of code to your blade
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Also, inspect the page before submitting and go to the network tab (check preserve log at the top) and proceed with the submission, you will get more details of what happened.
I'm creating a file called edit.blade to edit users.
The problem is, when i'm trying to make a list with all roles, Laravel prints the following error:
Undefined variable: roles (View: /home/ubuntu/workspace/resources/views/user/edit.blade.php)
Here is my edit.blade template:
#extends ('adminlte::page')
#section('content')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<div class="container" style="margin-left:25%">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">
<h2>Editar datos del Usuario</h2>
</div>
<div class="panel-body">
<form class="form-horizontal" method="POST" action="{{ route('user.update', ['user' => $user->id]) }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT"/>
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">
Nombre
</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ $user->name }}" autofocus style=" ">
#if ($errors->has('name'))
<span class="help-block">
<strong style=" ">{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">
Email
</label>
<div class="col-md-6">
<input id="email" type="text" class="form-control" name="email" value="{{ $user->email }}" autofocus style=" ">
#if ($errors->has('email'))
<span class="help-block">
<strong style=" ">{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<label for="club" class="col-md-4 control-label">
Club
</label>
<div class="col-md-6">
<select name="id_club" >
#foreach($clubs as $club)
<option value="{{$club->id_club}}" class="form-control">{{$club->nombre}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="roles" class="col-md-4 control-label">
Rol
</label>
<div class="col-md-6">
<select name="role_id" >
#foreach($roles as $role)
<option value="{{$role->id}}" class="form-control">{{$role->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Guardar</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
And my this is my UserController that calls the blade template with the relevant variables:
public function edit(User $user)
{
$roles = Role::all();
$clubs = Club::all();
return view('user.edit', ['user' => $user], ['clubs' => $clubs], ['roles' => $roles]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\User $user
* #return \Illuminate\Http\Response
*/
public function update(Request $request, User $user)
{
$user->save();
$user->roles()->sync([$request->input('role_id')]);
//$user->update($request->all());
return redirect()->route('user.index');
}
I'm including use App\Role; at the top of this file.
Change this:
return view('user.edit', ['user' => $user], ['clubs' => $clubs], ['roles' => $roles]);
to this:
return view('user.edit', ['user' => $user, 'clubs' => $clubs, 'roles' => $roles]);
The view() accepts the data you want to send to the view as an array as a second param.
You can also use view(...)->with(compact('user', 'clubs', 'roles'))
I created a registration form using make:auth command in Laravel 5.5. Its working alright but not showing errors when validation fails. I've checked many times that code to display errors exists in template but still getting this error.
Template Location:
resources/views/mybladetemplate
Method in Controller:
protected function validator(array $data)
{
return Validator::make($data, [
'field1' => 'required|string|max:255',
'emailField' => 'required|string|email|max:255|unique:users',
'fieldPwd' => 'required|string|min:6|confirmed',
'anotherfield' => 'string'
]);
}
RegistersUsers Trait:
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
Registeration Form:
<form class="form-horizontal" method="POST" action="{{ route('register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('field1') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Field Name</label>
<div class="col-md-6">
<input id="field1" type="text" class="form-control" name="field" value="{{ old('field1') }}" required autofocus>
#if ($errors->has('field1'))
<span class="help-block">
<strong>{{ $errors->first('field1') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('emailField') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="emailField" type="email" class="form-control" name="emailField" value="{{ old('emailField') }}" required>
#if ($errors->has('emailField'))
<span class="help-block">
<strong>{{ $errors->first('emailField') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<label for="anotherfield" class="col-md-4 control-label">Another Field</label>
<div class="col-md-6">
<input id="anotherfield" type="text" class="form-control" name="anotherfield" value="{{ old('anotherfield') }}" />
</div>
</div>
<div class="form-group{{ $errors->has('fieldPwd') ? ' has-error' : '' }}">
<label for="fieldPwd" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="fieldPwd" type="password" class="form-control" name="fieldPwd" required>
#if ($errors->has('fieldPwd'))
<span class="help-block">
<strong>{{ $errors->first('fieldPwd') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
Use this simple code in your blade to show all of the errors in a ul li structure :
#if($errors->any())
#foreach ($errors->all() as $error)
<div class="alert alert-warning" dir="rtl">
<ul>
<li>{!! $error !!}</li>
</ul>
</div>
#endforeach
#endif
Please help me to add upload profile picture on laravel 5.2 registration form.
I use Auth for my registration and login, but made some modification there.
this is my register.blade.php view
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-primary">
<div class="panel-heading">Pendaftaran Akun Baru</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
{!! csrf_field() !!}
<!-- start of nip -->
<div class="form-group{{ $errors->has('nip') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Nomor Induk Pegawai</label>
<div class="col-md-6">
<input type="text" class="form-control" name="nip" value="{{ old('nip') }}">
#if ($errors->has('nip'))
<span class="help-block">
<strong>{{ $errors->first('nip') }}</strong>
</span>
#endif
</div>
</div>
<!-- end of nip -->
<!-- start of nama -->
<div class="form-group{{ $errors->has('nama') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Nama</label>
<div class="col-md-6">
<input type="text" class="form-control" name="nama" value="{{ old('nama') }}">
#if ($errors->has('nama'))
<span class="help-block">
<strong>{{ $errors->first('nama') }}</strong>
</span>
#endif
</div>
</div>
<!-- end of nama -->
<!-- start of email -->
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Alamat E-mail</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<!-- end of email -->
<!-- start of password -->
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<!-- end of password -->
<!-- start of konfirmasi -->
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Konfirmasi Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password_confirmation">
#if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
#endif
</div>
</div>
<!-- end of konfirmasi -->
<!-- start of foto -->
<div class="form-group{{ $errors->has('avatar') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">F o t o</label>
<div class="col-md-6">
<div id="kv-avatar-errors" class="center-block" style="width:800px;display:none">
</div>
<div class="kv-avatar" style="width:200px">
<input id="avatar" name="avatar" type="file" class="file-loading">
</div>
</div>
</div>
<!-- end of foto -->
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-user"></i>Daftar
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
this is my eloquent app\Daftarpegawai.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Daftarpegawai extends Authenticatable {
protected $fillable = [
'nip', 'nama', 'email', 'password', 'foto',
];
protected $hidden = [
'password', 'status', 'remember_token',
];
}
and this is my AuthController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Daftarpegawai;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/';
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
protected function validator(array $data)
{
return Validator::make($data, [
'nip' => 'required|max:20',
'nama' => 'required|max:255',
'avatar' => 'mimes:jpg',
'email' => 'required|email|max:255|unique:daftarpegawais',
'password' => 'required|confirmed|min:6',
]);
}
protected function create(array $data)
{
return Daftarpegawai::create([
'nip' => $data['nip'],
'nama' => $data['nama'],
'foto' => $data['avatar'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
I am new in learning laravel
i've tried some methods that i found here with the same problem, but nothing works for me
if($data->hasFile('avatar'))
{
$destinationPath = 'path/to/upload/the/file'
$imgName = 'yourimagename.jpg'
$data->avatar->move($destinationPath, $imgName)
$path = $destinationPath . '/' . $imgName
}
DaftarPegawai::create([
'foto' => $path
])
You can use the below code as your POST request code for uploading avatar to user profile
public function update_avatar(Request $request){
$request->validate([
'avatar' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$user = Auth::user();
$avatarName = $user->id.'_avatar'.time().'.'.request()->avatar-
>getClientOriginalExtension();
$request->avatar->storeAs('avatars',$avatarName);
$user->avatar = $avatarName;
$user->save();
return back()
->with('success','You have successfully upload image.');
}
Source : Detailed tutorial https://www.5balloons.info/upload-profile-picture-avatar-laravel-5-authentication/
If someone is still facing problem, try using the following codes! Hope it will work...
$avatar = $data['avatar'];
$extension = $avatar->getClientOriginalExtension();
$avatar_name = time() . '.' . $extension;
$avatar->move(base_path('public/images/avatars/'), $avatar_name);
return User::create([
'avatar' => $avatar_name,
]);