I have the following validation requirement.
if inventory_purchase_bill == True, lines..sku should be
required|exists:items,sku if inventory_purchase_bill == False,
lines..sku should be nullable|exists:items,sku
Basically test for exists only if there is a value. If no value or null then, don't check for exists.
Tried following, but second-line overrides first. How to achieve this?
$validation = $this->validate($request, [
'inventory_purchase_bill' => 'required',
'lines.*.sku' => 'exclude_unless:inventory_purchase_bill,false|nullable|exists:items,sku',
'lines.*.sku' => 'exclude_unless:inventory_purchase_bill,true|required|exists:items,sku',
]);
'lines.*.sku' => function($attribute, $value, $fail) use ($request) {
if ($request->bill_type == 'Stock Purchase') {
if($value !=null && $value != ''){
$item = Item::where('sku',$value)->first();
if($item == null){
return $fail($value.' not in database. Please add new item.');
}
}else{
return $fail('SKU is required.');
}
}
if ($request->bill_type == 'Expense') {
if($value !=null && $value != ''){
$item = Item::where('sku',$value)->first();
if($item == null){
return $fail($value.' not in database. Please add new item.');
}
}
}
},
Related
How can I set "array_filter" to accept the null values, empty and zero?
I tried the callback function but it didn't work for me.
Here's my code
$student = array_filter($request->student);
$teacher = array_filter($request->teacher);
$ScID = array_map(null, $student, $teacher);
SchoolDescriptions::where('pin_id', $request->session()->get('pin_id'))->delete();
foreach ($ScID as $key=>$array) {
SchoolDescriptions::updateOrCreate([
'student' => $array[0],
'teacher' => $array[1],
'pin_id' => $request->session()->get('pin_id')
]);
}; return back()->withStatus(__('Successfully saved.'));
I was able to find a solution for this. So I just simply made a condition.
To accept null values when submitting teacher and student forms.
if ($request->student == null && $request->teacher == null)
{
//do nothing
}
else {
$student = array_filter($request->student);
$teacher = array_filter($request->teacher);
$ScID = array_map($student, $teacher);
SchoolDescriptions::where('pin_id', $request->session()->get('pin_id'))->delete();
foreach ($ScID as $key=>$array) {
SchoolDescriptions::updateOrCreate([
'student' => $array[0],
'teacher' => $array[1],
'pin_id' => $request->session()->get('pin_id')
]);
};
}
I want to make start_po_no validation(required_if and digits_between) when po_type value is 1 and use_spo field value is 1.
Now, I'm written validation rule in request file like following:
public function rules()
{
return [
'use_spo' => 'required',
'po_type' => 'required',
'start_po_no' => Rule::requiredIf(function () use ($supplierPoUse, $generatePo) {
return request("use_spo") == 1 && request("po_type") == 1;
}).'|digits_between:1,9',
];
}
But, this code only correct for required_if, but digits_between is still not working. I read laravel validation documentation but I'm not user how to check it correctly.
#Edit
But, digits_between validation still checking even use_spo or po_type is not 1. I want also digits_between validation when use_spo and po_type is equal to 1.
Based on the explanation, I believe you can just add the rules conditionally. No reason to make it more complicated.
public function rules()
{
$rules = [
'use_spo' => 'required',
'po_type' => 'required',
];
if (request("use_spo") == 1 && request("po_type") == 1) {
$rules['start_po_no'] = 'required|digits_between:1,9';
}
return $rules;
}
I have two ways of doing it
Using Closures
return [
'use_spo' => 'required',
'po_type' => 'required',
'start_po_no' => [
Rule::requiredIf(function () use ($supplierPoUse, $generatePo) {
return request("use_spo") == 1 && request("po_type") == 1;
}),
function ($attribute, $value, $fail) {
if (request("use_spo") == 1 && request("po_type") == 1) {
if(!(1 <= $value && $value <= 9))
$fail('The '.$attribute.' is invalid.');
}
}
]
];
Using add()
Don't use digits between just after but do it after validation rules are run.
you can add your validation after initial validation.
$validator = Validator::make(...);
$validator->after(function ($validator) {
if (request("use_spo") == 1 && request("po_type") == 1) {
if(!(1 <= $value && $value <= 9))
$validator->errors()->add(
'start_po_no', 'invalid start po no'
);
}
});
if ($validator->fails()) {
// throw your validation exception
}
I wrote this Code For Image Upload but I do not know if it is secure, or not. Is There any issue or vulnerability in this code??
if($request->hasFile('image')){
$AllowedImages = ['jpeg', 'jpg', 'png'];
$AllowedImageTypes = ['image/jpeg', 'image/png'];
$image = $request->image;
$ImageNameWithExtension = $image->getClientOriginalName();
$ImageName = pathinfo($ImageNameWithExtension, PATHINFO_FILENAME);
$ImageExtension = $image->getClientOriginalExtension();
$ImageType = $image->getMimeType();
$ImageLocalPath = $image->getPathName();
$ImageSize = $image->getSize();
$ImageError = $image->getError();
$ImageNewName = sha1(md5($ImageName)).''.sha1(time()).'.'.$ImageExtension;
if(in_array($ImageType, $AllowedImageTypes) && in_array($ImageExtension, $AllowedImages) && getimagesize($ImageLocalPath) && ($ImageError === 0) && ($ImageSize <= 2000000)){
if($ImageType == 'image/jpeg' && ( $ImageExtension == 'jpeg' || $ImageExtension == 'jpg')){
$img = imagecreatefromjpeg($ImageLocalPath);
imagejpeg( $img, $ImageNewName, 100);
}
elseif($ImageType == 'image/png' && $ImageExtension == 'png'){
$img = imagecreatefrompng($ImageLocalPath);
imagepng( $img, $ImageNewName, 9);
}
imagedestroy($img);
try
{
$StoreImage = $image->storeAs('public/Upload/', $ImageNewName);
if(!$StoreImage){
throw new customException('File Upload Failed');
}
}
catch(customException $e){
session()->flash('File_Error', $e->errorMessage());
return back();
}
}
else{
session()->flash('File_Error', 'Image Validation Error Found');
return back();
}
}
else{
return back();
}
Consider this refactor for your code, it will help make your code cleaner.
public function store(Request $request)
{
$record = Model::create( $this->validateRequest() ); // this can insert other data into your database. In the db table, initially set the image related fields to nullable
$this->storeFile($record); // this will check if the request has a file and update the image related fields accordingly, else it will remain blank as it is nullable by default
return 'all data is saved';
}
private function validateRequest(){
return request()->validate([
'type' => 'nullable',
'image'=> request()->hasFile('image') ? 'mimes:jpeg,jpg,png|max:2000' : 'nullable', // 2000 means a maximum of 2MB
'other_field_1' => 'required',
'other_field_2' => 'required',
'other_field_3' => 'required'
]);
}
private function storeFile($record){
if( request()->hasFile('image') ){
$record->update([
'type' => request()->file->extension(),
'image' => request()->file->store('uploads/files', 'public') // The file will be hashed by default. public is used as second argument so you can access the uploaded file via your public folder
]);
}
}
This is check for file in the request, validate the file and other data, upload the file into storage folder.
You can use this code, for upload image :
In Request file :
public function rules()
{
return [
'image' => 'required|mimes:jpeg,jpg,png|max:50000'
],
}
And in your controller :
public function uploadImage(YourRequestClass $request){
$image = $request->file('image');
try{
$order=new Order();
if (!file_exists('upload/' . $image)) {
$currentDate = Carbon::now()->toDateString();
$imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
if (!file_exists('upload/')) {
mkdir('upload/', 0777, true);
}
$image->move('upload/', $imageName);
$order->image = $imageName;
}
$order->save();
return back();
} catche(\Exception $e){
Log::error($e);
return back();
}
}
I am creating an application based on Laravel 5.8. I want to manually authentication users based on some checks, But these checks or fields have some null values or not null values.
I follow the official documentation Link
Instead of checking like this
if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
// The user is active, not suspended, and exists.
}
I want to check if some fields that are not null like
if (Auth::attempt(['email' => $email, 'password' => $password, 'activate_on' => 'SomeDateTimeValue Or Not Null' ])) {
}
So it means if the user has some activate_on fields value which should not Null then the Auth::attempt should return true otherwise false.
You can do it by adding your implementation of the UserProvider interface, but that's a lot of work.
I think the easiest way is to do it in two steps.
// first get the user by email
$user = User::whereEmail($email)->first();
if($user->activate_on && Auth::attempt(['email' => $email, 'password' => $password])
{
// logged in
}
I do not believe what you are asking is directly possible. If you take a look at the retrieveByCredentials() method which is called during the attempt() process, the query builder is only set up to accept a value where($key, $value) or an array of values whereIn($key, $value) to conditionally query the user.
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials) ||
(count($credentials) === 1 &&
array_key_exists('password', $credentials))) {
return;
}
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->newModelQuery();
foreach ($credentials as $key => $value) {
if (Str::contains($key, 'password')) {
continue;
}
if (is_array($value) || $value instanceof Arrayable) {
$query->whereIn($key, $value);
} else {
$query->where($key, $value);
}
}
return $query->first();
}
EDIT:
Sample helper function:
if (!function_exists('attempt')) {
function attempt($credentials, $dates = [], $remember = false)
{
$user = User::where('email',$credentials['email'])->first();
// if a date is null return false
foreach ((array)$dates as $date) {
if (is_null($user->{$date})) {
return false;
}
}
return Auth::attempt($credentials, $remember);
}
}
Usage:
// single date
if (attempt(['email' => $email, 'password' => $password],'activate_on')) {
// ...
}
// array of dates
if (attempt(['email' => $email, 'password' => $password],['activate_on','approve_on'])) {
// ...
}
// no date
if (attempt(['email' => $email, 'password' => $password])) {
// ...
}
Validation row with others rows in cakephp
i need validate "date range" to save with others "date ranges" values.
Something like this:
function dateNotColision($check) {
foreach($this->data[$this->name] as $row){
if(($row['date_start']>=$date_start && $date_start<=$row['date_end']) ||
($row['date_start']>=$date_end && $date_end<=$row['date_end']) ){
return false;
}
}
return true;
}
how i could?
public function customDateValidation($field) {
return ($this->data[$this->alias]['date_start'] >= $date_start && $this->data[$this->alias]['date_end'] <= $date_start) || ($this->data[$this->alias]['date_start'] >= $date_end && $this->data[$this->alias]['date_end'] <= $date_end)
}
in your validate array just set something like
'start_date' => array(
'rule' => 'customDateValidation',
'message' => 'wrong dates'
)
Finally i did this:
In Model:
public $validate = array(
'date_start' =>array('rule'=>'dateNotColision',
'message' => 'Date Colision'
),
'date_end' =>array('rule'=>'dateNotColision',
'message' => 'Date Colision'
)
);
var $dataArray=array();
public function setDataArray($array){
$this->dataArray=$array;
}
function dateNotColision($check) {
foreach($this->dataArray as $row){
if(($row['date_start']>=$check && $check<=$row['date_end'])){
return false;
}
}
return true;
}
In Controller:
$this->Hotel->Season->setDataArray($this->request->data['Season']);
foreach($this->request->data['Season'] as $reviewData){
$this->Hotel->Season->saveAssociated($reviewData);
}