Tag Archive error

C#: Error CS0579 Duplicate ‘System.Reflection.AssemblyCompanyAttribute’

เขียน c# อยู่ๆเจอ error Error CS0579 จริงๆ ไม่ใช่อยู่ๆ หรอก ทุกอย่างมันมีเหตุปัจจัยมันถึงแจ้ง error มาให้ดู โดยที่เจอคือ

  • Error CS0579 Duplicate ‘System.Reflection.AssemblyConfigurationAttribute’
  • Error CS0579 Duplicate ‘System.Reflection.AssemblyConfigurationAttribute’
  • Error CS0579 Duplicate ‘System.Reflection.AssemblyFileVersionAttribute’
  • Error CS0579 Duplicate ‘System.Reflection.AssemblyProductAttribute’
  • Error CS0579 Duplicate ‘System.Reflection.AssemblyTitleAttribute’
  • Error CS0579 Duplicate ‘System.Reflection.AssemblyVersionAttribute’

ดูโดยรวมแล้วมันคือไฟล์ที่บันทึกเวอร์ชั่นของโปรเจคที่เราได้เขียนเอาไว้ สาเหตุก็น่าจะง่ายๆ ไปแก้ไขมันแล้วพังนั่นเอง แต่ไม่น่าพังนะ ก็ไปแก้ตามวิธีปกติเองนี่น่า ไปไล่ๆ ดูจนพบวิธีแก้ คือ

  1. ไปที่โฟลเดอร์ของ project ที่มีปัญหา
  2. ค้นหาไฟล์ *.Assemblyinfo.cs
  3. จะเห็นว่ามันมีไฟล์ตัวนี้อยู่ 5 ตัวเลย ลบทิ้งทั้งหมด
  4. ลอง clearn แล้ว run project ดูใหม่ ถ้ายัง error ไปบรรทัดที่มันเตือนอยู่แล้วลบทิ้งไปเลย

Cr. Duplicate AssemblyVersion Attribute

ทำ Virtual Host ใน Apache

เพราะว่าต้องแยกส่วนของ frontend และ backend ไว้คนละ server เพื่อความสดวกและปลอดภัย โดยทั้งคู่จะติดต่อกันผ่านทาง api แต่เวลาเขียนมีคอมพิวเตอร์แค่เครื่องเดียว ตามปกติสามารถทำได้โดยแยกงานไว้คนละ folder และเพื่อให้เหมือนของจริงยิ่งขึ้นก็ใข้วิธีทำ Virtual Host แยกออกเป็น 2 เซิร์ฟเวอร์ (ถึงจะแค่เครื่องจำลองก็เถอะ)

ทำได้โดยการเปิดไฟล์ C:\xampp\apache\conf\extra\httpd-vhosts.conf และเพิ่ม[code language=”text” title=”C:\xampp\apache\conf\extra\httpd-vhosts.conf”]

## backend
Listen 81
<VirtualHost *:81>

CustomLog "D:\xampp\htdocs\cms_backend\logs\apacheAccess.txt" common
ErrorLog "D:\xampp\htdocs\cms_backend\logs\apacheError.txt"

php_flag display_errors on
php_flag log_errors on
php_value error_log "D:\xampp\htdocs\cms_backend\logs\phpError.txt"
php_value error_reporting 2147483647

DocumentRoot "D:\xampp\htdocs\cms_backend\www"

ServerAdmin [email protected]
ServerName backend.localhost

</VirtualHost>

## frontend
Listen 82
<VirtualHost *:82>

CustomLog "D:\xampp\htdocs\cms_frontend\logs\apacheAccess.txt" common
ErrorLog "D:\xampp\htdocs\cms_frontend\logs\apacheError.txt"

php_flag display_errors on
php_flag log_errors on
php_value error_log "D:\xampp\htdocs\cms_frontend\logs\phpError.txt"
php_value error_reporting 2147483647

DocumentRoot "D:\xampp\htdocs\cms_frontend\www"

ServerAdmin [email protected]
ServerName frontend.localhost

</VirtualHost>[/code]

อธิบาย

Listen
คือหมายเลขพอร์ตที่จะให้รอรับ request อย่าตั้งให้ชนกับตัวอื่น
CustomLog, ErrorLog
คือไฟล์ log ที่แยกออกมาของแต่ละ virtual host โดยสามารถอ่านคู่มือได้จาก Log Files
php_flag, php_value
คือ การปรับแต่ง php เป็นพิเศษสำหรับ server ตัวนี้เท่านั้น
DocumentRoot
คือ folder ที่ไว้เก็บ php ที่เราตั้งใจแยกไว้ เป็น code ชุดเดียวกับที่เราจะเอาขึ้น server จริง
ServerAdmin, ServerName
คือ ข้อมูล server แต่ละตัว

