'name' => ['required', 'regex:^[A- ZÀÂÇÉÈÊËÎÏÔÛÙÜŸÑÆŒa- zàâçéèêëîïôûùüÿñæœ0-9_.,()]+$^ ']
'name' => ['required', 'regex:/^[A- ZÀÂÇÉÈÊËÎÏÔÛÙÜŸÑÆŒa- zàâçéèêëîïôûùüÿñæœ0-9_.,() ]+$/']
address:{
minlength:2,
maxlength:500,
regex:/^[a-zA-z,-/_=. #]+$/,
},
protected static $rules = [
'zip_code' => 'required|regex:/\b\d{5}\b/'
];07 September, 2020
Xampp Server Start
Programing Coderfunda
September 07, 2020
No comments
Regex pattern validation in laravel
Programing Coderfunda
September 07, 2020
No comments
'name' => ['required', 'regex:^[A- ZÀÂÇÉÈÊËÎÏÔÛÙÜŸÑÆŒa- zàâçéèêëîïôûùüÿñæœ0-9_.,()]+$^ ']
'name' => ['required', 'regex:/^[A- ZÀÂÇÉÈÊËÎÏÔÛÙÜŸÑÆŒa- zàâçéèêëîïôûùüÿñæœ0-9_.,() ]+$/']
address:{
minlength:2,
maxlength:500,
regex:/^[a-zA-z,-/_=. #]+$/,
},
protected static $rules = [
'zip_code' => 'required|regex:/\b\d{5}\b/'
];
Xampp Server Start
Programing Coderfunda
September 07, 2020
No comments
Laravel validate decimal 0-99.99
Programing Coderfunda
September 07, 2020
No comments
The Laravel
between validation rule is actually pretty powerful and can handle decimal values as well. So there's no need to use regex just do this:'required|between:0,99.99'
function rules(){
return [
'field_name'=> array('regex:/^(?:d*.d{1,2}|d+ )$/','min:1','max:10')
];
}
Validate decimal numbers in JavaScript - IsNumeric()
// Whitespace strings:
IsNumeric(' ') == true;
IsNumeric('\t\t') == true;
IsNumeric('\n\r') == true;
// Number literals:
IsNumeric(-1) == false;
IsNumeric(0) == false;
IsNumeric(1.1) == false;
IsNumeric(8e5) == false;Some time ago I had to implement an IsNumeric function, to find out if a variable contained a numeric value, regardless of its type, it could be a String containing a numeric value (I had to consider also exponential notation, etc.), a Number object, virtually anything could be passed to that function, I couldn't make any type assumptions, taking care of type coercion (eg. +true == 1; but true shouldn't be considered as "numeric").2nd...................
Arrrgh! Don't listen to the regular expression answers. RegEx is icky for this, and I'm not talking just performance. It's so easy to make subtle, impossible to spot mistakes with your regular expression.
If you can't use
isNaN(), this should work much better:function IsNumeric(input)
{
return (input - 0) == input && (''+input).trim().length > 0;
}Here's how it works:The
(input - 0) expression forces JavaScript to do type coercion on your input value; it must first be interpreted as a number for the subtraction operation. If that conversion to a number fails, the expression will result in NaN. This numeric result is then compared to the original value you passed in. Since the left hand side is now numeric, type coercion is again used. Now that the input from both sides was coerced to the same type from the same original value, you would think they should always be the same (always true). However, there's a special rule that says NaN is never equal to NaN, and so a value that can't be converted to a number (and only values that cannot be converted to numbers) will result in false. The check on the length is for a special case involving empty strings. Also note that it falls down on your 0x89f test, but that's because in many environments that's an okay way to define a number literal. If you want to catch that specific scenario you could add an additional check. Even better, if that's your reason for not using
isNaN() then just wrap your own function around isNaN() that can also do the additional check.Laravel set between digits in validation
'required|integer|digits_ between:1,10'
or'display_post_count' => 'required|min:1|max:10',Get Multipal Array Data Array in Array Laravel
Programing Coderfunda
September 07, 2020
Laravel
No comments
(
[0] => Array
(
[id] => 8
[restaurant_id] => 5
[category_id] => 1
[deleted_at] =>
[created_at] => 2020-08-31T08:40:58.000000Z
[updated_at] => 2020-08-31T08:40:58.000000Z
[categorylistdd] => Array
(
[id] => 1
[name] => Burger
[image] => Burger.svg
[banner] =>
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
)
[1] => Array
(
[id] => 9
[restaurant_id] => 5
[category_id] => 2
[deleted_at] =>
[created_at] => 2020-08-31T08:44:40.000000Z
[updated_at] => 2020-08-31T08:44:40.000000Z
[categorylistdd] => Array
(
[id] => 2
[name] => Desert
[image] => Dessert.svg
[banner] =>
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
Laravel validate decimal 0-99.99
Programing Coderfunda
September 07, 2020
No comments
The Laravel
between validation rule is actually pretty
powerful and can handle decimal values as well. So there's no need to
use regex just do this:'required|between:0,99.99'
function rules(){
return [
'field_name'=> array('regex:/^(?:d*.d{1,2}|d+ )$/','min:1','max:10')
];
}
Validate decimal numbers in JavaScript - IsNumeric()
// Whitespace strings:
IsNumeric(' ') == true;
IsNumeric('\t\t') == true;
IsNumeric('\n\r') == true;
// Number literals:
IsNumeric(-1) == false;
IsNumeric(0) == false;
IsNumeric(1.1) == false;
IsNumeric(8e5) == false;
Some time ago I had to implement an IsNumeric function, to find out if a variable contained a numeric value, regardless of its type, it could be a String containing a numeric value (I had to consider also exponential notation, etc.), a Number
object, virtually anything could be passed to that function, I couldn't
make any type assumptions, taking care of type coercion (eg. +true == 1; but true shouldn't be considered as "numeric").2nd...................
Arrrgh! Don't listen to the regular expression answers. RegEx is icky for this, and I'm not talking just performance. It's so easy to make subtle, impossible to spot mistakes with your regular expression.
If you can't use
isNaN(), this should work much better:function IsNumeric(input)
{
return (input - 0) == input && (''+input).trim().length > 0;
}
Here's how it works:The
(input - 0) expression forces JavaScript to do type
coercion on your input value; it must first be interpreted as a number
for the subtraction operation. If that conversion to a number fails,
the expression will result in NaN. This numeric
result is then compared to the original value you passed in. Since the
left hand side is now numeric, type coercion is again used. Now that
the input from both sides was coerced to the same type from the same
original value, you would think they should always be the same (always
true). However, there's a special rule that says NaN is never equal to NaN,
and so a value that can't be converted to a number (and only values
that cannot be converted to numbers) will result in false. The check on the length is for a special case involving empty strings. Also note that it falls down on your 0x89f test, but that's because in many environments that's an okay way to define a number literal. If you want to catch that specific scenario you could add an additional check. Even better, if that's your reason for not using
isNaN() then just wrap your own function around isNaN() that can also do the additional check.Laravel set between digits in validation
'required|integer|digits_ between:1,10'
or'display_post_count' => 'required|min:1|max:10',Get Multipal Array Data Array in Array Laravel
Programing Coderfunda
September 07, 2020
Laravel
No comments
(
[0] => Array
(
[id] => 8
[restaurant_id] => 5
[category_id] => 1
[deleted_at] =>
[created_at] => 2020-08-31T08:40:58.000000Z
[updated_at] => 2020-08-31T08:40:58.000000Z
[categorylistdd] => Array
(
[id] => 1
[name] => Burger
[image] => Burger.svg
[banner] =>
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
)
[1] => Array
(
[id] => 9
[restaurant_id] => 5
[category_id] => 2
[deleted_at] =>
[created_at] => 2020-08-31T08:44:40.000000Z
[updated_at] => 2020-08-31T08:44:40.000000Z
[categorylistdd] => Array
(
[id] => 2
[name] => Desert
[image] => Dessert.svg
[banner] =>
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
03 September, 2020
Laravel: Login and Register forms on the same page
Programing Coderfunda
September 03, 2020
No comments
Laravel: Login and Register forms on the same page
July 1, 2019Quite often, especially in e-shop projects, you can see a page to login or register before finishing the purchase. But Laravel has these two routes as separate login and register pages. So how to merge them together and avoid conflicts?
Notice: at the end of this article, you will find a link to Github project with a simple Laravel checkout process.
This is the pre-checkout page we’re aiming for:
On the surface, it’s pretty easy – just copy/paste all the Blade code from /resources/views/auth/login.blade.php and /resources/views/auth/register.blade.php into one common, let’s say, /resources/views/checkout.blade.php. And that’s true, but that’s only one part of the story.
The problem arises because of the same names of fields.
Login form has this:
<input id="email" type="email"
class="form-control @error('email') is-invalid @enderror" name="email"
value="{{ old('email') }}" required autocomplete="email" autofocus>
Register form has this:
<input id="email" type="email"
class="form-control @error('email') is-invalid @enderror" name="register_email"
value="{{ old('email') }}" required autocomplete="email">
See the difference? I don’t either. Except for autofocus, which is irrelevant in our case.
And you would think that it’s ok, because those two forms POST to different URLs and perform different actions. But in case of failing validation, you get errors on both forms instead of one. For example, in case of invalid credentials when you login, you see this:
Step 1. Rename the fields in one of the form.
For this example, we will rename the fields in the registration part, adding “register_” prefix to them:
<input id="register_name" type="text"
class="form-control @error('register_name') is-invalid @enderror" name="register_name"
value="{{ old('register_name') }}" required autocomplete="name" autofocus>
@error('register_name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
...
<input id="register_email" type="email"
class="form-control @error('register_email') is-invalid @enderror" name="register_email"
value="{{ old('register_email') }}" required autocomplete="email">
@error('register_email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
...
<input id="register_password" type="password"
class="form-control @error('register_password') is-invalid @enderror"
name="register_password"
required autocomplete="new-password">
@error('register_password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
...
<input id="password-confirm" type="password" class="form-control"
name="register_password_confirmation" required autocomplete="new-password">
Step 2. Override validator in RegisterController.
Here’s the method validator() from the original app/Http/Controllers/Auth/RegisterController.php, that comes from the framework itself:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
So, we need to override that validator with our own, adding some rules to it. In fact, we just need to use the renamed fields from Step 1, and then assign their names properly. Here’s the code:
protected function validator(array $data)
{
$validator = Validator::make($data, [
'register_name' => ['required', 'string', 'max:255'],
'register_email' => ['required', 'string', 'email', 'max:255', 'unique:users,email'],
'register_password' => ['required', 'string', 'min:8', 'confirmed'],
]);
$validator->setAttributeNames([
'register_name' => 'name',
'register_email' => 'email',
'register_password' => 'password',
]);
return $validator;
}
As you can see, we now use register_xxx fields instead of just xxx, but we also need to make sure they return validation errors with their “original” names, so for that we use less-known setAttributeNames() method. It’s not officially mentioned in documentation, but you see another example of its use in this StackOverflow thread.
And that’s it! Now, we have separate two forms on the same “checkout” page.
Bonus: E-shop Checkout Demo Project
As a part of this article, we actually created a typical checkout process mini-project, which is available on Github for you to see. Homepage contains list of products, you can buy each of them, and then session has your “cart” which you can checkout at any time, by clicking “Checkout” on top.
Laravel: Login and Register forms on the same page
Programing Coderfunda
September 03, 2020
No comments
Laravel: Login and Register forms on the same page
July 1, 2019Quite often, especially in e-shop projects, you can see a page to login or register before finishing the purchase. But Laravel has these two routes as separate login and register pages. So how to merge them together and avoid conflicts?
Notice: at the end of this article, you will find a link to Github project with a simple Laravel checkout process.
This is the pre-checkout page we’re aiming for:
On the surface, it’s pretty easy – just copy/paste all the Blade code from /resources/views/auth/login.blade.php and /resources/views/auth/register.blade.php into one common, let’s say, /resources/views/checkout.blade.php. And that’s true, but that’s only one part of the story.
The problem arises because of the same names of fields.
Login form has this:
<input id="email" type="email"
class="form-control @error('email') is-invalid @enderror" name="email"
value="{{ old('email') }}" required autocomplete="email" autofocus>
Register form has this:
<input id="email" type="email"
class="form-control @error('email') is-invalid @enderror" name="register_email"
value="{{ old('email') }}" required autocomplete="email">
See the difference? I don’t either. Except for autofocus, which is irrelevant in our case.
And you would think that it’s ok, because those two forms POST to different URLs and perform different actions. But in case of failing validation, you get errors on both forms instead of one. For example, in case of invalid credentials when you login, you see this:
Step 1. Rename the fields in one of the form.
For this example, we will rename the fields in the registration part, adding “register_” prefix to them:
<input id="register_name" type="text"
class="form-control @error('register_name') is-invalid @enderror" name="register_name"
value="{{ old('register_name') }}" required autocomplete="name" autofocus>
@error('register_name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
...
<input id="register_email" type="email"
class="form-control @error('register_email') is-invalid @enderror" name="register_email"
value="{{ old('register_email') }}" required autocomplete="email">
@error('register_email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
...
<input id="register_password" type="password"
class="form-control @error('register_password') is-invalid @enderror"
name="register_password"
required autocomplete="new-password">
@error('register_password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
...
<input id="password-confirm" type="password" class="form-control"
name="register_password_confirmation" required autocomplete="new-password">
Step 2. Override validator in RegisterController.
Here’s the method validator() from the original app/Http/Controllers/Auth/RegisterController.php, that comes from the framework itself:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
So, we need to override that validator with our own, adding some rules to it. In fact, we just need to use the renamed fields from Step 1, and then assign their names properly. Here’s the code:
protected function validator(array $data)
{
$validator = Validator::make($data, [
'register_name' => ['required', 'string', 'max:255'],
'register_email' => ['required', 'string', 'email', 'max:255', 'unique:users,email'],
'register_password' => ['required', 'string', 'min:8', 'confirmed'],
]);
$validator->setAttributeNames([
'register_name' => 'name',
'register_email' => 'email',
'register_password' => 'password',
]);
return $validator;
}
As you can see, we now use register_xxx fields instead of just xxx, but we also need to make sure they return validation errors with their “original” names, so for that we use less-known setAttributeNames() method. It’s not officially mentioned in documentation, but you see another example of its use in this StackOverflow thread.
And that’s it! Now, we have separate two forms on the same “checkout” page.
Bonus: E-shop Checkout Demo Project
As a part of this article, we actually created a typical checkout process mini-project, which is available on Github for you to see. Homepage contains list of products, you can buy each of them, and then session has your “cart” which you can checkout at any time, by clicking “Checkout” on top.
Advanced Laravel: 14 Topics and Links to Learn Them
Programing Coderfunda
September 03, 2020
Laravel
No comments
Advanced Laravel: 14 Topics and Links to Learn Them
Programing Coderfunda
September 03, 2020
Laravel
No comments
02 September, 2020
Laravel 5 Eloquent where and or in Clauses
Programing Coderfunda
September 02, 2020
No comments
CabRes::where('m__Id', 46)
->where('t_Id', 2)
->where(function($q) {
$q->where('Cab', 2)
->orWhere('Cab', 4);
})
->get();whereIn():CabRes::where('m__Id', 46)
->where('t_Id', 2)
->whereIn('Cab', $cabIds)
->get();