Validation of array in laravel - laravel

I am building a small application in Laravel 5.6 where I am having an api which takes an array in format [1,2,5,90,25] I want to validate as required field in my validation rule.
I tried creating a request and validating the same as:
public function rules()
{
return [
'ProjectType.*'=> 'required',
]
}
public function messages()
{
return [
'projectType.*.required' => 'Project type is required',
];
}
But this thing is not working out, even if an empty array [] is being passed it accepts it.
How can we achieve these kind of array format

You must validate at the top level of the array, you may want this validation:
public function rules()
{
return [
'ProjectType'=> 'required|array',
'ProjectType.*'=> 'required',
]
}
public function messages()
{
return [
'projectType.*.required' => 'Project type is required',
];
}

Related

Laravel only ignore field if user id is present

If an user is logged in I need to ignore if the email field already exists in the users table. But if the user is not logged in the rule should be applied.
Naturally I only have the user id if the user is logged in. So what is your alternative to this?
public function rules(): array
{
return [
'email' => ['required', 'email:filter', Rule::unique('users')->ignore(Auth::user()->id)],
];
}
You can define the rules as a variable, and append the Rule if the Auth::user() is present:
public function rules(): array {
$rules = [
'email' => [
'required',
'email:filter'
]
];
if (Auth::user()) {
$rules['email'][] = Rule::unique('users')->ignore(Auth::user()->id);
}
return $rules;
}
However, I would expect that you still want emails to be unique within the users table of your Database, regardless if a User is logged in or not, in which case you can use a ternary:
public function rules(): array {
return [
'email' => [
'required',
'email:filter',
Auth::user() ? Rule::unique('users')->ignore(Auth::user()->id) : Rule::unique('users')
]
];
Use whatever approach works best for your scenario.

Find data before validate form request laravel

I want to update the data using the request form validation with a unique email role, everything works normally.
Assume I have 3 data from id 1-3 with url:
127.0.0.1:8000/api/user/update/3
Controller:
use App\Http\Requests\Simak\User\Update;
...
public function update(Update $request, $id)
{
try {
// UPDATE DATA
return resp(200, trans('general.message.200'), true);
} catch (\Exception $e) {
// Ambil error
return $e;
}
}
FormRequest "Update":
public function rules()
{
return [
'user_akses_id' => 'required|numeric',
'nama' => 'required|max:50',
'email' => 'required|email|unique:users,email,' . $this->id,
'password' => 'required',
'foto' => 'nullable|image|max:1024|mimes:jpg,png,jpeg',
'ip' => 'nullable|ip',
'status' => 'required|boolean'
];
}
but if the updated id is not found eg:
127.0.0.1:8000/api/user/update/4
The response gets The email has already been taken.
What is the solution so that the return of the data is not found instead of validation first?
The code looks like it should work fine, sharing a few things below that may help.
Solution 1: Check if $this->id contains the id you are updating for.
Solution 2: Try using the following changes, try to get the id from the URL segment.
public function rules()
{
return [
'user_akses_id' => 'required|numeric',
'nama' => 'required|max:50',
'email' => 'required|email|unique:users,email,' . $this->segment(4),
'password' => 'required',
'foto' => 'nullable|image|max:1024|mimes:jpg,png,jpeg',
'ip' => 'nullable|ip',
'status' => 'required|boolean'
];
}
Sharing one more thing that may help you.
Some person uses Request keyword at the end of the request name. The Update sounds generic and the same as the method name you are using the request for. You can use UpdateRequest for more code readability.
What I understand from your question is, you need a way to check if the record really exists or not in the form request. If that's the case create a custom rule that will check if the record exists or not and use that rule inside your request.
CheckRecordRule
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class CheckRecordRule implements Rule
{
protected $recordId;
public function __construct($id)
{
$this->recordId = $id;
}
public function passes($attribute, $value)
{
// this will check and return true/false
return User::where('id', $this->recordId)->exists();
}
public function message()
{
return 'Record not found.';
}
}
Update form request
public function rules()
{
return [
'email' => 'required|email|unique:users,email,' . $this->id.'|'. new CheckRecordRule($this->id),
];
}
So when checking for duplicate it will also check if the record really exists or not and then redirect back with the proper message.

TimestampBehavior does not work because of failing validation

I have the following class with a TimestampBehaviour:
/**
* #property int $id
* #property string $name
* #property int $created_at
*/
class Workspace extends yii\db\ActiveRecord {
public static function tableName() {
return 'workspace';
}
public function behaviors() {
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
ActiveRecord::EVENT_BEFORE_UPDATE => false,
],
'value' => date('Y-m-d H:i:s')
],
];
}
...
}
For some reason the behavior does not populate the property. It is always empty when I try to save the model ($workspace->save()). I cannot save it since validation fails ("created_at cannot be blank"). There is nothing special with this class. Nothing is overridden. What could be the problem?
It turned out that the validation rules caused the troubles. Unexpected, since I thought all is correct. These were my rules:
public function rules() {
return [
[['id', 'name', 'created_at'], 'required'],
[['id'], 'int'],
[['name'], 'string', 'max' => 100],
[['created_at' ], 'datetime'],
];
}
created_at must not be required - that was the problem.
It is even documented:
Because attribute values will be set automatically by this behavior,
they are usually not user input and should therefore not be validated,
i.e. created_at and updated_at should not appear in the rules() method
of the model.
When $workspace->save() gets executed then the first step is the validation. And only after that step the EVENT_BEFORE_INSERT/EVENT_BEFORE_UPDATE gets triggered which causes TimestampBehaviour to populate the specified fields. And this happens only if the validation was successful! (if you var_dump you will indeed see an empty created_at.) Too late, validation has taken place already and I've got the validation error.
Recommended solution is to remove created_at from the required rule. Other approaches are also possible, of course (e.g. turn off validation or pass the properties that should be validated when save() gets called).
Add behaviour like bellow
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
'value' => new Expression('NOW()'),
],
];
}
and add it to safe records in your model class.
public function rules()
{
return array(
array('create_time,update_time', 'safe'),
);
}

