Laravel Regex Validattion - No ending delimiter '/' found - regex

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}/']
]);

Related

WP_Query meta_query REGEXP

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.

Concatenate strings in file_line resource

I'm trying to concatenate a string in a puppet manifest like so:
file_line {'Append to /etc/hosts':
ensure => present,
line => "${networking['ip'] + '\t'}${networking['fqdn'] + '\t'}${networking['hostname']}",
match => "${'^#?'+ networking['ip'] + '\s+' + networking['fqdn'] + '\s+' + networking['hostname']}",
path => '/etc/hosts'
}
I either get syntax errors or in the case above:
The value '' cannot be converted to Numeric
Which I'm guessing means that it doesn't like the plus operator.
So how do I interpolate the strings in the match and line attributes?
The problem here is that the operator + is restricted to only Numeric types (documentation). It cannot be used with String types. However, the spacing and regular expressions can still be used as normal without attempting a string concatenate. These merely need to be placed outside the variable interpolation. Therefore:
file_line { 'Append to /etc/hosts':
ensure => present,
line => "${networking['ip']}\t${networking['fqdn']}\t${networking['hostname']}",
match => "^#?${networking['ip']}\s+${networking['fqdn']}\s+${networking['hostname']}",
path => '/etc/hosts'
}
should resolve your issues with the type mismatch and the + operator.
i know you already got your answer, but you can make life easier using host to manage /etc/hosts:
host {
"localhost": ip => "127.0.0.1";
"$::puppet_server":
ip => "1.2.3.4",
host_aliases => [ "puppetserver" ],
;
"dns-01.tld":
ip => "1.2.3.5",
host_aliases => [ "dns-01" ],
;
"dns-02.tld":
ip => "1.2.3.6",
host_aliases => [ "dns-02" ],
;
}

Laravel regex validation for empty string

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!

Laravel regex validation strange behavior

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.

Can Netbeans use regex to make my own 'format code' functions?

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.)