ทดสอบโดย restart apache ใหม่และเปิดเว็บ

PHP: json_encode ไม่มีผลลัพธ์

เจอว่า json_encode มันไม่ return ผลลัพธ์ ออกมาเลยในหน้าเดียวกัน แต่ต่างกันที่ product id ลอง print_r / var_dump ตัว array ที่เข้าไปใน function ก็ปกติดี ไม่ได้มากมาย หรือมีเครื่องหมายพิเศษอะไร ลอง debug ดูถึงเจอว่าถ้าใช้ function json_last_error() มันจะ return เลข 7 กลับมาในหน้าที่มีปัญหา ลองค้นดูรหัส 7 คือ infinity number ลอง unset ตัว key ที่เป็น float(-INF) ดูก็กลับเป็นปกติจริงๆ

รหัสต่างๆ คือ

JSON error codes
Code Constant Meaning Availability
0 JSON_ERROR_NONE No error has occurred  
1 JSON_ERROR_DEPTH The maximum stack depth has been exceeded  
2 JSON_ERROR_STATE_MISMATCH Invalid or malformed JSON  
3 JSON_ERROR_CTRL_CHAR Control character error, possibly incorrectly encoded  
4 JSON_ERROR_SYNTAX Syntax error  
5 JSON_ERROR_UTF8 Malformed UTF-8 characters, possibly incorrectly encoded PHP 5.3.3
6 JSON_ERROR_RECURSION One or more recursive references in the value to be encoded PHP 5.5.0
7 JSON_ERROR_INF_OR_NAN One or moreNANor INFvalues in the value to be encoded PHP 5.5.0
8 JSON_ERROR_UNSUPPORTED_TYPE A value of a type that cannot be encoded was given PHP 5.5.0
9 JSON_ERROR_INVALID_PROPERTY_NAME A property name that cannot be encoded was given PHP 7.0.0
10 JSON_ERROR_UTF16 Malformed UTF-16 characters, possibly incorrectly encoded PHP 7.0.0

