Check code in some programming languages for syntax errors.
Currently supported languages:
Feel free to let me know what else you want added via:
The preferred way to install this extension is through composer.
Either run
$ php composer.phar require ancor/yii2-code-syntax
or add
"ancor/yii2-code-syntax": "dev-master"
to the require
section of your composer.json
file.
Validation occurs as follows:
- Get the data from the field model
- Wrapping wrapper
- Add at the start
EXPLAIN
- Execute sql query
- If the returned error, add this errors to validation message
use ancor\codeSyntax\SqlSyntaxValidator;
public function rules()
{
return [
[['sqlCodeField'], SqlSyntaxValidator::className()],
];
}
use ancor\codeSyntax\SqlSyntaxValidator;
public function rules()
{
return [
[
['sqlCodeField'],
SqlSyntaxValidator::className(),
// 'wrapper' => '...{{part}}...',
// 'makeSql' => function($validator, $partSql) { ... }
'message' => 'Field {attribute} is invalid',
],
];
}
Warning: this validator use php cli. And if php has been not added to $PATH, validator will not work.
use ancor\codeSyntax\PhpSyntaxValidator;
public function rules()
{
return [
[['phpCodeField'], PhpSyntaxValidator::className()],
];
}
use ancor\codeSyntax\PhpSyntaxValidator;
public function rules()
{
return [
[
['phpCodeField'],
PhpSyntaxValidator::className(),
'isWrapPhp' => true,
'message' => 'Field {attribute} is invalid',
],
];
}
Warning: this validator use json_decode()
php function. And depends on php-json extension.
use ancor\codeSyntax\JsonSyntaxValidator;
public function rules()
{
return [
[['jsonCodeField'], JsonSyntaxValidator::className()],
];
}
use ancor\codeSyntax\JsonSyntaxValidator;
public function rules()
{
return [
[
['jsonCodeField'],
JsonSyntaxValidator::className(),
'message' => 'Field {attribute} has invalid json. Code: {errCode}, Msg: {errMsg}',
],
];
}