Laravel 9 Field doesn't have a default value - laravel

In my project, I got many forms, so I've decided to specify each one with an iscription field, for exemple: Kids' form => <input = 'hidden' name = 'inscripted_in' value = 'kids'>. I want to set each one with a default value, but whenever I sign in, I get this error message:
SQLSTATE[HY000]: General error: 1364 Field 'inscripted_at' doesn't have a default value
Although when I go to Laravel Debug, I still get the inserted constant value, what's the problem?
This is one of my forms
<div class="InputBox">
<input type="hidden" name="inscripted_at" value="Adults">
<input type="hidden" name="status" value="pending">
</div>
my controller:
public function store(Request $req)
{
$this->validate($req,[
'name' => 'required|max:120',
'surname' => 'required|max:120',
'job' => 'required|max:120',
'day' => 'required',
'month' => 'required',
'year' => 'required',
'hobby' => 'required|max:120',
'help' => 'required|max:120',
'place' => 'required|max:120',
'residence' => 'required|max:120',
'email' => 'required|email|unique:users',
'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'scholar_year' => 'required|max:120',
'tel' => 'required|regex:/(05)[0-9]{8}/',
]);
Chababounauser::create($req->all());
return redirect()->route('chababounausers.index')
->with('success','chababouna User inserted successfully.');
}

Check the fillable property of the Chababounauser model.
P.S.: Please, don't put spaces in HTML between attribute name and it's value.

Isn't 'inscripted_at' a default column in the database giving the date of inscription ? If so, you can't default a value to that kind of output.

Related

October CMS - required checkbox

How could I validate if the checkbox is ticked?
public $rules = [
'name' => 'required',
'email' => 'required|email',
'phone' => 'required',
'terms' => 'required',
];
'terms' is the name of the checkbox. Now I get the alert about required field even if it is checked.
HTML:
<input type="checkbox" name="terms" value="1" id="terms"/>
Simple HTML5 attribute required is also not working.
Wouter Van Damme has it right. You simply need to use the trait listed in that documentation. like so:
public $rules = [
'name' => 'required',
'email' => 'required|email',
'phone' => 'required',
'terms' => 'required|accepted',
];
here is an october specific link although the answers are the same.http://octobercms.com/docs/services/validation#rule-accepted

laravel password confirm doesn't work

I made sign up form, but there some are problems with passwords. i enter exactly the same passwords in password field and confirm password field but it fails
The password confirmation confirmation does not match.
my sign up form
<div class="form-group">
{!! Form::password('password', ['class' => 'form-control','placeholder' => '*Password']) !!}
</div>
<div class="form-group">
{!! Form::password('password_confirmation', ['class' => 'form-control', 'placeholder' => '*Password Confirm']) !!}
</div>
my validations
'password' => 'required|between:8,255',
'password_confirmation' => 'confirmed',
i also try this
'password' => 'required|between:8,255',
'password_confirmation' => 'required|between:8,255|confirmed',
and this
'password' => 'required|between:8,255|confirmed',
'password_confirmation' => 'required|between:8,255|confirmed',
but still doesn't work
This is best approach for password confirmation:
'password' => 'required|between:8,255|confirmed'
Explain:
confirmed: The field under validation must have a matching field of foo_confirmation.
For example,
password field named: password
password confirmation field would be: password_confirmation
Then the confirmed property will add default check for password confirmation.
Try
'password' => 'required|between:8,255|confirmed'
Note the confirmed is added to password validation, you dont have to add the rules twice because a confirm field needs to have the same fields.
Your validation should be like :
'password' => 'required | confirmed ',
'password_confirmation' => 'required ',
For more details See Here
I am using Laravel 5.2
and I am able to work this out using
'password' => 'required|min:8|same:confirm_password'
Validator::extend('old_password', function ($attribute, $value, $parameters, $validator) {
return Hash::check($value, current($parameters));
},
'Current password is wrong!'
);
$request->validate([
'old_password' => 'required|old_password:' . $user->password,
'password' => 'required|confirmed|min:6',
'password_confirmation' => 'required|same:password'
]);
confirmed must be passed into password validation rule:
'password' => 'required|between:8,255|confirmed',
'password_confirmation' => 'required',
$validator_password = validator::make($request->all(), [
'old_password' => ['required', 'string', 'min:8', 'old_password:' . $user->password],
'new_password' => 'required|confirmed|min:6',
'password_confirmation' => 'required|same:new_password'
]);