[code language=”php” title=”php_json_encode.php”]
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<title>PHP: json_encode error</title>
<meta content="Pitt Phunsanit" name="author" />
</head>
<body>
<div class="container">
<?php
$datas = [
‘ininfity’ => -9e1000,
‘title’ => ‘title’,
];
?>
<div class="row"><label class="col-md-2" for="">Datas Arrya:</label><textarea class="col-md-10" cols="100" rows="6"><?=var_dump($datas); ?></textarea></div>
<div class="row"><label class="col-md-2" for="">Json Datas:</label><textarea class="col-md-10" cols="100" rows="6"><?=json_encode($datas); ?></textarea></div>
<div class="row">
<label class="col-md-2" for="">Json Error:</label>
<div class="col-md-10"><?=json_last_error(); ?></div>
</div>
<table class="table table-striped">
<caption><strong>JSON error codes</strong></caption>
<thead>
<tr>
<th>Code</th>
<th>Constant</th>
<th>Meaning</th>
<th>Availability</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td><strong><code><?=JSON_ERROR_NONE; ?></code></strong></td>
<td><strong><code>JSON_ERROR_NONE</code></strong></td>
<td>No error has occurred</td>
<td class="empty">&nbsp;</td>
</tr>
<tr>
<td><strong><code><?=JSON_ERROR_DEPTH; ?></code></strong></td>
<td><strong><code>JSON_ERROR_DEPTH</code></strong></td>
<td>The maximum stack depth has been exceeded</td>
<td class="empty">&nbsp;</td>
</tr>
<tr>
<td><strong><code><?=JSON_ERROR_STATE_MISMATCH; ?></code></strong></td>
<td><strong><code>JSON_ERROR_STATE_MISMATCH</code></strong></td>
<td>Invalid or malformed JSON</td>
<td class="empty">&nbsp;</td>
</tr>
<tr>
<td><strong><code><?=JSON_ERROR_CTRL_CHAR; ?></code></strong></td>
<td><strong><code>JSON_ERROR_CTRL_CHAR</code></strong></td>
<td>Control character error, possibly incorrectly encoded</td>
<td class="empty">&nbsp;</td>
</tr>
<tr>
<td><strong><code><?=JSON_ERROR_SYNTAX; ?></code></strong></td>
<td><strong><code>JSON_ERROR_SYNTAX</code></strong></td>
<td>Syntax error</td>
<td class="empty">&nbsp;</td>
</tr>
<tr>
<td><strong><code><?=JSON_ERROR_UTF8; ?></code></strong></td>
<td><strong><code>JSON_ERROR_UTF8</code></strong></td>
<td>Malformed UTF-8 characters, possibly incorrectly encoded</td>
<td>PHP 5.3.3</td>
</tr>
<tr>
<td><strong><code><?=JSON_ERROR_RECURSION; ?></code></strong></td>
<td><strong><code>JSON_ERROR_RECURSION</code></strong></td>
<td>One or more recursive references in the value to be encoded</td>
<td>PHP 5.5.0</td>
</tr>
<tr>
<td><strong><code><?=JSON_ERROR_INF_OR_NAN; ?></code></strong></td>
<td><strong><code>JSON_ERROR_INF_OR_NAN</code></strong></td>
<td>
One or more
<a class="link" href="http://php.net/manual/de/language.types.float.php#language.types.float.nan" target="_blank"><strong><code>NAN</code></strong></a>
or <a class="link" href="http://php.net/manual/de/function.is-infinite.php" target="_blank"><strong><code>INF</code></strong></a>
values in the value to be encoded
</td>
<td>PHP 5.5.0</td>
</tr>
<tr>
<td><strong><code><?=JSON_ERROR_UNSUPPORTED_TYPE; ?></code></strong></td>
<td><strong><code>JSON_ERROR_UNSUPPORTED_TYPE</code></strong></td>
<td>A value of a type that cannot be encoded was given</td>
<td>PHP 5.5.0</td>
</tr>
<tr>
<td><strong><code><?=JSON_ERROR_INVALID_PROPERTY_NAME; ?></code></strong></td>
<td><strong><code>JSON_ERROR_INVALID_PROPERTY_NAME</code></strong></td>
<td>A property name that cannot be encoded was given</td>
<td>PHP 7.0.0</td>
</tr>
<tr>
<td><strong><code><?=JSON_ERROR_UTF16; ?></code></strong></td>
<td><strong><code>JSON_ERROR_UTF16</code></strong></td>
<td>Malformed UTF-16 characters, possibly incorrectly encoded</td>
<td>PHP 7.0.0</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>[/code]

laravel / PHP error 500

เจอ error 500 หลังจาก upload งานที่เขียนโดยใช้ laravel ทั้งๆที่เทสในเครื่องแล้ว หาสาเหตุอยู่นานก็ไม่เจอ

การตรวจสอบขั้นต้นที่เจอในเว็บทั่วๆไปคือ ให้ตั้ง permission ใหม่โดย

  • folder ให้เซ็ตเป็น 755 โดย[code type=”text” title=”chmod”]find . -type d -exec chmod 755 {} \;[/code]
  • และตั้งไฟล์เป็น 644[code type=”text” title=”chmod”]find . -type f -exec chmod 644 {} \;[/code]
  • ถ้าไม่หาย อาจะเพราะ user ที่ run apache กับ ftp หรือ เจ้าของไฟล์เป็นคนละ user กัน ทำให้การทำงานทำได้ไม่สมบูรณ์ แก้ได้โดยคำสั่ง chmod -R o+w โดยถ้าเก็บงานไว้ที่ /colume1/web/CMS_linux ก็ใช้[code type=”text” title=”chmod”]chmod -R o+w /colume1/web/CMS_linux[/code]

    ถ้าไม่เข้าใจอ่าน การเปลี่ยนสิทธิอนุญาตในไฟล์หรือไดเร็กทอรี

​ Cannot resolve the collation conflict between​ "Thai_CI_AS" and "Thai_CI_AI"

ดึงข้อมูลจากหลาย database เจอ error

