This is my Laravel code:
$validator = \Validator::make($val, [
'service_activity' => 'required|max:1000',
'inspired' => 'required',
'obstacles' => 'required|max:1000',
'working_on_acitivity' => array('required', 'regex:^(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d)$'),
'spend_time' => 'required',
'activity_impact' => 'required|max:1000',
'fund_raising' => 'required',
'raised_amount' => 'required|numeric',
'people_involved' => 'required|numeric',
'learned_from_experience' => 'required|max:1000',
],$messages);
I want to use the regex for the field in working_on_acitivity but always getting the same error. I have used the regex in an array as given in the Laravel documentation:
type":"ErrorException","message":"preg_match(): No ending delimiter '^' found
Change
'regex:^(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d)$'
to
'regex:#^(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d)$#'
using # as delimiter.
(This is based on your error message talking about missing delimiters.)
Related
Array one:
[
'jabatan' => 'required',
'tipe_kegiatan' => 'required',
'waktu' => 'required',
'nrt' => 'required',
0 => [
'materi' => 'required',
'metode' => 'required',
'hasil' => 'required',
'img' => 'required|image|max:1024',
]
];
Array two:
[
'jabatan' => 'required',
'tipe_kegiatan' => 'required',
'waktu' => 'required',
'nrt' => 'required',
'materi' => 'required',
'metode' => 'required',
'hasil' => 'required',
'img' => 'required|image|max:1024',
];
how to change the data array as shown below
Change Data array one to array two (I try using array_push but I don't get the wanted result)
My case:
I have 2 conditions where if the user inputs by selecting form type 1 then the validation will adjust to form 1 and if the user selects input with form 2 then the validation also adjusts to form 2. all processes are in one post request action.
you should use array_merge like this:
<?php
$a1=["red","green"];
$a2=["blue","yellow"];
print_r(array_merge($a1,$a2));
?>
I want to register user but I want to put validation rule on username that username should not start with special characters and also should not start with web. I found regex that work fine special characters but detect the string it give me error of Invalid format
return [
'username' => [
'required',
'regex:/^\S*$/u',
'regex:/^[_]?[a-zA-Z0-9]+([_.-]?[a-zA-Z0-9])*$/',
'unique:users'
],
'full_name' => 'required',
];
Try this?
return [
'username' => [
'required|
not_regex:/^[web_-][a-z_\-0-9]*/i|
regex:/^[A-Za-z_ \-0-9]+$/u|
unique:users'
],
'full_name' => 'required',
];
This should work as you want it to
return [
'username' => [
'required',
'regex:/^(?!web)[a-zA-Z]\w+$/',
'unique:users'
],
'full_name' => 'required',
];
Here's a regex101 demo for you to test it out.
I am currently trying to setup a Laravel project on IIS and so far everything works as it should. The only problem is that when I try to validate my form that is being submitted it crashes with the error Serialization of 'Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed'
This happens on the following section of code:
$rules = array(
'title' => 'required',
'short_title' => 'required|between:1,15',
//'alias' => 'required',
'description' => 'required',
'province' => 'required',
'start_datetime' => 'required|date',
'url' => 'required',
'max_entries' => 'required|numeric',
'fee_sub' => 'numeric',
'fee_class_batch' => 'numeric',
'fee_class_batch_unpaid' => 'numeric',
'fee_class_batch_t' => 'numeric',
'fee_class_batch_t_unpaid' => 'numeric',
'fee_team_remove' => 'numeric'
);
$validator = Validator::make(Input::all(), $rules);
If I comment out the last line the form submit works without error. So it has something to do with that Input::all(), $rules.
Does someone know why this is happening?
I have a set of validation rules in laravel. there are two date fields that one of them have to be greater than other. but when I use gt operator, an error apears:
$validation = Validator::make($request->all(), [
'description' => 'required|string',
'started_at' => 'required|date',
'finished_at' => 'required|date|gt:started_at',
]);
error is: Method Illuminate\Validation\Validator::validateGt does not exist.
gt:started_at is wrong gt rule does not exist in laravel use after
$validation = Validator::make($request->all(), [
'description' => 'required|string',
'started_at' => 'required|date',
'finished_at' => 'required|date|after:started_at',
]);
I'm trying to write a validation check in PHP Laravel for a username field with the functionality to let the user know what went wrong. I have a couple of if statements with regular expression checks but it won't work. The requirements of the regular expression are: can't start with a ".", No more than 1 "." in a row, No capitals, Only a-z, No special characters. So for example like this "user.name" would be valid, but things like "username." or ".username" would all be invalid.
So far I got this:
$oValidator = Validator::make(Input::all(), [
'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/',
'username' => 'required',
'password' => 'required',
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required|email'
]);
I want to give feedback for the mistakes that user makes, example: user input is ".username", program feedback should be "Dot in front of string is not allowed".
All you have to do is to include a custom message for your validation.
$this->validate($request, [
'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/',
], ['regex' => 'Username cannot start with period(.), etc...]);
Your code should look like this. Please remember regex custom message will apply too all of these fields instead of just username so I would separate username validation like above.
$oValidator = Validator::make(Input::all(), [
'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/',
'username' => 'required',
'password' => 'required',
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required|email'
], ['regex' => 'Username cannot start with period, etc...']);