How to apply a validation rule to every element present within an array?

How to apply validation rules to every item within an items[] array? For example:
...->validate($request, [
'items[]' => 'required' // <-- what is the correct syntax?
]);
Try something like this
$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users',
'person.*.first_name' => 'required_with:person.*.last_name',
]);
Where person is the name of the input field and email is the key
Laravel 5.2 has an array validation all you need to do is :
In your view assuming that you have an inputs like this :
<input type="text" name="example[]" />
<input type="text" name="example[]" />
The [] are the key for this :)
And in your controller you can just do :
$this->validate($request, [
'example.*' => 'required|email'
]);
$this->validate($request, [
'items' => 'required|array',
'items.*.title' => 'required',
]);

Solving Method Not Allowed Http Exception in Laravel 5.2

I want some values in my table editable, so I created this simple custom form. But this will throw error of method not allowed http exception.Any help?
<form action="{{ url('/idx-test/update-this-student/'. $student->id)}}" class="" method="POST">//changing this to put, patch does not solve the error
Route
Route::post('/idx-test/update-this-student/{id}', 'StudentController#updateThisStudent'); //again changing this to patch,or put does not help
Controller
public function updateThisStudent(StudentRequest $request, $id)
{
$student = Student::findOrFail($id);
$student->update($request->all());
// return redirect('city');
echo "updated";
}
StudentRequest
public function rules()
{
return [
'firstname' => 'required|alpha|min:2|max:10',
'lastname' => 'required|alpha|min:2|max:10',
'bday' => 'required|date',
'address' => 'required|min:10',
'zip' => 'required|min:4|max:10',
'phone' => 'required|digits:7',
'mobile' => 'required|digits:11',
'email' => 'required|email',
'city_id' => 'required',
'yearlevel_id' => 'required',
'section_id' => 'required',
];
}
By adding this small piece of code
<input type="hidden" name="_method" value="PATCH">
My problem solve

Laravel Spark - Adding additional fields to registration form, but when empty no errors are returned

I'm building my first app with Laravel 5.2 & Laravel Spark. The front end is built using Vue.js I believe and despite adding the following to register-common-form.blade.php:
<!-- Username -->
<div class="form-group" :class="{'has-error': registerForm.errors.has('username')}">
<label class="col-md-4 control-label">Username</label>
<div class="col-md-6">
<input type="name" class="form-control" name="username" v-model="registerForm.username" autofocus>
<span class="help-block" v-show="registerForm.errors.has('username')">
#{{ registerForm.errors.get('username') }}
</span>
</div>
</div>
I can't actually see a way to fully register that extra field so that it is picked up for error handling. I've got it so that the UserRepository handles the field and inserts it, but just can't get the front end errors to show properly.
Is anyone able to help with this at all?
Okay I finally stumbled across it :D
In Laravel\Spark\Interactions\Auth\CreateUser.php there is a $rules method like so:
public function rules($request)
{
return [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'vat_id' => 'max:50|vat_id',
'terms' => 'required|accepted',
];
}
All I have done is add my username field, and it works brilliantly!
public function rules($request)
{
return [
'name' => 'required|max:255',
'username' => 'required|unique:users',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'vat_id' => 'max:50|vat_id',
'terms' => 'required|accepted',
];
}
Above answer is just for validation rules you also need to navigate to spark\src\Repositories\UserRepository.php and add 'username' => $data['username'], to the create() method like this:
public function create(array $data)
{
$user = Spark::user();
$user->forceFill([
'name' => $data['name'],
'username' => $data['username'], // ADDED THIS
'email' => $data['email'],
'password' => bcrypt($data['password']),
'confirmation_code' => str_random(30),
'last_read_announcements_at' => Carbon::now(),
'trial_ends_at' => Carbon::now()->addDays(Spark::trialDays()),
])->save();
return $user;
}

Resources