-
-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
generate the form attributes based on validations #70
Open
Faryshta
wants to merge
4
commits into
yiisoft:master
Choose a base branch
from
Faryshta:patch-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2d1cdbc
assign each safe attribute a validator by weight
Faryshta 2918842
switch to generate the fields by validator
Faryshta 7f3bd0c
Merge branch 'master' of git://github.com/yiisoft/yii2-gii into patch-3
Faryshta aee758b
accomodate methods in generator and not on view
Faryshta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,10 @@ | |
use yii\db\BaseActiveRecord; | ||
use yii\db\Schema; | ||
use yii\gii\CodeFile; | ||
use yii\helpers\ArrayHelper; | ||
use yii\helpers\Inflector; | ||
use yii\helpers\VarDumper; | ||
use yii\validators\Validator; | ||
use yii\web\Controller; | ||
|
||
/** | ||
|
@@ -548,4 +550,133 @@ public function getColumnNames() | |
return $model->attributes(); | ||
} | ||
} | ||
|
||
public function validatorPriority(Validator $validator) | ||
{ | ||
static $priorities = [ | ||
'yii\validators\NumberValidator' => 0, | ||
'yii\validators\EmailValidator' => 1, | ||
'yii\validators\BooleanValidator' => 2, | ||
'yii\validators\RangeValidator' => 3, | ||
'faryshta\\validators\\EnumValidator' => 4, | ||
'yii\validators\DateValidator' => 5, | ||
'yii\validators\CapchaValidator' => 6, | ||
'yii\validators\FileValidator' => 7, | ||
'yii\validators\ImageValidator' => 8, | ||
'yii\validators\ExistValidator' => 9, | ||
]; | ||
|
||
return ArrayHelper::getValue($priorities, $validator->className(), -1); | ||
} | ||
|
||
public function generateValidatorField($attribute, $validator) | ||
{ | ||
// space tab | ||
static $t = [ | ||
'', | ||
' ', | ||
' ', | ||
' ', | ||
' ', | ||
]; | ||
|
||
// code to generate each attribute based on the validators saved sofar. | ||
switch ($validator->className()) { | ||
|
||
case 'yii\validators\NumberValidator': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of hardcoding we can introduce something like |
||
return "\$form->field(\$model, '$attribute')->input('number')"; | ||
break; | ||
|
||
case 'yii\validators\EmailValidator': | ||
return "\$form->field(\$model, '$attribute')->input('email')"; | ||
break; | ||
|
||
case 'yii\validators\BooleanValidator': | ||
return "\$form->field(\$model, '$attribute')->checkbox()"; | ||
break; | ||
|
||
case 'yii\validators\RangeValidator': | ||
$field = "\$form->field(\$model, '$attribute')->dropDownList(\n" | ||
. "$t[2][\n"; | ||
|
||
foreach ($validator->range as $key) { | ||
$field .= "{$t[3]}'$key' => '$key',\n"; | ||
} | ||
|
||
return $field . "{$t[2]}],\n" | ||
. "{$t[2]}['prompt' => " | ||
. "\$model->getAttributeLabel('$attribute')]\n" | ||
. "{$t[1]})"; | ||
break; | ||
|
||
case 'faryshta\validators\EnumValidator': | ||
$params = ''; | ||
if (!empty($validator->enumClass)) { | ||
$params .= "'enumClass' => '{$validator->enumClass}',"; | ||
} | ||
if (!empty($validator->enumName)) { | ||
$params .= "'enumName' => '{$validator->enumName}',"; | ||
} | ||
if (!empty($params)) { | ||
$params = ",\n{$t[2]}[$params]"; | ||
} | ||
|
||
return "\$form->field(\$model, '$attribute')->widget(\n" | ||
. "{$t[2]}'faryshta\\widgets\\EnumDropdown'$params\n" | ||
. "{$t[1]})"; | ||
break; | ||
|
||
case 'yii\validators\DateValidator': | ||
return "\$form->field(\$model, '$attribute')->widget(\n" | ||
. "{$t[2]}'yii\jui\Datepicker',\n" | ||
. "{$t[2]}['dateFormat' => '{$validator->format}'],\n" | ||
. "{$t[1]})"; | ||
break; | ||
|
||
case 'yii\validators\CapchaValidator': | ||
return "\$form->field(\$model, '$attribute')->widget('yii\captcha\Captcha')"; | ||
break; | ||
|
||
case 'yii\validators\FileValidator': | ||
case 'yii\validators\ImageValidator': | ||
return "\$form->field(\$model, '$attribute')->fileInput()"; | ||
break; | ||
|
||
case 'yii\validators\ExistValidator': | ||
$targetAttribute = empty($validator->targetAttribute) | ||
? $attribute | ||
: $validator->targetAttribute; | ||
|
||
// if the attribute is an array for example ['id' => 'user_id'] | ||
// then the first key will be used as targetAttribute | ||
if (is_array($targetAttribute)) { | ||
$targetAttribute = array_keys($targetAttribute); | ||
$targetAttribute = $targetAttribute[0]; | ||
} | ||
|
||
$targetClass = empty($validator->targetClass) | ||
? $model->className() | ||
: $validator->targetClass; | ||
|
||
$field = "\$form->field(\$model, '$attribute')->dropDownList(\n" | ||
. "{$t[2]}$targetClass::find()\n" | ||
. "{$t[3]}->select(['$targetAttribute'])\n"; | ||
|
||
if (is_array($validator->filter)) { | ||
$field .= "{$t[3]}->andWhere([\n"; | ||
foreach ($validator->filter as $key => $val) { | ||
$field .= "{$t[4]}'$key' => '$val',\n"; | ||
} | ||
$field .= "{$t[3]}])\n"; | ||
} | ||
|
||
echo "{$field}{$t[3]}->indexBy('$targetAttribute')\n" | ||
. "{$t[3]}->column(),\n" | ||
. "{$t[2]}['prompt' => " | ||
. "\$model->getAttributeLabel('$attribute')]\n" | ||
. "{$t[1]})"; | ||
break; | ||
default: return $validator->className(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does it do in the core? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i will take it out then. I was also thinking on a way to do it extendable so plugins could create their generators on gii without making an entire new gii.
any idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make it non-static then and move it to module level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't understand this tip. can you explain further?
specially on how to let other modules or libraries define their own associated widgets for the validators they define such as my enum validator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've suggested taking
static $priorities
out from the function to the class level i.e.public $validators
or something like that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way it could be configured externally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it doesn't really make sense to specify priorities as integers. Order of values in array should be enough.