Combine numbers and letters in the username in yii2 - yii2

I want the pattern definition for the username to have common numbers and letters and to get an error if it enters only numbers or only letters
[['user'], 'match', 'pattern' => '/^[A-Za-z0-9_-]+$/i']

check this pattern (positive lookahead):
[['user'], 'match', 'pattern' => '/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/']
found here

Related

Puppet: file_line evaluates os.path.join

I'm trying to replace the Django-Settings line
'NAME': os.path.join(BASE_DIR , 'db.sqlite3'),
by
'NAME': os.path.join(BASE_DIR , 'db.mysql'),
but it doesn't find it.
The other line with the ENGINE parameter works fine. The problem must be the "os.path.join"-part.
# replace sqlite3 db with mysql
file { '/var/www/mysite/mysite/settings.py':
ensure => present,
}
file_line { 'replace db engine':
path => '/var/www/mysite/mysite/settings.py',
replace => true,
line => "'ENGINE': 'django.db.backends.mysql',",
match => "'ENGINE': 'django.db.backends.sqlite3',",
append_on_no_match => false,
}
file_line { 'replace db name':
path => '/var/www/mysite/mysite/settings.py',
replace => true,
line => "\'NAME\': os.path.join(BASE_DIR , \'db.mysql\'),",
match => "\'NAME\': os.path.join(BASE_DIR , \'db.sqlite3\'),",
append_on_no_match => false,
}
I tried it with \' and without \ .
Can somebody please help?
EDIT:
So if I add something like this beforehand:
class { '::mysql::server':
root_password => 'strongpassword',
override_options => { 'mysqld' => { 'max_connections' => '1024' } }
}
mysql::db { 'mynewDB':
user => 'admin',
password => 'secret',
host => 'master.puppetlabs.vm',
sql => '/tmp/states.sql',
require => File['/tmp/states.sql'],
}
Then I would replace the NAME parameter with "mynewDB"?
Did I understand it correctly?
You need to bear in mind that the match parameter to a file_line resource conveys a regular expression, not a plain string. Puppet uses the Ruby flavor of regular expressions. In that dialect, like in many others, the parentheses (()) are metacharacters, signifying grouping. You must escape them if you want them to be interpreted as literals. Moreover, because Ruby regexes use the same escape character that Puppet strings do, you must also escape the escape character to pass it through Puppet to the underlying regex engine. On the other hand, you do not need to escape single quotes inside a double-quoted string, or vise versa, though doing so should not be harmful.
Example:
file_line { 'replace db name':
path => '/var/www/mysite/mysite/settings.py',
replace => true,
line => "'NAME': os.path.join(BASE_DIR , 'db.mysql'),",
match => "'NAME': os.path.join\\(BASE_DIR , 'db.sqlite3'\\),",
append_on_no_match => false,
}
But that's a bit of a poor design. If you're trying to ensure that the database you want is properly named (regardless of what the actual name should be), then to the greatest degree possible, you should match the line you want to manage in a way that does not depend on the current database name.
I'm not knowledgeable about Django specifics, but if you can rely on only one NAME property being specified in the settings file then you might instead do this:
file_line { 'replace db name':
path => '/var/www/mysite/mysite/settings.py',
replace => true,
line => 'Whatever the line should really be',
match => "\\s*'NAME':.*",
append_on_no_match => false,
}
The match expression there matches a line with an arbitrary amount of leading whitespace, followed by the literal characters 'NAME':, followed by anything.
But you should also consider whether file_line is the right tool for the job at all. It really makes sense only if you need to accommodate some parts of the file being managed outside Puppet, which is an uncomfortable situation, albeit one that sometimes we are stuck with. If possible, though, it is better to allow Puppet to manage the entire file, including its complete contents.

Syntax for defining rules for routes in yii2

I am using Yii2's pretty urls and want to play around with the rules defined in my UrlManager but not finding any documentation as to how I can define variables in the 'pattern' => 'route' rule set. Found some examples like
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
"home" => "site/index",
"login" => "site/login",
"sign-up" => "site/sign-up",
'<controller:[\w-]+>/<id:\d+>' => '<controller>/view',
],
],
But what does the :[\w-]+ or the :\d+ stand for?
What if I wanted, for example, to define a pattern to point to my action which needs two parameters
class MyController extends Controller{
...
public function actionMyAction($param1, $param2){
...
}
}
now I want my web users to type in the url bar www.mysite.com/my-controller/my-action/X-Y where X is the value of $param1 and Y is the value of $param2 and using - as a parameter separator.
Thanks.
[\w-]+ and \d+ are regular expressions, the first indicating any letter or the dash character, repeated one or more times, the section indicating numbers only, repeated one or more times.
In the rule expression, you use <variable name:regex> to put a placeholder for your route that will resolve to variables passed to your controller action.
The rule should look like this if both $param1 and $param2 are numbers.
'my-controller/my-action/<param1:\d+>-<param2:\d+>' => 'my-controller/my-action',
Swap \d for \w if you need letters.

Raw HTML in HelperList column

