required_if works incorrect - laravel

I use the following validation rule:
'email' => 'required_if:phone-register,false|email|max:255|unique:users',
And my incoming data are:
array:6 [▼
"_token" => "xPGUfppYirlLmSezTQduKZV4NXUAqf3I5DhrSJTZ"
"email" => null
"phone" => "514033390"
"phone-register" => "true"
"password" => "1234567"
"password_confirmation" => "1234567"
]
Despite on I have incoming email as null, the validation for email still works. Why, if I set required_if:phone-register,false
Full code of validation:
return Validator::make($data, [
'email' => 'required_if:phone-register,"false"|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'phone-register' => 'required|boolean',
'phone' => 'required_if:phone-register,"true"|numeric|min:6|max:15'
]);
Now I have:
protected function validator(array $data)
{
$v = Validator::make($data, [
'password' => 'required|min:6|confirmed',
'phone-register' => 'required',
'phone' => 'required_unless:phone-register,true|numeric|min:6'
]);
$v->sometimes('email', 'required|email|max:255|unique:users', function ($input) {
return $input->phone-register === "false";
});
}

You can add rules conditionally:
$v = Validator::(....);
$v->sometimes('email', 'required|email|max:255|unique:users', function ($input) {
return $input->phone-register === "false";
});

Related

Create a collection given a number of times keeping the key

I'm trying to create a kind of dataset using laravel Collections, for this I'm using the "times" method:
function getValidData(int $amount): Collection
{
return Collection::times($amount, function (int $number): array {
$password = faker()->password(8, 12);
return [
"User #{$number}" => [
'name' => faker()->firstName(),
'email' => faker()->unique()->safeEmail(),
'password' => $password,
'password_confirmation' => $password
]
];
});
}
However, the result is not as I expected:
[
0 => [
'User #1' => [
'name' => 'ana',
'email' => 'ana#email.com',
'password' => '12345678',
'password_confirmation' => '12345678'
]
],
1 => [
'User #2' => [
'name' => 'leo',
'email' => 'leo#email.com',
'password' => '3231232132',
'password_confirmation' => '3231232132'
]
]
]
I was hoping it would be:
[
'User #1' => [
'name' => 'ana',
'email' => 'ana#email.com',
'password' => '12345678',
'password_confirmation' => '12345678'
],
'User #2' => [
'name' => 'leo',
'email' => 'leo#email.com',
'password' => '3231232132',
'password_confirmation' => '3231232132'
]
]
I can solve this using array_merge(...(Collection::times...)) but it seems a kinda off... Is there any way to solve this in a cleaner way using Collections?
If you would like to map the collection with new keys, you could do the following, using the mapWithKeys() function.
Example
$users = collect(range(1, $amount))
->mapWithKeys( function($index) {
$password = faker()->password(8, 12);
return [sprintf("User #%d", $index) => [
'name' => faker()->firstName(),
'email' => faker()->unique()->safeEmail(),
'password' => $password,
'password_confirmation' => $password,
]];
});
This will run from 1 to the specified $amount. Example output with 3 users:
Illuminate\Support\Collection {#2016
#items: array:3 [
"User #1" => array:4 [
"name" => "Garret"
"email" => "aurelio03#example.com"
"password" => "Vp=Et3!?w0"
"password_confirmation" => "Vp=Et3!?w0"
]
"User #2" => array:4 [
"name" => "Genoveva"
"email" => "hilda.bins#example.net"
"password" => "W?miL1Kts"
"password_confirmation" => "W?miL1Kts"
]
"User #3" => array:4 [
"name" => "Zella"
"email" => "mmurray#example.net"
"password" => "g8)"pYgjQ~"
"password_confirmation" => "g8)"pYgjQ~"
]
]
}
You could also chain the toArray() method on the collection if you prefer an Array over a Collection.

How to write validation rule for JSON laravel?

There is JSON object that I want to validate:
[{
"id": 1,
"settings": {
"GRSYSEM": 1
}
},
{
"id": 2,
"settings": {
"GRSYSEM": 1
}
},
{
"id": 3,
"settings": {
"GRSYSEM": 1
}
}
]
How to write validation rule in Laravel?
I tried this rule:
$validator = Validator::make($request->all(), [
'id' => 'required|array',
'id.*' => 'required',
'settings.*.GRSYSEM' => 'required'
]);
You are almost there, simply put the wildcard * first:
$validator = Validator::make($request->all(), [
'*.id' => 'required',
'*.settings.GRSYSEM' => 'required'
]);
It literally says: For each element in the array, I expect an id and a setting GRSYSEM.
You could also ensure it's an array by using a little hack:
$data = ['input' => $request->all()];
$validator = Validator::make($data, [
'input' => 'required|array',
'input.*.id' => 'required',
'input.*.settings.GRSYSEM' => 'required'
]);
If entry in $request->all() is id (as I can see), you should try like that :
$validator = Validator::make($request->all(), [
'id' => 'required|array',
'id.*.id' => 'required',
'id.*.settings.GRSYSEM' => 'required'
]);

Search in Laravel Datatables

