I use pickadate.js and have a problem with the laravel validation. If I send the form with the datepicker to my application, then there are two fields. birthday and birthday_submit. The last is created by pickadate.js and contains the date in the following format: yyyy/mm/dd.
My model has the column birthday. And my validation looks the following:
$request->validate([
"first_name"=>"required_without:last_name",
"email"=>"nullable|email",
"birthday_submit"=>"nullable|date"
]);
If I pass it to the create() function (Eloquent) then I get the error, that birthday_submit doesn't exist.
Is there a way to rename birthday_submit to birthday so I can mass assign it? Also the error messages would be better because the user shouldn't get the error message that birthday_submit is invalid.
You can 'interfere' with the $request parameter by doing something like this before validation:
$request->merge(['birthday' => $request->birthday_submit]);
Basically you insert another field inside the $request with key birthday and the value of birthday_submit
Then you can use this field in the validation.
Related
I have the following problem. I am using Backpack for Laravel with the PermissionManager Extension. By default, you have to log in via your email address. However, you can change that to log in with a username. Works fine if I (for example) seed the database. But for some strange reason, I can not create new users via the PermissionManager PermissionManager Extension. If I try the following error message is the result:
SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a
default value
(SQL: insert into users (updated_at, created_at)
values (2018-11-29 22:26:32, 2018-11-29 22:26:32))
Just for testing, I gave the name column a default value, but the error message is the same, except the error now is:
Field 'username' doesn't have a default value.
It just takes the next column in my database.
Of course, I've changed the CRUD-Requests, Form-Requests and so on, to store a username instead of an email. But it looks like the Request doesn't get my form values. It only tries to store the timestamps.
Strange thing: If I reverse all I did in the Controllers and so on and create an email column again, it will just work fine... did I overlook something?
How can I solve this error and store users with a username instead of an email?
Edit:
This is even odder... The data gets posted.
Edit 2:
After taking a really close look at the error messages I found the following:
As you can see, the array with my filled in data is still here.
However, in the next instance, the parameters array is completely empty as well as the $instance array.
Step 1: In order to using username you have to edit app/Http/Controllers/Auth/LoginController and add a new method called username
public function username(){
return 'username';
}
Step 2: In User model make sure you use AuthenticatesUsers trait and add username field in fillable array.
Step 3: Make sure in app/Http/Controllers/Auth/RegisterController you add the username validation and add username field in create method.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
That's all. Hope it will solve your problem.
When this issue happened for me, I think I caused it by running php artisan vendor:publish instead of following the instructions and running php artisan vendor:publish --provider="Backpack\PermissionManager\PermissionManagerServiceProvider first.
My config/laravel-permission.php file didn't just have the permission and user models incorrect, but the entire file was spatie's version instead of Laravel Backpack's version. My guess is that spatie's vendor:publish register typically runs before Laravel Backpack PermissionManager's.
Never used this package before but I reviewed this package's files from github for solving your problem, i saw some thing in the UserCrudController.php file. The file provides default user CRUD operations. The create and update form items define via setup method in this controller. But i don't see any define for username field.
as I understand it, your user table has a username and in your model $fillable variable contains username and the user create/update form does not post username field to controller.
Can you try define username form element in to UserCrudController.php file for username, or remove username attribute from $fillable in your model.
I hope these solve your problem.
I have a question,
I have a Controller and I Have a form request to validate the update.
I want to make conditions for example. If I update an "name" the form request allow change the name but if I didn't make change use the same and don't appear "unique rule" because sometimes I have to change the name, but some times not and the problem is when I haven't to change the name because if I put the same name, I have the message "duplicated" and if I try to update another ID appear "duplicated".
I don't know if you understand but i am trying to be specific.
My rule is the next with a condition.
public function rules()
{
if (Attribute::where('name', '=', Request::get('name'))->count() == 1) {
return [
'name' => 'required'
];
} elseif (Attribute::where('name', '=', Request::get('name'))->count() != 1) {
return [
'name' => 'unique:attributes'
];
}
}
So I compare if name count is ==1 only required but when is !=1 only unique but doesn't work correctly.
Some suggestion?
Based on this Answer,
How validate unique email out of the user that is updating it in Laravel?
You can ignore the current user details and preserve the unique rule while validating the update request in such rule,
As per documentation,
Forcing A Unique Rule To Ignore A Given ID:
Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. Of course, you will want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address. You only want to throw a validation error if the user provides an e-mail address that is already used by a different user. To tell the unique rule to ignore the user's ID, you may pass the ID as the third parameter:
Your rule while updating should look like,
'name' => 'unique:attributes,name,'.$attribute->id
Here, $attribute refers to the attribute being updated.
I hope you will understand.
you can use Rule to check unique
use Illuminate\Validation\Rule;
Rule::unique('table_name')->ignore('id');
I want to create a new record in my database through the API but i get the error "message": "Array to string conversion (SQL: insert into....) from Postman
API Route:
Route::post('/posts','PostController#store');
The store function in my controller:
public function store(Request $request)
{
$post= new Post;
$post->all = $request->all();
$post->save();
}
First, check your post#all field type. If it a database field - you can set only data of the same type, for example for string type you can set only php string.
If it is not a filed but you want to set all attributes from request to model, you can do it with Model::create($request->all()).
However, before doing so, you will need to specify either a fillable
or guarded attribute on the model, as all Eloquent models protect
against mass-assignment by default.
Source - Mass Assignment
In other words to may define a property in model fillable which will be an array and contains fields that will be mass assignable (in your way - fields from request).
It might be better to use $request->only() and provide only the data you want to take out of the request, which will reduce the possibility of user errors causing you a problem, as $request->all() will include any input data, including the query string.
I want to validate a date field in my form and I also have a date field in a table and I want to check if the date in the form is after the date in the table.
Can I do this with the Validation function after:date?
I think with this function I need to define the date in the model (where I got the validation rules).
Or do I need to check it with a custom validation rule?
Thanks
You should be able to use the after:date validation if you retrieve the date to be checked against from the database table first, eg.
$date_from_table = call to retrieve date from table;
$validator = Validator::make(
array('date' => $date_from_form),
array('date' => 'after:'.$date_from_table)
);
Which as you note could go in the model if that is where you are defining the validation rules - in which case it would probably be slightly different to the above as you may not be making the validator instance in the model, just specifying the validation rules, eg.
protected $rules = array(
'date' => 'after:'.$this->date_from_table
);
If this works it probably isn't worth the bother of creating a custom validation rule unless you are going to be doing the same sort of thing in a number of different models.
I've created an Employer model in my Laravel 4 application and, in Employer.php I have created the following function to validate user input before saving it to the database:
public static function validate($input)
{
$validator = Validator::make($input, static::$rules);
if ($validator->fails() {
return $validator;
}
return true;
}
This works fine when I'm creating a new record in the database, because I am passing in values for all the rules where I have specified a particular field is required.
However, there are certain fields in the database I don't want the user to edit after they have been created (for example, business_name). On the controller's edit method I create a form and omit those fields from the form. But validation fails because business_name is required by the $rules.
As a temporary work around, I tried just creating a hidden field in the edit form and populating it with the business_name. However, this is also required to be unique and fails when I PATCH my form to the update method!
Any advice? Is there any way I can specify which validation rules should be applied depending on the method calling it? Or should I create a new method in Employer.php specifically to validate on the update method?
You could use the required_without validation rule. Since an newly instantiated model doesn't have an id field yet, you can require some fields only when id is not present. This should work:
public static $rules = array(
'business_name' => 'required_without:id'
);
http://laravel.com/docs/validation#rule-required-without