I have a HelperList in Prestashop (1.6) with a few columns/rows. One of the columns is an anchor element (a href), and I need to show it like that in the list, but Prestashop escapes the value and represents it as a literal string.
How can I show an actual anchor element in one of the columns of a HelperList?
Plus: I'd like to not have to override PS classes nor copy the entire template just to change a single line of code. I do know how to do it using either of those ways, but I'm looking for something less "aggressive".
You can use this trick:
$fields_list = array(
'your_link' => array(
'title' => $this->l('Your title'),
'type' => 'bool',
'float' => true, // a trick - prevents from html escaping
// else code
),
);

Yii2 Query not quoting correctly

I run into this problem, time and time again. It would be nice to find out how to build queries properly so I can stop resorting to Yii::$app->db->createCommand() as a workaround.
My Yii2 query:
$users = UserSpatial::find()
->select('user_id, harvesine(y(coordinates), x(coordinates), :lat, :lon) as dist, astext(coordinates)')
->where('st_within(coordinates, envelope(linestring(point(:rlon1, :rlat1), point(:rlon2, :rlat2))))')
->orderBy('st_distance(point(:lon, :lat), coordinates)')
->params([
':lon' => $geo->lon,
':lat' => $geo->lat,
':rlon1' => $rlon1,
':rlat1' => $rlat1,
':rlon2' => $rlon2,
':rlat2' => $rlat2
])
->all();
The generated query ends up with backticks in all the wrong places and, oddly enough, not all parameters were backticked (sorry but you'll need to look closely for the misplaced backticks because I didn't know how best to highlight the incorrect placements):
SELECT \`user_id\`, harvesine(y(coordinates), x(coordinates), \`32.7699547\`, \`-116.9911288)\` AS \`dist\`, astext(coordinates)
FROM \`user_spatial\`
WHERE st_within(coordinates, envelope(linestring(point(-117.07730792871, 32.697490931884), point(-116.90494967129, 32.842418468116))))
ORDER BY st_distance(point(-116.9911288, \`32.7699547)\`, \`coordinates)\`
The query should look like the following as I did not wrap double-square-brackets around any of the fields or values:
SELECT \`user_id\`, harvesine(y(coordinates), x(coordinates), 32.7699547, -116.9911288) AS dist, astext(coordinates)
FROM \`user_spatial\`
WHERE st_within(coordinates, envelope(linestring(point(-117.07730792871, 32.697490931884), point(-116.90494967129, 32.842418468116))))
ORDER BY st_distance(point(-116.9911288, 32.7699547), coordinates)
I can live with Yii2 adding some backticks around field names and table names but why on earth is it backticking numerical values? (FYI: the $rlon and $rlat values don't seem to get backticked but I was assuming that was because they are a result of math calculations!?!?).
I've already tried forcing $geo->lon and $geo->lat to float values like so:
'lon' => (float)$geo->lon;
or
'lon' => (float)$geo->lon * 1;
but it didn't help.
Try to use array format for select and orderBy methods, like docs suggest:
Besides column names, you can also select DB expressions. You must use
the array format when selecting a DB expression that contains commas
to avoid incorrect automatic name quoting. For example,
$query->select(["CONCAT(first_name, ' ', last_name) AS full_name",
'email']);
In you case it will be like this:
$users = UserSpatial::find()
->select([
'user_id',
'harvesine(y(coordinates), x(coordinates), :lat, :lon) as dist',
'astext(coordinates)'
])
->where('st_within(coordinates, envelope(linestring(point(:rlon1, :rlat1), point(:rlon2, :rlat2))))')
->orderBy(['st_distance(point(:lon, :lat)', 'coordinates)'])
->params([
':lon' => $geo->lon,
':lat' => $geo->lat,
':rlon1' => $rlon1,
':rlat1' => $rlat1,
':rlon2' => $rlon2,
':rlat2' => $rlat2
])
->all();

Cakephp 3.0 I would like to include a Year input field with a drop down, but it is inputing as an array

I have a Year field in a form and I am using FormHelper.
echo $this->Form->input('year', [
'type' => 'year',
'minYear' => date('Y')-10,
'maxYear' => date('Y')
]);
The table file validator looks like:
->add('year', 'valid', ['rule' => 'numeric'])
->allowEmpty('year')
I have a very similar input in another app that seems to work fine. I set the MySql column to int(5) to match what I had working elsewhere.
Checking debugkit it shows the "year" input as an array while the other inputs are strings. If I remove the validation rule it throws an illegal array to string conversion, so I assume this is where the error is.
Any help is greatly appreciated.
I have just tested with your above code and it is working fine for me. Try to delete the cache and check it once more.
Creates a select element populated with the years from minYear to maxYear. Additionally, HTML attributes may be supplied in $options. If $options['empty'] is false, the select will not include an empty option:
empty - If true, the empty select option is shown. If a string, that
string is displayed as the empty element.
orderYear - Ordering of
year values in select options. Possible values ‘asc’, ‘desc’. Default
‘desc’ value The selected value of the input.
maxYear The max year to
appear in the select element.
minYear The min year to appear in the
select element.
Try this one:
<?php
echo $this->Form->year('exp_date', [
'minYear' => date('Y')-10,
'maxYear' => date('Y'),
'id' => 'cc-year',
'class' => 'form-control',
'empty' => false,
'orderYear' => 'asc'
]);
?>
Official Documentation: CookBook - Creating Year Inputs