Browse Source

Added luthfi/formfield package and remove local FormField service

pull/1/head
Nafies Luthfi 9 years ago
parent
commit
b532d2f839
  1. 29
      app/Providers/FormFieldServiceProvider.php
  2. 10
      app/Services/Facades/FormField.php
  3. 307
      app/Services/FormField.php
  4. 2
      composer.json
  5. 42
      composer.lock
  6. 9
      config/app.php

29
app/Providers/FormFieldServiceProvider.php

@ -1,29 +0,0 @@
<?php
namespace App\Providers;
use App\Services\FormField;
use Illuminate\Support\ServiceProvider;
class FormFieldServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->alias(FormField::class, 'formField');
}
}

10
app/Services/Facades/FormField.php

@ -1,10 +0,0 @@
<?php
namespace App\Services\Facades;
use Illuminate\Support\Facades\Facade;
class FormField extends Facade
{
protected static function getFacadeAccessor() { return 'formField'; }
}

307
app/Services/FormField.php

@ -1,307 +0,0 @@
<?php
namespace App\Services;
use Form;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
use Session;
/**
* FormField Class (Site FormField Service)
*/
class FormField
{
protected $errorBag;
protected $options;
protected $fieldParams;
public function __construct()
{
$this->errorBag = Session::get('errors', new MessageBag);
}
public function text($name, $options = [])
{
$hasError = $this->errorBag->has($name) ? 'has-error' : '';
$htmlForm = '<div class="form-group ' . $hasError . '">';
$value = isset($options['value']) ? $options['value'] : null;
$type = isset($options['type']) ? $options['type'] : 'text';
$fieldParams = ['class'=>'form-control'];
if (isset($options['class'])) { $fieldParams['class'] .= ' ' . $options['class']; }
$htmlForm .= $this->setFormFieldLabel($name, $options);
if (isset($options['addon'])) { $htmlForm .= '<div class="input-group">'; }
if (isset($options['addon']['before'])) {
$htmlForm .= '<span class="input-group-addon">' . $options['addon']['before'] . '</span>';
}
if (isset($options['readonly']) && $options['readonly'] == true) { $fieldParams += ['readonly']; }
if (isset($options['disabled']) && $options['disabled'] == true) { $fieldParams += ['disabled']; }
if (isset($options['required']) && $options['required'] == true) { $fieldParams += ['required']; }
if (isset($options['min'])) { $fieldParams += ['min' => $options['min']]; }
if (isset($options['placeholder'])) { $fieldParams += ['placeholder' => $options['placeholder']]; }
if (isset($options['style'])) { $fieldParams += ['style' => $options['style']]; }
if (isset($options['id'])) { $fieldParams += ['id' => $options['id']]; }
$htmlForm .= Form::input($type, $name, $value, $fieldParams);
if (isset($options['addon']['after'])) {
$htmlForm .= '<span class="input-group-addon">' . $options['addon']['after'] . '</span>';
}
if (isset($options['addon'])) { $htmlForm .= '</div>'; }
if (isset($options['info'])) {
$class = isset($options['info']['class']) ? $options['info']['class'] : 'info';
$htmlForm .= '<p class="text-' . $class . ' small">' . $options['info']['text'] . '</p>';
}
$htmlForm .= $this->errorBag->first($name, '<span class="form-error">:message</span>');
$htmlForm .= '</div>';
return $htmlForm;
}
public function textarea($name, $options = [])
{
$hasError = $this->errorBag->has($name) ? 'has-error' : '';
$htmlForm = '<div class="form-group ' . $hasError . '">';
$rows = isset($options['rows']) ? $options['rows'] : 3;
$value = isset($options['value']) ? $options['value'] : null;
$fieldParams = ['class'=>'form-control','rows' => $rows];
if (isset($options['readonly']) && $options['readonly'] == true) { $fieldParams += ['readonly']; }
if (isset($options['disabled']) && $options['disabled'] == true) { $fieldParams += ['disabled']; }
if (isset($options['required']) && $options['required'] == true) { $fieldParams += ['required']; }
if (isset($options['placeholder'])) { $fieldParams += ['placeholder' => $options['placeholder']]; }
$htmlForm .= $this->setFormFieldLabel($name, $options);
$htmlForm .= Form::textarea($name, $value, $fieldParams);
$htmlForm .= $this->errorBag->first($name, '<span class="form-error">:message</span>');
$htmlForm .= '</div>';
return $htmlForm;
}
public function select($name, $selectOptions, $options = [])
{
$hasError = $this->errorBag->has($name) ? 'has-error' : '';
$htmlForm = '<div class="form-group ' . $hasError . '">';
$value = isset($options['value']) ? $options['value'] : null;
$fieldParams = ['class'=>'form-control'];
if (isset($options['class'])) { $fieldParams['class'] .= ' ' . $options['class']; }
if (isset($options['readonly']) && $options['readonly'] == true) { $fieldParams += ['readonly']; }
if (isset($options['disabled']) && $options['disabled'] == true) { $fieldParams += ['disabled']; }
if (isset($options['required']) && $options['required'] == true) { $fieldParams += ['required']; }
if (isset($options['multiple']) && $options['multiple'] == true) { $fieldParams += ['multiple', 'name' => $name . '[]']; }
if (isset($options['placeholder']))
$fieldParams += ['placeholder' => $options['placeholder']];
else
$fieldParams += ['placeholder' => '-- Pilih ' . str_split_ucwords(str_replace('_id', '', $name)) . ' --'];
$htmlForm .= $this->setFormFieldLabel($name, $options);
$htmlForm .= Form::select($name, $selectOptions, $value, $fieldParams);
$htmlForm .= $this->errorBag->first($name, '<span class="form-error">:message</span>');
$htmlForm .= '</div>';
return $htmlForm;
}
public function multiSelect($name, $selectOptions, $options = [])
{
$options['multiple'] = true;
return $this->select($name, $selectOptions, $options);
}
public function email($name, $options = [])
{
$options['type'] = 'email';
return $this->text($name, $options);
}
public function password($name, $options = [])
{
$options['type'] = 'password';
return $this->text($name, $options);
}
public function radios($name, array $radioOptions, $options = [])
{
$hasError = $this->errorBag->has($name) ? 'has-error' : '';
$htmlForm = '<div class="form-group ' . $hasError . '">';
$htmlForm .= $this->setFormFieldLabel($name, $options);
$listStyle = isset($options['list_style']) ? $options['list_style'] : 'inline';
$htmlForm .= '<ul class="radio list-' . $listStyle . '">';
foreach ($radioOptions as $key => $option) {
$value = null;
$fieldParams = ['id' => $name . '_' . $key];
if (isset($options['value']) && $options['value'] == $key) { $value = true; }
if (isset($options['v-model'])) { $fieldParams += ['v-model' => $options['v-model']]; }
$htmlForm .= '<li><label for="' . $name . '_' . $key . '">';
$htmlForm .= Form::radio($name, $key, $value, $fieldParams);
$htmlForm .= $option;
$htmlForm .= '&nbsp;</label></li>';
}
$htmlForm .= '</ul>';
$htmlForm .= $this->errorBag->first($name, '<span class="form-error">:message</span>');
$htmlForm .= '</div>';
return $htmlForm;
}
public function checkboxes($name, array $checkboxOptions, $options = [])
{
$hasError = $this->errorBag->has($name) ? 'has-error' : '';
$htmlForm = '<div class="form-group ' . $hasError . '">';
$htmlForm .= $this->setFormFieldLabel($name, $options);
$listStyle = isset($options['list_style']) ? $options['list_style'] : 'inline';
$htmlForm .= '<ul class="checkbox list-' . $listStyle . '">';
$value = isset($options['value']) ? $options['value'] : new Collection;
foreach ($checkboxOptions as $key => $option) {
$fieldParams = ['id' => $name . '_' . $key];
if (isset($options['v-model'])) { $fieldParams += ['v-model' => $options['v-model']]; }
$htmlForm .= '<li><label for="' . $name . '_' . $key . '">';
$htmlForm .= Form::checkbox($name . '[]', $key, $value->contains($key), $fieldParams);
$htmlForm .= $option;
$htmlForm .= '&nbsp;</label></li>';
}
$htmlForm .= '</ul>';
$htmlForm .= $this->errorBag->first($name, '<span class="form-error">:message</span>');
$htmlForm .= '</div>';
return $htmlForm;
}
public function textDisplay($name, $value, $options = [])
{
$label = isset($options['label']) ? $options['label'] : str_split_ucwords($name);
$htmlForm = '<div class="form-group">';
$htmlForm .= Form::label($name, $label, ['class'=>'control-label']);
$htmlForm .= '<div class="form-control" readonly>' . $value . '</div>';
$htmlForm .= '</div>';
return $htmlForm;
}
public function file($name, $options = [])
{
$hasError = $this->errorBag->has($name) ? 'has-error' : '';
$label = isset($options['label']) ? $options['label'] : str_split_ucwords($name);
$htmlForm = '<div class="form-group ' . $hasError . '">';
$htmlForm .= $this->setFormFieldLabel($name, $options);
$fieldParams = ['class'=>'form-control'];
if (isset($options['class'])) { $fieldParams['class'] .= ' ' . $options['class']; }
if (isset($options['multiple']) && $options['multiple'] == true) {
$name = $name . '[]';
$fieldParams += ['multiple' => true];
}
$htmlForm .= Form::file($name, $fieldParams);
if (isset($options['info'])) {
$htmlForm .= '<p class="text-' . $options['info']['class'] . ' small">' . $options['info']['text'] . '</p>';
}
$htmlForm .= $this->errorBag->first($name, '<span class="form-error">:message</span>');
$htmlForm .= '</div>';
return $htmlForm;
}
public function delete($form_params = [], $button_label = 'x', $button_options = [], $hiddenFields = [])
{
$form_params['method'] = 'delete';
$form_params['class'] = isset($form_params['class']) ? $form_params['class'] : 'del-form';
$form_params['style'] = isset($form_params['style']) ? $form_params['style'] : 'display:inline';
if (! isset($button_options['class']))
$button_options['class'] = 'pull-right';
if (! isset($button_options['title']))
$button_options['title'] = 'Remove this';
$htmlForm = Form::open($form_params);
if (!empty($hiddenFields))
{
foreach ($hiddenFields as $k => $v)
{
$htmlForm .= Form::hidden($k, $v);
}
}
$htmlForm .= Form::submit($button_label, $button_options);
$htmlForm .= Form::close();
return $htmlForm;
}
public function arrays($name, array $fieldKeys, $options = [])
{
$hasError = $this->errorBag->has($name) ? 'has-error' : '';
$label = isset($options['label']) ? $options['label'] : str_split_ucwords($name);
$htmlForm = '<div class="form-group ' . $hasError . '">';
$htmlForm .= Form::label($name, $label, ['class'=>'control-label']);
if (empty($contents) == false) {
foreach ($checkboxOptions as $key => $option) {
$htmlForm .= '<div class="row">';
$htmlForm .= Form::text($name . '[]', $key);
$htmlForm .= '</div>';
}
}
$htmlForm .= '<div class="new-' . $name . ' row">';
$htmlForm .= '<div class="col-md-4">';
$htmlForm .= Form::text($fieldKeys[0], null, ['class' => 'form-control']);
$htmlForm .= '</div>';
$htmlForm .= '<div class="col-md-8 row">';
$htmlForm .= Form::text($fieldKeys[1], null, ['class' => 'form-control']);
$htmlForm .= '</div>';
$htmlForm .= '</div>';
$htmlForm .= '<a id="add-service" class="btn btn-info btn-xs pull-right"><i class="fa fa-plus fa-fw"></i></a>';
$htmlForm .= $this->errorBag->first($name, '<span class="form-error">:message</span>');
$htmlForm .= '</div>';
return $htmlForm;
}
public function price($name, $options = [])
{
$options['addon'] = ['before' => isset($options['currency']) ? $options['currency'] : 'Rp'];
$options['class'] = 'text-right';
return $this->text($name, $options);
}
private function setFormFieldLabel($name, $options)
{
if (isset($options['label']) && $options['label'] != false) {
$label = isset($options['label']) ? $options['label'] : str_split_ucwords($name);
return Form::label($name, $label, ['class'=>'control-label']) . '&nbsp;';
} elseif (! isset($options['label'])) {
return Form::label($name, str_split_ucwords($name), ['class'=>'control-label']) . '&nbsp;';
}
}
}

