I have blade:
<div class="form-group">
<label for="exampleInputEmail1">Họ và tên</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Nhập tên" name="username" value="{{old('username')}}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Nhập địa chỉ email" name="email" value="{{old('email')}}">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Mật khẩu</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Nhập mật khẩu" name="pass1">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Nhập lại mật khẩu</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Nhập lại mật khẩu" name="pass2">
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="cbadmin" value="1" {{old('cbadmin') ? "checked" : ''}} > Admin
</label>
</div>
I want set value variable $lv =1 while checkbox "cbadmin" checked. But not know how to retrieve it. I want set default lv=0, lv=1 if checkbox checked.
public function getAdd(AddUserRequest $request)
{
User::create([
'name' => $request->username,
'email' => $request->email,
'password' => Hash::make($request->pass1),
'level' => ????
]);
return redirect('admin/manage-user/add')->with('success','Bạn đã thêm thành công!');
}
You could check whether cbadmin exists in the $request. If it exists, it was checked and then you could set $lv to 1.
$lv=isset($request['cbadmin'])?1:0;
Use input() with a default value for the case of cbadmin being unchecked:
User::create([
'name' => $request->username,
'email' => $request->email,
'password' => Hash::make($request->pass1),
'level' => (int)$request->input('cbadmin', 0)
);
Related
i'm learning laravel.
I have to do a simple store function to create a new post. The form appears to work correctly but the data does not reach the DB.
There's my store function:
public function store(Request $request)
{
$request->validate($this->validationRules);
$formData = $request->all();
$puzzle = new Puzzle();
$puzzle->fill($formData);
$puzzle->save();
// $newPuzzle = Puzzle::create($formData);
// return redirect()->route('puzzles.show', $newPuzzle->id);
}
My model:
class Puzzle extends Model
{
public $timestamps = false;
protected $fillable = [
'title',
'pieces',
'image',
'description',
'brand',
'price',
'available',
'quantity',
];
}
My form:
<form method="POST" action="{{route('puzzles.store')}}">
#csrf
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" value="{{ old('title') }}">
</div>
<div class="mb-3">
<label for="pieces" class="form-label">Pieces</label>
<input type="number" class="form-control" id="pieces" name="pieces" value="{{ old('pieces')}}">
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<input type="text" class="form-control" id="description" name="description" value="{{ old('description')}}">
</div>
<div class="mb-3 form-check">
<label class="form-check-label" for="available">Available</label>
<input type="checkbox" class="form-check-input" id="available" name="available" checked>
</div>
<div class="col-md-4">
<label for="quantity" class="form-label">Quantity</label>
<input type="number" min="1" step="any" class="form-control" id="quantity" name="quantity">
</div>
<div class="col-md-4">
<label for="price" class="form-label">Price</label>
<input type="number" min="3.0" step="0.1" class="form-control" id="price" name="price">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
what am I doing wrong?
Thank you
Make changes in your store function as per below
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'min:5|max:250',
'pieces' => 'numeric',
'description' => 'min:5|max:500',
'price' => 'numeric',
'available' => 'boolean',
'quantity' => 'numeric',
]);
$puzzle = Puzzle::create($request->only(
'title', 'pieces', 'description', 'available', 'quantity', 'price'
));
return redirect()->route('puzzles.show', $puzzle->id);
}
in .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_databse_name
DB_USERNAME=your_databse_user_name
DB_PASSWORD=your_databse_password
My Blade File Code :-
<div class="col-md-6">
<div class="form-group">
<label for="city">Assigned Hour<span class="text-danger">*</span></label>
<div class="dateBox">
<input type="number" name="assigned_hour[[]" class="form-control newAssignedHour" autocomplete="off" value="" required>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="city">Consumed Hour<span class="text-danger">*</span></label>
<div class="dateBox">
<input type="number" name="consumed_hours[[]" class="form-control newConsumedHour" autocomplete="off" value="" required>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group c-label datepicker-wrap">
<label for="city">Date<span class="text-danger">*</span></label>
<div class="dateBox">
<input type="text" name="date[]" class="form-control input-daterange newDate" id="dateInput" autocomplete="off" value="" required>
<span href="" class="dateCalender emp-cost-i">
<i class="fa fa-calendar"></i> </span>
</div>
</div>
</div>
my Controller :-
$valid = request()->validate([
'project_id' => 'required|int|exists:projects,project_id',
'email_id' => 'required|email',
'emp_id' => 'required|int|exists:contacts,employee_id',
'project_name' => 'required|string|exists:projects,name',
'GMWO' => 'required',
'employee_name' => 'required|string',
'newJobIds' => 'required|array',
'newJobNames' => 'required|array',
'newAssignedHours' => 'required|array',
'newConsumedHours' => 'required|array',
'newDates' => 'required|array'
]);
Can Anyone Suggest How to Retain the old Value for these fields. {{ old('field_name') }} that doesn't work for this.
thanks in Advance
Please try with the below method when validation gets failed and is redirected to form in the controller
->withInput()
For more reference see the below link
https://laravel.com/docs/9.x/responses#redirecting-with-input
I'm using ReCaptcha in my Laravel project, done it with this
tutorial.
I need to create a page where user can post his message after checking captcha.
I have created a modal dialog where user can fill in data like this :
<form class="form-horizontal" action="" method="post">
<div class="form-group error">
<label for="messageName" class="col-sm-3 control-label">Name</label>
<div class="col-sm-9">
<input type="text" class="form-control has-error" id="name" name="name" placeholder="Your name" value=""
ng-model="message.name" ng-required="true">
<span class="help-inline"
ng-show="GBM.text.$invalid && GBM.text.$touched">Required</span>
</div>
</div>
<div class="form-group error">
<label for="messageEmail" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input type="email" class="form-control has-error" id="email" name="email" placeholder="E-mail" value=""
ng-model="message.email" ng-required="true">
<span class="help-inline"
ng-show="GBM.email.$invalid && GBM.email.$touched">Required</span>
</div>
</div>
<div class="form-group error">
<label for="messageLink" class="col-sm-3 control-label">Web</label>
<div class="col-sm-9">
<input class="form-control" rows="3" class="form-control has-error" id="web" name="web" placeholder="Link for your web" value="" ng-model="message.web" ng-required="false" >
</div>
</div>
<div class="form-group error">
<label for="messageText" class="col-sm-3 control-label">Comment</label>
<div class="col-sm-9">
<textarea class="form-control" rows="3" class="form-control has-error" id="comment" name="comment" placeholder="Your comment" value="" ng-model="message.text" ng-required="true" ></textarea>
<span class="help-inline"
ng-show="GBM.text.$invalid && GBM.text.$touched">Required</span>
</div>
</div>
{!! csrf_field() !!}
<!-- recaptcha -->
{{Request::is('contactd')}}
<div class="form-group">
<div class="col-md-9">
<div class="g-recaptcha" data-sitekey="{{env('GOOGLE_RECAPTCHA_KEY')}}"></div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-9 control-label"></label>
<div class="col-md-9">
<button type="submit" name="send" class="btn btn-primary btn-lg btn-block">Add new message <span class="fa fa-paper-plane-o"></span></button>
</div>
</div>
</form>
For a route I got it like this:Route::post('contact','ContactController#store');
And here is the problem, in my controller i got this code to verify captcha:
public function store(ReCaptchataTestFormRequest $request){
return "Captcha done right! ";}
And this code to save data to database
public function store(Request $request)
{
$this->validate($request, [ 'name' => 'required|max:255' ]);
$this->validate($request, [ 'email' => 'required | email' ]);
$this->validate($request, [ 'comment' => 'required' ]);
$ip = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER['HTTP_USER_AGENT'];
$guestbook = Guest_books::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'web' => $request->input('web'),
'comment' => $request->input('comment'),
'ip' => $ip,
'browser' => $browser
]);
return $guestbook;
}
So the question is: What to write in Controller for project to verify Captcha and then post it to database?
The tutorial you've followed teaches you how to create a custom validation rule which you can then use when validating requests, either through a Form Request or directly in your controller.
The mistake you've made in your controller is that you've called validate multiple times, instead you should pass it an array containing all of your rules, including your recaptcha rule, e.g:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:255',
'email' => 'required|email',
'comment' => 'required',
'g-recaptcha-response' => 'required|recaptcha',
]);
// ...
}
Additionally, you should note that the store method should always return a redirect.
want to produce data exclusive to some field in database, so i add them in my view create . but there is an error cannot be null. even though the field is nullable.
this my view form.blade.php
<form action="" method="POST">
#csrf
<div class="box-body">
<div class="form-group">
<label for="">Project *</label>
<select class="form-control select2" style="width: 100%;" name="project_id">
<option>Select One</option>
#foreach($projects as $id => $project)
<option value="{{$id}}">{{$project}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Story # Meeting Name *</label>
<input type="text" class="form-control" name="user_story">
</div>
<div class="form-group">
<label for="">Category *</label>
<select class="form-control select2" style="width: 100%;" name="task_category">
<option>Select One</option>
#foreach($task_categories as $task_category)
<option value="{{$task_category->task_category}}">{{$task_category->task_category}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Estimated *</label>
<input type="text" class="form-control" name="estimated_time">
</div>
</div>
<div class="box-footer">
<a href="">
<button type="submit" class="btn btn-primary col-md-12" style="border-radius : 0px;">SAVE</button>
</a>
</div>
</form>
this my request
public function rules()
{
return [
'project_id',
'task_category',
'estimated_time',
'spent_time' => 'nullable',
'user_story' => 'nullable',
'story/meeting_name' => 'nullable',
'assign' => 'nullable',
'percentage' => 'nullable',
'lateness' => 'nullable',
'index' => 'nullable',
];
}
this my controller
public function store(OngoingRequest $request)
{
// dd($request->all());
$spentime = SpentTime::create([
'project_id' => request('project_id'),
'user_story' => request ('user_story'),
'task_category' => request('task_category'),
'estimated_time' => request('estimated_time'),
'spent_time' => request('spent_time'),
'assign' => request('assign'),
'story/meeting_name' => request('story/meeting_name'),
'percentage'=> request('percentage'),
'lateness' => request('lateness'),
'index'=> request('index'),
]);
return redirect()->route('plan.index');
}
and the error :
error
fields that i don't want to fill and not in the form must be filled. but im not need that.
set spent_time filed default value other wise set a nullable value
$table->string('spent_time')->nullable();
$table->boolean('spent_time')->default(0);
I have created a register form in wordpress and doing register in ajax.
The form is like this
<form id="user-register-form" method="" action="" novalidate="novalidate">
<div class="full-column-width">
<div class="half-column-width">
<input type="text" name="firstname" placeholder="First Name" id="firstname" required="" aria-required="true">
</div>
<div class="half-column-width">
<input type="text" name="lastname" placeholder="Last Name" id="lastname" required="" aria-required="true">
</div>
</div>
<div class="full-column-width">
<input type="email" name="email" placeholder="E-mail" id="email" required="" aria-required="true">
</div>
<div class="full-column-width">
<input type="number" name="phone" placeholder="Phone" id="phone" required="" aria-required="true">
</div>
<div class="full-column-width">
<input type="password" name="password" placeholder="Password" id="password" required="" aria-required="true">
</div>
<div class="full-column-width">
<input class="submit" type="submit" name="submit" id="user_get_register" value="Register">
</div>
</form>
in the functions file I have made my function like this
function user_register() {
$data = $_POST['data'];
parse_str($data, $arr);
$userdata = array (
'user_login' => $arr['email'],
'firstname' => $arr['firstname'],
'lastname' => $arr['lastname'],
'email' => $arr['email'],
'phone' => $arr['phone'],
'password' => $arr['password'],
);
//check username exists
if( username_exists( $arr['email'] ) ) {
echo 'username exists';
}
else {
$id = wp_insert_user($userdata);
if( !is_wp_error( $id )) {
wp_set_current_user($id);
wp_set_auth_cookie($id);
echo 'user created';
}
}
exit;
}
add_action('wp_ajax_nopriv_user_register', 'user_register');
It is doing register the user with its details but when I am doing the same details for login from the wordpress admin login its not working. I have checked the database. The values are there. Everytime when I am doing login to the dashboard it is showing
ERROR: The password you entered for the username test#name.com is incorrect.
Can someone tell me why this error? and how to solve this?
you have a mistake in user data, change password to user_pass
$userdata = array (
'user_login' => $arr['email'],
'firstname' => $arr['firstname'],
'lastname' => $arr['lastname'],
//'email' => $arr['email'], ---- cant store this with this function
//'phone' => $arr['phone'], --- cant store this with this function
'user_pass' => $arr['password'],
);
Also you are passing in keys that dont exist for wp_insert_user(), you need to save these as usermeta =
update_user_meta( $id, '_telephone', '555-555' );