I have the following validation,
it should match string with letters,numbers,dashes. And empty input should also be valid.
The normal string validation is ok, but I can not make it match "empty" input.
'letter_code' => 'regex:/^[A-Za-z0-9\-]*$/'
letter_code format is invalid
tests:
"C14" // valid
"3.14" // "format is invalid", as expected
"-" // valid
"" // "format is invalid", NOT expected
I just found out in laracasts forum, that there is a nullable rule.
You can read about it in the official docs.
Without the nullable rule empty strings are considered invalid if there is a regex rule as well.
If you don't add required as additional validator empty string must pass
Here is phpunit test:
/** #test */
public function letterCode()
{
$trans = new \Illuminate\Translation\Translator(
new \Illuminate\Translation\ArrayLoader, 'en'
);
$regex = 'regex:/^[A-Za-z0-9\-]*$/';
$v = new Validator($trans, ['x' => 'C14'], ['x' => $regex]);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['x' => '3.14'], ['x' => $regex]);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['x' => '-'], ['x' => $regex]);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['x' => ''], ['x' => $regex]);
$this->assertTrue($v->passes());
}
This is tested with Laravel 5.5
I was facing the same problem in Laravel 7.x using GraphQL Types. I needed something similar to this in a field called phone or nothig (empty string). So I did something like this:
'phone' => [
'name' => 'phone',
'type' => Type::string(),
'rules' => ['regex:/^[A-Za-z0-9\-]*$/','nullable']
Here, phone is the name of the field you want to enter text into, and rules is what we define as regex, or NULL.
Hope this helps someone!
Related
I am pretty much below beginner level for REGEX/REGEXP and have hit a blocking point in a project I am working in, where I am trying to get the ids for posts that match the search criteria , but I want to restrict the search between 2 sub-strings. I am trying to figure out is how to write the REGEXP in the meta_query:
$args = array(
'post_type'=> 'custom',
'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $page,
'meta_query' => array(
array(
'key' => 'key',
'value' => "title*".$search."*_title",
'compare' => 'REGEXP',
)
),
);
And an example of the field in the DB :
a:302:{s:5:"title";s:10:"Test title";s:6:"_title";s:19:"
Unfortunately none of the combinations I tried based on documentation of SQL REGEXP won't return any values and I am trying to understand how I can pull this off and would appreciate any input.
Also would rather stick to WP_Query for now even though an SQL LIKE "%title%{$search}%_title%" works perfectly , so an alternative solution would be how to set the compare to 'LIKE' and parse it '%' since that is not possible out of the box as the % get escaped I believe.
I don't know why am receiving a:
No ending delimiter '/' found`
Here is a live sample
Here is my regex validations
$validation = Validator::make($request->all(), [
'cpf' => 'required|regex:/[0-9]{11}/',
'identidade' => 'required|regex:/[0-9]{6,11}/'
]);
This one regex:/[0-9]{11}/ is working fine;
But when I put a min delimiter regex:/[0-9]{6,11}/ I get the error.
Anyone know why?
Which version of Laravel are you using?
I'm using 5.8 and it is working fine.
But you can also try to use an array for the rules:
$validation = Validator::make($request->all(), [
'cpf' => ['required', 'regex:/[0-9]{11}/'],
'identidade' => ['required', 'regex:/[0-9]{6,11}/']
]);
When i am using preg_match to validate a input it behaves as i want.
$str = "abc-'xx";
$is_valid = preg_match("/[^A-Za-z'-]/", $str); // output => true
But when I am using laravel validation
$rules = [
'name' => "regex:/[^A-Za-z'-]/"
];
OR
$rules = [
'name' => ["regex:/[^A-Za-z'-]/"]
];
it always failed.
Why? and How can i solve this?
I was able to solve this by using regex /^[A-Za-z\-\']+$/
$rules = [
'name' => ["regex:/^[A-Za-z\-\']+$/"]
];
But couldn't find a reason, Appreciate someone can explain this.
Before i insert the email into the database -> i validate the adress with
if (filter_var($emailAdress, FILTER_VALIDATE_EMAIL))
{
....
}
.. but is this maybe a security flaw?
$userAccObj = $db->user->findOne( array('email' => array('$regex' => '^'.$emailAdress.'$', '$options' => 'i') ));
Schould i do this? or is it not necessary?
$emailAdress= preg_replace("/\#/", '\#', $emailAdress);
$emailAdress= preg_replace("/\-/", '\-', $emailAdress);
$emailAdress= preg_replace("/\./", '\.', $emailAdress);
if (filter_var($emailAdress, FILTER_VALIDATE_EMAIL))
Is a good way to vlaidate an email address in PHP, however, it does use regexes but so far, those have proven to be the best.
$userAccObj = $db->user->findOne( array('email' => array('$regex' => '^'.$emailAdress.'$', '$options' => 'i') ));
The only real problem with that is the . which is a special character which will effect how the regex works, but do you really need to do a regex here? You have checked it is a full email address as such you just need to check for where that exact email address exists (or better yet make a unique index on the field).
As I such I think you can take out the regex and do an exact match.
I was searching for a function, or a 'format code' function in NetBeans to format my arrays in PHP in a proper way.
E.g.:
$arr1 = array('allow',' => array('index', 'admin', 'view', 'create', 'update', 'delete'),' => array('#'),), array('deny', 'users' => array('*'),);
^^ Is quite hard to read. So if there would be a function to search for:
everytime there is a array( followed by ' replace it with an array(\n'.
or
everytime there is ,\s'\w+' followed by => replace ,\s' with ,\s\n'
Does anyone know if there is a plugin for that? (Btw, I'm not a star in regex so I'm just looking for the array() format problem.)