Skip to content

Commit 8ec5207

Browse files
committed
Fixes bug in the validation rules
1 parent e42b45c commit 8ec5207

File tree

5 files changed

+158
-31
lines changed

5 files changed

+158
-31
lines changed

config/codegenerator.php

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
|
1010
| Here you change the stub templates to use when generating code.
1111
| You can duplicate the 'default' template folder
12-
| and call it what ever template your like 'ex. skyblue'.
12+
| and call it what ever template name you like 'ex. skyblue'.
1313
| Now, you can change the stubs to have your own templates generated.
1414
|
1515
|
1616
| IMPORTANT: It is not recomended to modify the default template, rather create a new template.
1717
| If you modify the default template and then executed 'php artisan vendor:publish' command,
18-
| it will override the default template causing you to lose your modification.
18+
| will override your changes!
1919
|
2020
*/
2121
'template' => 'default',
@@ -38,6 +38,8 @@
3838
| If you want to generate code by using laravel-collective, you must first
3939
| install the package. Then add the tamplate name that should be using
4040
| Laravel-Collective extensions when generating code.
41+
|
42+
|
4143
*/
4244
'laravel_collective_templates' => [
4345
'default-collective'
@@ -57,13 +59,15 @@
5759
| The default output format for datetime fields.
5860
|--------------------------------------------------------------------------
5961
|
62+
| This output format can also be changed at the field level using the
63+
| "date-format" property of the field.
6064
|
6165
*/
6266
'datetime_out_format' => 'm/d/Y H:i A',
6367

6468
/*
6569
|--------------------------------------------------------------------------
66-
| The default path of where the field json files are located.
70+
| The default path of where the json field files are located.
6771
|--------------------------------------------------------------------------
6872
|
6973
| In this path, you can create json file to import the fields from.
@@ -108,7 +112,7 @@
108112

109113
/*
110114
|--------------------------------------------------------------------------
111-
| The name of the default resource mapper.
115+
| The name of the default resources map file.
112116
|--------------------------------------------------------------------------
113117
*/
114118
'default_mapper_file_name' => 'resources_map.json',
@@ -136,11 +140,27 @@
136140
| placeholders for the html code. Here you can define which html-types should
137141
| the generator create placeholder for. Also, you can define how would you like
138142
| the text to read when no placeholder is assigned.
143+
|
144+
| The follwowing templates can be used to. assuming the field name is owner_name
145+
| [% field_name %] <=> "owner name"
146+
| [% field_name_sentence %] <=> "Owner name"
147+
| [% field_name_plural %] <=> "owner names"
148+
| [% field_name_plural_title %] <=> "Owner Names"
149+
| [% field_name_snake %] <=> "owner_name"
150+
| [% field_name_studly %] <=> "OwnerName"
151+
| [% field_name_slug %] <=> "owner-name"
152+
| [% field_name_kebab %] <=> "owner-name"
153+
| [% field_name_title %] <=> "Owner Name"
154+
| [% field_name_title_upper %] <=> "OWNER NAME"
155+
| [% field_name_plural_variable %] <=> "ownerNames"
156+
| [% field_name_singular_variable %] <=> "ownerName"
139157
*/
140158
'placeholder_by_html_type' =>
141159
[
142160
'text' => 'Enter [% field_name %] here...',
143161
'number' => 'Enter [% field_name %] here...',
162+
'password' => 'Enter [% field_name %] here...',
163+
'email' => 'Enter [% field_name %] here...',
144164
'select' => 'Select [% field_name %]',
145165
],
146166

@@ -151,7 +171,7 @@
151171
|
152172
| When creating resources from existing database, the codegenerator scans
153173
| the field's name for a mattching pattern. When found, these field are considred
154-
| foreign keys even if the database does not have a foreign constraints.
174+
| foreign keys even when the database does not have a foreign constraints.
155175
| Here you can specify patterns to help the generator understand your
156176
| database naming convension.
157177
|
@@ -163,10 +183,10 @@
163183
| Patterns to use to pre-set field's properties.
164184
|--------------------------------------------------------------------------
165185
|
166-
| To make constructing fields easy, the codegenerator scans the field's name
167-
| for a matching pattern. If the name matches any of the set pattern, the the
168-
| field's properties will be preset. defining pattern will save you from having
169-
| to re-define the properties for common fields.
186+
| To make constructing fields easy, the code-generator scans the field's name
187+
| for a matching pattern. If the name matches any of these patterns, the
188+
| field's properties will be set accordingly. Defining pattern will save
189+
| you from having to re-define the properties for common fields.
170190
|
171191
*/
172192
'common_definitions' => [
@@ -201,12 +221,24 @@
201221
]
202222
],
203223
[
204-
'match' => ['picture','file'],
224+
'match' => ['picture','file','photo'],
205225
'set' => [
206226
'is-on-index' => false,
207227
'html-type' => 'file',
208228
]
209229
],
230+
[
231+
'match' => ['*password*'],
232+
'set' => [
233+
'html-type' => 'password'
234+
]
235+
],
236+
[
237+
'match' => ['*email*'],
238+
'set' => [
239+
'html-type' => 'email'
240+
]
241+
],
210242
[
211243
'match' => ['*_id','*_by'],
212244
'set' => [
@@ -295,6 +327,32 @@
295327
| The "in-function-with-collective" key of the sub array, tell the generator that,
296328
| this string would be used in a function or not.
297329
|
330+
| The follwowing templates can be used. Assuming the model name is AssetCategory
331+
| [% model_name %] <=> "asset category"
332+
| [% model_name_sentence %] <=> "Asset category"
333+
| [% model_name_plural %] <=> "asset categories"
334+
| [% model_name_plural_title %] <=> "Asset Category"
335+
| [% model_name_snake %] <=> "asset_category"
336+
| [% model_name_studly %] <=> "AssetCategory"
337+
| [% model_name_slug %] <=> "asset-category"
338+
| [% model_name_kebab %] <=> "asset-category"
339+
| [% model_name_title %] <=> "Asset Category"
340+
| [% model_name_title_upper %] <=> "ASSET CATEGORY"
341+
| [% model_name_plural_variable %] <=> "assetCategories"
342+
| [% model_name_singular_variable %] <=> "assetCategory"
343+
|
344+
| ~Example
345+
| Lets say we need to add a new template in our views that reads the following
346+
| "Creating resources for ... was a breeze!"
347+
| The following entry can be added to the below array
348+
|
349+
| 'custom_template_1' => [
350+
| 'text' => 'Creating resources for [% model_name %] was a breeze!',
351+
| 'template' => 'custom_template_example',
352+
| ],
353+
| Finally, add [% custom_template_example %] in the view where you want it to appear!
354+
|
355+
|
298356
*/
299357
'generic_view_labels' => [
300358
'create' => [

src/Models/Field.php

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,6 @@ public function toArray()
682682
'is-unsigned' => $this->isUnsigned,
683683
'is-auto-increment' => $this->isAutoIncrement,
684684
'is-inline-options' => $this->isInlineOptions,
685-
'is-multiple-answers' => $this->isMultipleAnswers,
686685
'is-date' => $this->isDate,
687686
'date-format' => $this->dateFormat,
688687
'cast-as' => $this->castAs,
@@ -786,6 +785,26 @@ public function isTime()
786785
return in_array($this->dataType, ['time','timeTz']);
787786
}
788787

788+
/**
789+
* Checks if the data type is numeric.
790+
*
791+
* @return bool
792+
*/
793+
public function isNumeric()
794+
{
795+
return $this->isDecimal() || in_array($this->dataType, ['bigIncrements','bigInteger','increments','integer','mediumIncrements','mediumInteger','smallIncrements','smallInteger','tinyInteger','unsignedBigInteger','unsignedInteger','unsignedMediumInteger','unsignedSmallInteger','unsignedTinyInteger']);
796+
}
797+
798+
/**
799+
* Checks if the data type string
800+
*
801+
* @return bool
802+
*/
803+
public function isString()
804+
{
805+
return in_array($this->dataType, ['char','string']);
806+
}
807+
789808
/**
790809
* Checks if the data type is time stamp.
791810
*
@@ -881,29 +900,52 @@ public function getFalseBooleanOption()
881900
}
882901

883902
/**
884-
* Checks if the data type is numeric.
903+
* Get the total decimal point
885904
*
886-
* @return bool
905+
* @return int
887906
*/
888-
public function isNumeric()
907+
public function getDecimalPointLength()
889908
{
890-
return $this->isDecimal() || in_array($this->dataType, ['bigIncrements','bigInteger','increments','integer','mediumIncrements','mediumInteger','smallIncrements','smallInteger','tinyInteger','unsignedBigInteger','unsignedInteger','unsignedMediumInteger','unsignedSmallInteger','unsignedTinyInteger']);
909+
if ($this->isDecimal() && ! is_null($value = $this->getMethodParam(1))) {
910+
return $value / ($value * 100);
911+
}
912+
913+
return 0;
891914
}
892915

893916
/**
894-
* Get the total decimal point
917+
* Gets the Min length a field.
895918
*
896-
* @return int
919+
* @return int|null
897920
*/
898-
public function getDecimalPointLength()
921+
public function getMinLength()
899922
{
900-
if ($this->isDecimal() && ! is_null($value = $this->getMethodParam(1))) {
901-
return $value / ($value * 100);
923+
if($this->isString() && isset($this->methodParams[0])) {
924+
return intval($this->methodParams[0]);
902925
}
903926

904927
return 0;
905928
}
906929

930+
/**
931+
* Gets the Min length a field.
932+
*
933+
* @return int|null
934+
*/
935+
public function getMaxLength()
936+
{
937+
if($this->isString() && isset($this->methodParams[0]) ) {
938+
939+
if(isset($this->methodParams[1])) {
940+
return intval($this->methodParams[1]);
941+
}
942+
943+
return intval($this->methodParams[0]);
944+
}
945+
946+
return null;
947+
}
948+
907949
/**
908950
* Gets the maximum value a field can equal.
909951
*

src/Support/FieldOptimizer.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ class FieldOptimizer
4848
'uuid'
4949
];
5050

51-
51+
/**
52+
* Create a new optemizer instance.
53+
*
54+
* @param CrestApps\CodeGenerator\Models\Field $field
55+
* @param array $meta
56+
*
57+
* @return void
58+
*/
5259
public function __construct(Field $field, array $meta = null)
5360
{
5461
$this->field = $field;

src/Support/FieldTransformer.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class FieldTransformer
9191
'checkbox',
9292
'radio',
9393
'number',
94-
'date',
94+
//'date',
9595
'select',
9696
'multipleSelect',
9797
'textarea',
@@ -747,28 +747,41 @@ protected function setValidationProperty(Field & $field, array $properties)
747747
$field->validationRules = is_array($properties['validation']) ? $properties['validation'] : Helpers::removeEmptyItems(explode('|', $properties['validation']));
748748
}
749749

750-
if (Helpers::isNewerThan('5.2') && $field->isNullable) {
750+
if (Helpers::isNewerThan('5.2') && $field->isNullable && !in_array('nullable', $field->validationRules)) {
751751
$field->validationRules[] = 'nullable';
752752
}
753753

754754
if ($field->isBoolean() && !in_array('boolean', $field->validationRules)) {
755755
$field->validationRules[] = 'boolean';
756756
}
757757

758+
if ($field->isFile() && !in_array('file', $field->validationRules)) {
759+
$field->validationRules[] = 'file';
760+
}
761+
if ($field->isMultipleAnswers && !in_array('array', $field->validationRules)) {
762+
$field->validationRules[] = 'array';
763+
}
764+
765+
if (in_array($field->dataType, ['char','string']) && in_array($field->htmlType, ['text','textarea']) ) {
766+
if (!in_array('string', $field->validationRules)) {
767+
$field->validationRules[] = 'string';
768+
}
769+
770+
if (!$this->inArraySearch($field->validationRules, 'min')) {
771+
$field->validationRules[] = sprintf('min:%s', $field->getMinLength());
772+
}
773+
774+
if (!$this->inArraySearch($field->validationRules, 'max') && !is_null($field->getMaxLength()) ) {
775+
$field->validationRules[] = sprintf('max:%s', $field->getMaxLength());
776+
}
777+
}
778+
758779
$params = [];
759780

760781
if ($this->isKeyExists($properties, 'data-type-params')) {
761782
$params = $this->getDataTypeParams($field->dataType, (array) $properties['data-type-params']);
762783
}
763784

764-
if (in_array($field->dataType, ['char','string'])
765-
&& isset($params[0]) && ($length = intval($params[0])) > 0) {
766-
if (!$this->inArraySearch($field->validationRules, 'between')) {
767-
$min = $field->isRequired() ? 1 : 0;
768-
$field->validationRules[] = sprintf('between:%s,%s', $min, $length);
769-
}
770-
}
771-
772785
if ( $field->htmlType == 'number' || (in_array($field->dataType, ['decimal','double','float'])
773786
&& isset($params[0]) && ($length = intval($params[0])) > 0
774787
&& isset($params[1]) && ($decimal = intval($params[1])) > 0) )

src/Support/FieldsOptimizer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class FieldsOptimizer
2424
*/
2525
protected $fields = [];
2626

27+
/**
28+
* Create a new optemizer instance.
29+
*
30+
* @param array $mappers
31+
*
32+
* @return void
33+
*/
2734
public function __construct(array $mappers)
2835
{
2936
$this->mappers = $mappers;

0 commit comments

Comments
 (0)