Skip to content

Commit

Permalink
Fixes a bug when using relations in the form view
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Alhayek committed Jul 8, 2017
1 parent 991b2b2 commit 318e0fe
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "crestapps/laravel-code-generator",
"license": "MIT",
"description": "A clean code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages or request-forms! It is extremely flexible and customizable to cover many use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application.",
"version": "v2.1.1",
"version": "v2.1.2",
"keywords": [
"laravel","crud","crud generator",
"laravel crud generator","laravel crud builder",
Expand Down
2 changes: 1 addition & 1 deletion config/codegenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
]
],
[
'match' => ['*_date'],
'match' => ['*_date','date_*'],
'set' => [
'data-type' => 'date',
'date-format' => 'm/d/Y',
Expand Down
16 changes: 4 additions & 12 deletions src/CodeGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,22 @@

class CodeGeneratorServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__ . '/../config/codegenerator.php' => config_path('codegenerator.php'),
], 'default');
$dir = __DIR__ . '/../';

$this->publishes([
__DIR__ . '/../templates/default' => base_path('resources/codegenerator-templates/default'),
$dir . 'config/codegenerator.php' => config_path('codegenerator.php'),
$dir . 'templates/default' => base_path('resources/codegenerator-templates/default'),
], 'default');

$this->publishes([
__DIR__ . '/../templates/default-collective' => base_path('resources/codegenerator-templates/default-collective'),
$dir . 'templates/default-collective' => base_path('resources/codegenerator-templates/default-collective'),
], 'default-collective');

$this->createDirectory(base_path('resources/codegenerator-files'));
Expand Down
31 changes: 23 additions & 8 deletions src/HtmlGenerators/HtmlGeneratorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,11 @@ protected function getPickItemsHtmlField(Field $field, Label $option, Validation
$filename = sprintf('form-pickitems%s-field.blade', $field->isInlineOptions ? '-inline' : '');
$stub = $this->getStubContent($filename, $this->template);
$fieldName = ($field->isMultipleAnswers) ? $this->getFieldNameAsArray($field->name) : $field->name;

$checkedItem = $this->getCheckedItemForPickItem($option->value, $field->name, $field->isMultipleAnswers, $field->htmlValue);
$this->replaceFieldType($stub, $field->htmlType)
->replaceFieldName($stub, $fieldName)
->replaceOptionValue($stub, $option->value)
->replaceCheckedItem($stub, $this->getCheckedItemForPickItem($option->value, $field->name, $field->isMultipleAnswers, $field->htmlValue))
->replaceCheckedItem($stub, $checkedItem)
->replaceItemId($stub, $option->id)
->replaceFieldRequired($stub, ($field->htmlType == 'checkbox') ? false : $parser->isRequired())
->replaceCssClass($stub, $field->cssClass)
Expand Down Expand Up @@ -468,6 +468,13 @@ protected function getSelectHtmlField(Field $field, ValidationParser $parser)
return $stub;
}

/**
* Gets the fields accessor
*
* @param CrestApps\CodeGeneraotor\Support\Field $field
*
* @return string
*/
protected function getFieldItem(Field $field)
{
if ($field->hasForeignRelation()) {
Expand All @@ -479,6 +486,13 @@ protected function getFieldItem(Field $field)
return '$text';
}

/**
* Gets the fields item accessor
*
* @param CrestApps\CodeGeneraotor\Support\Field $field
*
* @return string
*/
protected function getFieldItemAccessor(Field $field)
{
if ($field->hasForeignRelation()) {
Expand All @@ -489,14 +503,15 @@ protected function getFieldItemAccessor(Field $field)
return '$text';
}

/**
* Gets the field value accesor.
*
* @param CrestApps\CodeGeneraotor\Support\Field $field
*
* @return string
*/
protected function getFieldValueAccessor(Field $field)
{
if ($field->hasForeignRelation()) {
$relation = $field->getForeignRelation();

return sprintf('$%s->%s', $relation->getSingleName(), $relation->getPrimaryKeyForForeignModel());
}

return in_array($field->htmlType, ['selectRange','selectMonth']) ? '$value' : '$key';
}

Expand Down

0 comments on commit 318e0fe

Please sign in to comment.