​[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot resolve the collation conflict between “Thai_CI_AS” and “Thai_CI_AI” in the UNION operation.

สาเหตุคือทั้ง 4 database มี table ชื่อเดียวกัน โครงสร้างเหมือนกัน แต่ collation ใช้ ต่างกัน แถมตรวจดูตารางอื่นๆ ก็ใช้กระจัดกระจาย Thai_CI_AI บ้าง Thai_CI_AS บ้าง ดีที่เจอ query ที่แก้ให้ได้ง่ายๆ เอามาดัดแปลงอักนิดให้เรียงตามชื่อตารางก็ใช้ง่ายเลย

[code language=”sql”]
DECLARE @collate SYSNAME
SELECT @collate = ‘Thai_CI_AS’
SELECT
‘[‘ + SCHEMA_NAME(o.[schema_id]) + ‘].[‘ + o.name + ‘] -> ‘ + c.name AS a
, ‘ALTER TABLE [‘ + SCHEMA_NAME(o.[schema_id]) + ‘].[‘ + o.name + ‘]
ALTER COLUMN [‘ + c.name + ‘] ‘ +
UPPER(t.name) +
CASE WHEN t.name NOT IN (‘ntext’, ‘text’)
THEN ‘(‘ +
CASE
WHEN t.name IN (‘nchar’, ‘nvarchar’) AND c.max_length != -1
THEN CAST(c.max_length / 2 AS VARCHAR(10))
WHEN t.name IN (‘nchar’, ‘nvarchar’) AND c.max_length = -1
THEN ‘MAX’
ELSE CAST(c.max_length AS VARCHAR(10))
END + ‘)’
ELSE ”
END + ‘ COLLATE ‘ + @collate +
CASE WHEN c.is_nullable = 1
THEN ‘ NULL’
ELSE ‘ NOT NULL’
END
FROM sys.columns c WITH(NOLOCK)
JOIN sys.objects o WITH(NOLOCK) ON c.[object_id] = o.[object_id]
JOIN sys.types t WITH(NOLOCK) ON c.system_type_id = t.system_type_id AND c.user_type_id = t.user_type_id
WHERE t.name IN (‘char’, ‘varchar’, ‘text’, ‘nvarchar’, ‘ntext’, ‘nchar’)
AND c.collation_name != @collate
AND o.[type] = ‘U’
ORDER BY a
[/code]เอาผลลัพธ์ ใน column ที่ 2 ไป execute อีกครั้งก็ใช้ได้

ขอบคุณ Cr: Devart สำหรับคำตอบใน how to change the collate to all the columns of the database?

บั๊คที่แท้มันคืออะไรตับไตใส้พุง

มี user ถามมาว่า bug คืออะไร ทำไม่มี bug ก็เลยลองเขียนตัวอย่างให้ดู ยกตัวอย่างบัคที่เกิดจาการกรอกข้อมูลผิดไปจากที่ระบบออกแบบและคาดหวังไว้ ก็มีคำถามกลับมาประมาณว่าทำไม่ไม่ตรวจให้หมดละ เลยลองทำตัวอย่างออกมา

[code language=”php”]
<?php

$strings = [
‘!@#$%^&*()_-+="{}[]\+:<>,./?’,
‘Apple Inc.’,
’email.gmail.com’,
’email.job.co.th’,
[email protected]’,
[email protected]’,
‘https://pitt.plusmagi.com/about/’,
‘https://pitt.plusmagi.com/เชื่อมต่อ-php-กับ-sql-server-sqlsrv/’,
‘james bond 007’,
‘johnny english พยัคฆ์ร้าย 00ก๊าก’,
‘pitt@小米科技.cn’,
‘sale 20%’,
‘Xiaomi Inc. (小米科技)’,
‘Xiaomi Inc.’,
‘พิชญ์ พันธุ์สนิท pitt phunsanit’,
‘พิชญ์ พันธุ์สนิท’,
‘พิชญ์@gmail.com’,
‘หนึ่งในพระราชดำริ ช่อง 9’,
‘หนึ่งในพระราชดำริ ช่อง ๙’,
‘ไทย นี่มันไทยจริงๆ’,
];

function validateAlphanumeric($string)
{
if (preg_match(‘/[^A-Za-z0-9]/’, $string)) {
return true;
} else {
return false;
}
}

function validateAlphanumericEnglish($string)
{
if (preg_match(‘/^[a-zA-Z0-9\s]+$/’, $string)) {
return true;
} else {
return false;
}
}

function validateAlphanumericThai($string)
{
if (preg_match(‘/[^A-Za-z0-9-ก-๙]/’, $string)) {
return true;
} else {
return false;
}
}

function validateAlphanumericThaiOnly($string)
{
/* อักษรภาษาไทย และ space */
if (preg_match(‘/^[ก-ฮ\s]+$/’, $string)) {
return true;
} else {
return false;
}
}

function validateEmail($string)
{
if (filter_var($string, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
return false;
}
}

function validateFilterURL($string)
{
if (filter_var($string, FILTER_VALIDATE_URL)) {
return true;
} else {
return false;
}
}

function validatePregURL($string)
{
if (preg_match(‘/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i’, $string)) {
return true;
} else {
return false;
}
}

echo ‘<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP String Validation By Ptii Phunsanit</title>
<meta name="author" content="Pitt Phunsanit">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<table class="table table-striped">
<caption>
PHP String Validation
</caption>
<thead>
<tr>
<th>Test</th>
<th>String</th>
<th colspan="4">Alphabet</th>
<th>Email</th>
<th colspan="2">URL</th>
</tr>
<tr>
<th></th>
<th></th>
<th>Alphanumeric</th>
<th>Alphanumeric English</th>
<th>Alphanumeric Thai</th>
<th>Alphanumeric Thai Only</th>
<th>Email</th>
<th>Filter URL</th>
<th>Preg URL</th>
</tr>
</thead>
<tbody>’;
foreach ($strings as $no => $string) {
echo ‘<tr>’,
‘<th>’, ($no + 1), ‘</th>’,
‘<th>’, $string, ‘</th>’,
‘<td>’, (int) validateAlphanumeric($string), ‘</td>’,
‘<td>’, (int) validateAlphanumericEnglish($string), ‘</td>’,
‘<td>’, (int) validateAlphanumericThai($string), ‘</td>’,
‘<td>’, (int) validateAlphanumericThaiOnly($string), ‘</td>’,
‘<td>’, (int) validateEmail($string), ‘</td>’,
‘<td>’, (int) validateFilterURL($string), ‘</td>’,
‘<td>’, (int) validatePregURL($string), ‘</td>’,
‘</tr>’;
}
echo ‘</tbody></table></body></html>’;[/code]
ผลที่ได้ ดูเต็มๆ เว็บนี้ยังมี bug เลย แต่ไม่คุ้มที่จะแก้
1 คือผ่านการทดสอบ 0 คือ ไม่ผ่านการทดสอบ

PHP String Validation
Test String Alphabet Email URL
Alphanumeric Alphanumeric English Alphanumeric Thai Alphanumeric Thai Only Email Filter URL Preg URL
1 !@#$%^&*()_-+=”{}[]\+:<>,./? 1 0 1 0 0 0 0
2 Apple Inc. 1 0 1 0 0 0 0
3 email.gmail.com 1 0 1 0 0 0 0
4 email.job.co.th 1 0 1 0 0 0 0
5 [email protected] 1 0 1 0 1 0 0
6 [email protected] 1 0 1 0 1 0 0
7 https://pitt.plusmagi.com/about/ 1 0 1 0 0 1 1
8 https://pitt.plusmagi.com/เชื่อมต่อ-php-กับ-sql-server-sqlsrv/ 1 0 1 0 0 0 1
9 james bond 007 1 1 1 0 0 0 0
10 johnny english พยัคฆ์ร้าย 00ก๊าก 1 0 1 0 0 0 0
11 pitt@小米科技.cn 1 0 1 0 0 0 0
12 sale 20% 1 0 1 0 0 0 0
13 Xiaomi Inc. (小米科技) 1 0 1 0 0 0 0
14 Xiaomi Inc. 1 0 1 0 0 0 0
15 พิชญ์ พันธุ์สนิท pitt phunsanit 1 0 1 0 0 0 0
16 พิชญ์ พันธุ์สนิท 1 0 1 1 0 0 0
17 พิชญ์@gmail.com 1 0 1 0 0 0 0
18 หนึ่งในพระราชดำริ ช่อง 9 1 0 1 0 0 0 0
19 หนึ่งในพระราชดำริ ช่อง ๙ 1 0 1 1 0 0 0
20 ไทย นี่มันไทยจริงๆ 1 0 1 1 0 0 0

เริ่มจากชุดแรก Alphanumeric ใช้ preg_match(‘/[^A-Za-z0-9]/’, $string เหมือนจะดูดี ตามคู่มือคือเทียบโดยตัวอักษร a ถึง z และตัวเลข 0 ถึง 9 แต่ขอโทษชุดสตริงค์ แปลกๆ ?!@#$%^&*()_-+=”{}[]\+:<>,./ มันยังผ่าน จนด้วยคำพูดจริงๆ ใส่อะไรก็ผ่าน

ชุดที่ 2 Alphanumeric English ใช้ preg_match(‘/^[a-zA-Z0-9\s]+$/’, $string) ผลคือ ที่มีอักษรไทยอยู่ตกหมด และ james bond 007 ผ่าน แต่ Apple Inc. กับตัวอื่นๆ ตกไปง่ายๆแค่ใส่ . สรุปถ้าเขียนเป็นประโยคมาก็จบชีวิต

ชุดที่ 3 Alphanumeric Thai ใช้ preg_match(‘/[^A-Za-z0-9-ก-๙]/’, $string) รั่วทุกตัวอักษรอีกชุก ถึงจะใส่ ก-๙ มาอักษรจีนก็ยังรอดอยู่ดี

ชุดที่ 4 Alphanumeric Thai Only ใช้ preg_match(‘/^[ก-ฮ\s]+$/’, $string) มันไทยมากจริง อย่าได้ใส่เลขอารบิกมาเชียว บางคนก็ไม่รู้นะครับว่าเลขไทยนะมันพิมพ์ยังไง

ชุดที่ 5 Email ใช้ filter_var($string, FILTER_VALIDATE_EMAIL) ตรวจว่าเป็นอีเมล์จริงๆ รึเปล่า ผ่าน email.gmail.com และ พิชญ์@gmail.com ก็หลอกมันไม่ได้

ชุดที่ 6 ติดใจคำสั่ง filter_var ในข้อที่แล้วใช้ filter_var($string, FILTER_VALIDATE_URL) ผลคือ เกือบจะดีแล้ว ตรวจ url ได้จริงๆ ยกเว้น https://pitt.plusmagi.com/เชื่อมต่อ-php-กับ-sql-server-sqlsrv/ link ของบล๊อคของผมเองยังไม่ผ่าน ปวดตับ แม้แต่ function สำเร็จรูปก็ไม่ได้ดีเสมอตามที่คิดว่า “ใช้กันมากเป็นมาตราฐานมันต้องดีกว่าเขียนเองซิ” function filter_var ของ PHP มันออกมาตั้งแต่ 2 November 2006 ก็แค่ไม่กี่ปีเองมั๊ง ปีนี้ ค.ศ. อะไรแล้ว แต่มันก็ยังไม่รับภาษาอื่นนอกจากภาษาอังกฤษอยู่เหมือนเดิม ไม่ใช่ไม่มีคนแจ้งไปที่คนเขียนภาษา php นะ ตามนี้ แต่พี่แกก็ยังไม่แก้แค่นั้นเอง

ชุดที่ 7 ใช้ preg_match(‘/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i’, $string) ยาวยืดอย่าพิมพ์ผิดเชียว ผมก็ก๊อบเค้ามา แต่ตรวจ url ได้ถูกต้อง

ที่ทำตัวอย่างให้ดูสรุปคือ ในการเขียนโปรแกรมไม่มีอะไรที่ทำงานได้ดี 100% เต็ม ทำอย่างหนึ่งจะมีการแลกเปลี่ยนที่เท่าเทียม (รึเปล่า) ตามมาด้วยเสมอ อย่างตรวจ link แบบชุดที่ 6 จริงๆ มันตรวจว่าเป็นลิงค์เว็บจริงๆ มาได้หลายปีแล้ว แต่เว็บหลังๆ เน้นทำ SEO กับท้องถิ่นตามภาษาและประเทศมากขึ้นแทนที่จะใช้ภาษาอังกฤษอย่างเดียว ตอนนี้เลยต้องมาเขียนตัวตรวจสอบกันแบบยาวๆ และปวดตับมากกกก

การที่กว่าจะได้งานที่สมบูรณ์ที่สุดไกล้ความสมบูรณ์แบบถึงต้องใช้พลังและความร่วมมือจากทุกๆคน และมีค่าใช้จ่ายและเวลาที่สูงมาก ผลคือ การลดค่าใช้จ่ายอย่าง

  • บอกมานิดเดียว น้องไปคิดต่อเอาเองนะ พี่มางานอื่น (พี่ครับ ผมต้องเขียน business plan ต้องมาคิดให้ว่าบริษัทพี่นี่ มันขายอะไร ต้องทำยัง บริการอะไรบ้าง และมันทำงานยังไง สุดท้ายก็ออกมาขาดๆ เกินๆ)
  • ไม่จ้าง tester ครับ programmer เขียนเสร็จก็ต้องมาลองเอง เพราะว่าออกแบบและเขียนเองมากะมือ มันเลยจำติดอยู่ในสมองว่า ตรงนี้ต้องกรอกอะไร ตอนทดสอบ ก็เผลอทำแบบเดิม ทำมาให้กรอกตัวเลข ต้อนทดสอบก็ใส่ 1555, 5544, 444, 85 มัวๆไป ตอนให้ user ทำลองใช้ มีคนใส่โง่ๆ “100 บาท” เกิดอะไรขึ้น
    1. โปรแกรมก็เอา 100 บาท ไปใส่ในสูตร
      จำนวนบาท คูณจำนวน ก็ออกมาเป็น
      100บาท x 5 = ?
      แล้วก็บอกกลับมาว่า พ่องมึง คูณได้เหรอวะ แต่มันบอกมาแบบที่มีแต่โปรแกรมเมอร์ที่เข้าใจ แลัวฆ่าตัวตายไปต่อหน้าต่อตา
    2. บัญชีก็บอกว่า นี่ทำเสร็จแล้วเหรอ เทสแล้วแน่นะ
    3. คนเขียนเห็นก็อยากจะบอกว่า ข้างหลังใส่ label บาทให้ละ จะกรอกไปทำซากอะไร
    4. ทีนี้เกินอะไรขึ้นต่อไปในการประชุม
      • project manager ก็บอกว่านิดหนึ่งพี่ แก้ไม่ถึง 5 นาทีก็เสร็จแล้วครับ
      • programmer คิด มึงเคยถามกูยัง ทำไงต่อ ทำนานปะ แก้แล้วตรงไหนจะพังมั่ย แล้วอย่างอื่นมันจะทันป่าว
      • บัญชีของลูกค้าก็คิด ขี้เกียจเทสแน่ๆ เมื่อไหร่จะได้ใช้
      • เซลล์ก็คิด ทำไม่ไม่เอาใบส่งสินค้าที่เขียนกับมือ เข้าเครื่องถ่ายเอกสาร แล้วได้รายงานออกมาเลย ไม่เสียเวลา เอาไปขายให้ที่ไหนมีแต่คนอยากซื้อ ทำไม่ไม่ยอมทำ จะได้ขายง่ายๆ ซักที % จะได้เยอะๆ อุสาห์คิดให้ดีๆ ไม่เข้าใจ
      • ลูกค้า ก็คิด รู้งี้ยอมจ่ายอีกนิดจ้างอีกเจ้า ยอมไม่ไปเที่ยวเมืองนอกก็ได้ จะได้ทำอย่างอื่นซะที
    5. จ้างมาแพง ต้องรีบๆทำงาน ให้ได้ function ผลคือไม่ได้ test งานจริงๆ หรือให้มันใช้งานได้ง่าย อย่างงานที่เคยเจอต้องมีคนมา เอารูปถ่ายสินค้ามาตัดให้พอดีกับขนาดที่ใช้ในเว็บ ทั้งๆที่ ก็ถ่ายโดยกล้องเดิม ขนาดเดิม และมุมกล้องเดิมทุกวัน ทุกชิ้น ถ้ายอมให้เวลาเขียนโปรแกรมเพิ่มอีกนิด ก็แค่วางของที่เดิม กดถ่ายรูปแล้วโยนให้โปรแกรมมันตัดรูป เปลี่ยนขนาด แล้วเอาให้ลูกค้าดูเอง ประหยัดเวลาในชีวิตออกไปได้เยอะเลยแท้ๆ แต่พี่รีบ พี่ไม่อยากจ่ายเงินเพิ่ม

มันก็เหมือนคำพูดที่ว่า ของดีไม่แพง นั้นละครับ ยอมจ่ายได้แค่ไหน คนขายก็ประมาณ ต่อกันอย่างนี้ ขอฟรีเลยมั๋ยพี่ ที่สำคัญคือ จะหาจุดสมดุลย์ได้ที่ไหน ถึงจะพอใจกันทั้งสองฝ่าย

แก้ validation error ใน YII 2

ฟอร์มของ yii จะมีการแจ้งเตือนถ้าหากพบว่าข้อมูล input ที่เรากรอกใน form ไม่ถูกต้อง โดยจะทำกรอบอินพุต ป้าย label เป็นสีแดงและมีข้อความแสดงใน help-block เพิ่มขึ้นมา บางครั้งก็ทำให้ฟอร์มที่จัดไว้แน่นๆ ไม่ใช่แบบบรรทัดละกล่องข้อความตามแบบเว็บสมัยใหม่ เวลาเจอความผิดพลาด มันก็จะถีบตัวอื่นออกไป หรือดูอัดแน่นเกินไปจนดูไม่สวย

ก็สามารถเอาออกได้โดยใช้ form template เหมือนเดิม โดยยังสามารถทำ validation ได้ตามปกติ

วิธีการคือ

  1. เปิดไฟล์ _form.php ใน view เป้าหมาย
  2. เปลี่ยน
    [code language=”php”]
    <?php $form = ActiveForm::begin([‘id’ => ‘login-form’]); ?>
    [/code]เป็น
    [code language=”php”]
    <?php $form = ActiveForm::begin([
    ‘fieldConfig’ => [
    ‘template’ => "{label}\n{input}\n{hint}"
    ],
    ‘id’ => ‘login-form’
    ]); ?>
    [/code]

อย่าลืมเปลี่ยน id นะครับ

ถ้าใช้[code language=”php”]use yii\bootstrap\ActiveForm;[/code]ให้เปลี่ยน template เป็น[code language=”php”]’template’ => "{label}\n{beginWrapper}\n{input}\n{hint}\n{endWrapper}"[/code]

google maps จาก jQuery ajax

ใช้เจคิวรี่เอแจ็กซ์ โหลด fragment google map เข้ามาแสดงในเท็ปแล้วไม่ขึ้น ไม่เห็น ทั้งๆที่ก็เขียน code ตามปกติ ดูตรงๆ ไม่ผ่านเอเจ็กซ์ ก็ทำงานปกติ ให้เปลี่ยนมาใช้ HTML fragment code ตามนี้ครับ
[code language=”html”]
<div id="map-canvas" style="width: 100$; height: 400px"></div>
<script type="text/javascript">
function initialize()
{
var mapOptions = {
center: {lat: 13.799604, lng: 100.51129},
zoom: 16
};
var map = new google.maps.Map(document.getElementById(‘map-canvas’),
mapOptions);
}

$(function()
{
$.getScript("https://maps.googleapis.com/maps/api/js?key=ใส่ google map API key ตรงนี้&async=2&callback=initialize", function () {});
});
</script>
[/code]

Javascript Debugging

Code สั้นๆ ไว้แสดง message เวลา javascript มี error หรือจะดัดแปลงให้ส่ง error กลับไปที่ server ผ่าน ajax ก็ได้ครับ
[sourcecode language=”html”]
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>javascript onerror</title>
</head>

<body>
<script>
window.onerror = function (msg, url, line) {
str = "Message :tt"+msg+"nUrl :ttt"+url+"nLine number :t"+line;
alert(str);
}

/* ตัวอย่างไปเรียก function ที่ไม่มีจริง */
noBody();
</script>
</body>
</html>
[/sourcecode]เรื่องที่เกี่ยวข้อง

jQuery Ajax Debugging

ข้อเสียอย่างหนึ่งเวลาใช้ Ajax คือ เวลามี error อะไร user จะไม่ทราบอะไรเลย เพราะว่าเป็นการโหลดแบบเบื้องหลัง บางทีโหลดเกือบจะเสร็จแล้ว แต่เห็นหน้านิ่งๆ เลย refresh ใหม่ซะเลย ความจริง jQuery มี event onErrors ไว้สำหรับจัดการเวลาใช้ Ajax แล้วมีปัญหาอยู่แล้ว แต่การที่จะต้องมาเขียนกำหนดให้ทุกตัวที่เรียกใช้ Ajax ก็ไม่สดวกเลย จึงมี $.ajaxSetup ไว้ให้สามารถกำหนดคุณสมบัติที่เป็นส่วนกลางให้คำสั่ง Ajax ต่างๆ ทั้ง $.Ajax, $.get, $.json, $.post ใช้ร่วมกัน แค่ประกาศใช้ $.ajaxSetup เอาไว้ล่วงหน้า
[sourcecode language=”html”]
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery.ajaxSetup</title>
</head>

<body>
<script src="assets/jQuery/jquery.min.js"></script>
<script>
$.ajaxSetup({
"cache": false,
"error": function(jqXHR, exception)
{
if(jqXHR.status === 0)
{
alert(‘Not connect.n Verify Network.’);
}
else if(jqXHR.status == 404)
{
alert(‘Requested page not found. [404]’);
}
else if(jqXHR.status == 500)
{
alert(‘Internal Server Error [500].’);
}
else if(exception === ‘parsererror’)
{
alert(‘Requested JSON parse failed.’);
}
else if(exception === ‘timeout’)
{
alert(‘Time out error.’);
}
else if(exception === ‘abort’)
{
alert(‘Ajax request aborted.’);
}
else
{
alert(‘Uncaught Error.n’ + jqXHR.responseText);
}
},
"scriptCharset": "utf-8",
"timeout": 60000
});

$.post(‘nofile.php’, function(data){
alert(data);
});
</script>
</body>
</html>
[/sourcecode]
เรื่องที่เกี่ยวข้อง