unique value with custom request laravel 5.3

i have custom request in laravel ..
this is the code
public function rules()
{
if($this->ajax())
{
return [];
}
else
{
return
[
'username'=> 'required|min:3|max:30|unique:users',
'password'=> 'required|min:6',
'email'=>'required|min:3|max:35|unique:users',
'permission'=>'required',
'phone'=>'required',
'division'=>'required',
];
}
}
and i need to to ignore the current id from validation
i tried this
public function rules()
{
if($this->ajax())
{
return [];
}
else
{
return
[
'username'=> 'required|min:3|max:30|unique:users,id'.$this->id,
'password'=> 'required|min:6',
'email'=>'required|min:3|max:35|unique:users',
'permission'=>'required',
'phone'=>'required',
'division'=>'required',
];
}
}
but its ignoring the whole user name from validation not just the current id ..
Use auth()->user()->id instead of $this->id to get current user's ID.
Also, I'm not sure about the syntax you're using when trying to add ignoring ID. From unique() rule docs:
To instruct the validator to ignore the user's ID, we'll use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the | character to delimit the rules:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);

Trim inputs before validation in the Requests - Laravel

I am using laravel 5.1 and I am validating the inputs in a Requests as below,
public function rules()
{
return [
'first_name' => 'required|min:2'
];
}
Here if the first_name = ' s' validation passes which i not expected, I need to trim the inputs before the validation happens.
I tried like this,
public function rules()
{
$this->replace(array_map('trim', $this->all()));
return [
'first_name' => 'required|min:2'
];
}
But I am getting the same result.
I saw something related to laravel 4.x (LINK)
How to get this done?
NOTE: I am not validating in the controller by creating a validation instance, I am using Request to validate the request.
try this, I hope this might help you.
public function rules()
{
$input = $this->all();
$input['first_name'] = trim($input['first_name']);
$this->replace($input);
return [
'first_name' => 'required|min:2',
];
}

Resources