2
composer.json

@ -9,7 +9,7 @@
"barryvdh/laravel-dompdf": "^0.7.0",
"intervention/image": "^2.3",
"laravel/framework": "5.4.*",
"luthfi/formfield": "1.*",
"luthfi/formfield": "^0.1.6",
"laracasts/flash": "~2",
"laracasts/presenter": "^0.2.1",
"backup-manager/laravel": "^1.0",

42
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "2f401db91b0298fc38ade2c58a191657",
"content-hash": "9853f898a7ea6916d797832a1737a999",
"packages": [
{
"name": "backup-manager/backup-manager",
@ -958,6 +958,46 @@
"time": "2016-12-28T01:30:30+00:00"
},
{
"name": "luthfi/formfield",
"version": "0.1.6",
"source": {
"type": "git",
"url": "https://github.com/nafiesl/FormField.git",
"reference": "dfbbae8f86f6d332a9f548ad6cebc77e2c66810f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nafiesl/FormField/zipball/dfbbae8f86f6d332a9f548ad6cebc77e2c66810f",
"reference": "dfbbae8f86f6d332a9f548ad6cebc77e2c66810f",
"shasum": ""
},
"require": {
"illuminate/support": "5.3.* || 5.4.*",
"laravelcollective/html": "5.3.* || 5.4.*"
},
"require-dev": {
"orchestra/testbench": "~3.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Luthfi\\FormField\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nafies Luthfi",
"email": "nafiesl@gmail.com"
}
],
"description": "Form Field helper class for Laravel 5.3 with Twitter Bootstrap",
"time": "2017-06-16T03:22:15+00:00"
},
{
"name": "maatwebsite/excel",
"version": "2.1.19",
"source": {

9
config/app.php

@ -156,13 +156,12 @@ return [
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\FormFieldServiceProvider::class,
App\Providers\OptionServiceProvider::class,
App\Providers\RouteServiceProvider::class,
BackupManager\Laravel\Laravel5ServiceProvider::class,
Collective\Html\HtmlServiceProvider::class,
Laracasts\Flash\FlashServiceProvider::class,
Luthfi\FormField\FormFieldServiceProvider::class,
Maatwebsite\Excel\ExcelServiceProvider::class,
Spatie\Fractal\FractalServiceProvider::class,
],
@ -214,11 +213,11 @@ return [
'Carbon' => Carbon\Carbon::class,
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
'Fractal' => Spatie\Fractal\FractalFacade::class,
'Option' => App\Services\Facades\Option::class,
'FormField' => Luthfi\FormField\FormFieldFacade::class,
'Form' => Collective\Html\FormFacade::class,
'FormField' => App\Services\Facades\FormField::class,
'Html' => Collective\Html\HtmlFacade::class,
'Flash' => Laracasts\Flash\Flash::class,
'Option' => App\Services\Facades\Option::class,
],

Loading…
Cancel
Save