In my datatables, the search function works only on the name column. The name column ist different that the others. It means, the name column gives the value of the database back, while the others gives a value back with the function add column
In the column array there are the following values:
´´´
protected function getColumns()
{
return [
'status',
'name' => [
'title' => 'Name',
'orderable' => true,
'searchable' => true
],
'location' => [
'title' => 'Standort',
],
'department' => [
'title' => 'Abteilung',
'orderable' => true,
'searchable' => true
],
'division' => [
'title' => 'Bereich',
'orderable' => true,
'searchable' => true
],
'leader' => [
'title' => 'Verantwortlicher',
'orderable' => true,
'searchable' => true
],
'start_date' => [
'title' => 'Startdatum',
'searchable'=> true,
],
'end_date' => [
'title' => 'Enddatum',
'searchable'=> true
]
];
}
```
Why it doesn't search on all columns? What i have to do?
Try to search by using this code.
return Datatables::of($tasks)
->filter(function ($query) use ($request) {
$title = $request->title;
if (isset($title) && !empty($title)) {
$query->where('title', 'like', '%'.$title.'%');
}
})->make(true);
OR
return Datatables::of($tasks)
->filterColumn('title', function($query, $value) {
$query->whereRaw("title like ?", ["%{$value}%"]);
})->make(true);

Laravel validation rules for related checkboxes

In my form I have a couple checkboxes and i want at least one of them (or both) to be checked before submitting my form. My input checkboxes are named car and bike. In my controller i am validating my request
$this->validate($request, [
'content'=> 'required',
'title' => 'required',
'car | bike' => 'required', //this is not correct.
]);
What i couldn't figure is the statement that allows me to do the validation above
return Validator::make($request, [
'firstName' => 'required|max:255',
'lastName' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'checkbox' =>'required_without_all',
]);
OR
return Validator::make($request, [
'firstName' => 'required|max:255',
'lastName' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'option' =>'accepted'
]);
OR
Validator::make(
[ 'cats' => Input::get('cats') ],
[ 'cats' => 'min:1' ]
);
Examples:
$validator = Validator::make([
'cats' => ['Boots', 'Mittens', 'Snowball']
], ['cats' => 'min: 1']);
$result = $validator->fails(); // returns false
$validator = Validator::make([
'cats' => ['Boots', 'Mittens', 'Snowball']
], ['cats' => 'min: 2']);
$result = $validator->fails(); // returns false
$validator = Validator::make([
'cats' => ['Boots', 'Mittens', 'Snowball']
], ['cats' => 'min: 4']);
$result = $validator->fails(); // returns true
OR
Refer : https://laravel.com/docs/5.1/validation#available-validation-rules

These credentials do not match our records

I have an app with laravel in which, I have different users and different roles. I insert some users, roles, role_user, permission and permission_role in my DB using seeder but, when I try to login using email and password already recorded in DB, I got : These credentials do not match our records.
this is UserTableSeeder :
public function run()
{
//
$user = [
[
'name' => 'admin',
'email' => 'admin#mail.co',
'password' => 'passwordadmin'
],
[
'name' => 'president',
'email' => 'President#mail.co',
'password' => 'passwordpresident'
],
[
'name' => 'utilisateur1',
'email' => 'utilisateur1#mail.co',
'password' => 'passworduser'
],
[
'name' => 'utilisateur2',
'email' => 'utilisateur2#mail.co',
'password' => 'passworduser'
]
];
foreach ($user as $key => $value) {
User::create($value);
}
}
RoleTableSeeder :
public function run()
{
//
$role = [
[
'name' => 'admin',
'display_name' => 'Administrateur',
'description' => 'Administrateur du système'
],
[
'name' => 'president',
'display_name' => 'Président',
'description' => 'President de la commune'
],
[
'name' => 'utilisateur_normal',
'display_name' => 'membre du conseil',
'description' => 'membre du conseil'
]
];
foreach ($role as $key => $value) {
Role::create($value);
}
}
RoleUserTableSeeder :
public function run()
{
//
DB::table( 'role_user' )->insert([
[ 'user_id' => 6, 'role_id' => 4 ],
[ 'user_id' => 7, 'role_id' => 5 ],
[ 'user_id' => 8, 'role_id' => 6 ],
[ 'user_id' => 9 , 'role_id' => 6 ],
]);
}
Models I have : User, Role, Permission.
Any idea please ?
It looks you are not hashing your passwords into your database, you need to use bycript:
public function run()
{
//
$user = [
[
'name' => 'admin',
'email' => 'admin#mail.co',
'password' => bcrypt('passwordadmin')
],
[
'name' => 'president',
'email' => 'President#mail.co',
'password' => bcrypt('passwordpresident')
],
[
'name' => 'utilisateur1',
'email' => 'utilisateur1#mail.co',
'password' => bcrypt('passworduser')
],
[
'name' => 'utilisateur2',
'email' => 'utilisateur2#mail.co',
'password' => bcrypt('passworduser')
]
];
foreach ($user as $key => $value) {
User::create($value);
}
}
You can see more seeder examples on the oficial seeder Laravel documentation.
As Troyer said, you need to hash them with bcrypt. Laravel provides a handy wrapper with \Hash::make($str).
It is extremely bad practice to have your passwords in plaintext.
You need to run your seeder like this:
public function run()
{
$users = [
[
'name' => 'admin',
'email' => 'admin#mail.co',
'password' => \Hash::make('passwordadmin') // Hash them in order to make use of \Auth library (and more importantly, in order not to be a dick to your users)
],
[
'name' => 'president',
'email' => 'President#mail.co',
'password' => \Hash::make('passwordpresident')
],
[
'name' => 'utilisateur1',
'email' => 'utilisateur1#mail.co',
'password' => \Hash::make('passworduser')
],
[
'name' => 'utilisateur2',
'email' => 'utilisateur2#mail.co',
'password' => \Hash::make('passworduser')
]
];
User::create($users); // Import them with 1 query for performance
}

Resources