Skip to content
This repository was archived by the owner on Aug 30, 2020. It is now read-only.

Commit

Permalink
using custom error bag
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-aliraqi committed Jul 18, 2019
1 parent 248fcab commit f535ba7
Show file tree
Hide file tree
Showing 26 changed files with 131 additions and 77 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ return [
...
];
```

<a name="errorBug"></a>
# # Using Custom Error Message Bag
> You can using custom error bag to display validation errors without any conflict.
```
// Default error bag
BsForm::errorBag('default')
// Other error bag
BsForm::errorBag('create')
```
<a name="example"></a>
# # Example Register Form
```
Expand Down
27 changes: 24 additions & 3 deletions src/BsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class BsForm
*/
protected static $instance;

/**
* The key to be used for the view error bag.
*
* @var string
*/
protected $errorBag = 'default';

/**
* BsForm constructor.
*/
Expand All @@ -97,7 +104,8 @@ public function registerComponent($name, $component)
public function __call($name, $arguments)
{
if (isset($this->components[$name])) {
$instance = new $this->components[$name]($this->resource);
$instance = (new $this->components[$name]($this->resource))
->errorBag($this->errorBag);

if ($instance instanceof LocalizableComponent) {
$instance->locale($this->locale);
Expand Down Expand Up @@ -147,6 +155,19 @@ public function resource($resource)
return $this;
}

/**
* The key to be used for the view error bag.
*
* @param string $bag
* @return $this
*/
public function errorBag($bag = 'default')
{
$this->errorBag = $bag;

return $this;
}

/**
* Set the components style.
*
Expand Down Expand Up @@ -175,7 +196,7 @@ public function clearStyle()
/**
* Set the input inline validation errors option.
*
* @param bool $bool
* @param bool $bool
* @return $this
*/
public function inlineValidation($bool = true)
Expand Down Expand Up @@ -227,4 +248,4 @@ private function __clone()
{
//
}
}
}
35 changes: 28 additions & 7 deletions src/Components/BaseComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ abstract class BaseComponent implements Htmlable
*/
protected $inlineValidation = true;

/**
* The key to be used for the view error bag.
*
* @var string
*/
protected $errorBag = 'default';

/**
* Set resource name property.
*
Expand All @@ -104,8 +111,8 @@ public function __construct($resource)
/**
* Initialized the input arguments.
*
* @param null $name
* @param null $value
* @param null $name
* @param null $value
* @return $this
*/
abstract public function init();
Expand Down Expand Up @@ -166,6 +173,19 @@ public function name($name)
return $this;
}

/**
* The key to be used for the view error bag.
*
* @param string $bag
* @return $this
*/
public function errorBag($bag = 'default')
{
$this->errorBag = $bag;

return $this;
}

/**
* @param $label
* @return $this
Expand Down Expand Up @@ -225,8 +245,8 @@ public function autofocus($autofocus = true)
/**
* Add custom attribute.
*
* @param string|array $key
* @param null $value
* @param string|array $key
* @param null $value
* @return $this
*/
public function attribute($key, $value = null)
Expand Down Expand Up @@ -272,7 +292,7 @@ public function style($style)
/**
* Set the input inline validation errors option.
*
* @param bool $bool
* @param bool $bool
* @return $this
*/
public function inlineValidation($bool = true)
Expand All @@ -297,6 +317,7 @@ protected function render()
'note' => $this->note,
'attributes' => $this->attributes,
'inlineValidation' => $this->inlineValidation,
'errorBag' => $this->errorBag,
], $this->viewComposer());

return view($this->getViewPath())
Expand All @@ -317,7 +338,7 @@ protected function viewComposer()
/**
* Transform the properties to be used in view.
*
* @param array $properties
* @param array $properties
* @return array
*/
protected function transformProperties(array $properties)
Expand All @@ -334,4 +355,4 @@ public function toHtml()
{
return $this->render();
}
}
}
3 changes: 2 additions & 1 deletion src/Facades/BsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @method static \Elnooronline\LaravelBootstrapForms\BsForm style($style = null)
* @method static \Elnooronline\LaravelBootstrapForms\BsForm clearStyle()
* @method static \Elnooronline\LaravelBootstrapForms\BsForm inlineValidation($bool = true)
* @method static \Elnooronline\LaravelBootstrapForms\BsForm errorBag($bag = 'default')
* @method static \Illuminate\Support\HtmlString close()
*
* @package Elnooronline\LaravelBootstrapForms\Facades
Expand All @@ -38,4 +39,4 @@ protected static function getFacadeAccessor()
{
return 'bootstrap.form';
}
}
}
6 changes: 3 additions & 3 deletions src/views/checkbox/default.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div class="form-group{{ $errors->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="form-group{{ $errors->{$errorBag}->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="checkbox">
<label>
{{ Form::checkbox($name, $value, $checked) }} {{ $label }}
</label>
</div>
@if($inlineValidation)
@if($errors->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->first($nameWithoutBrackets) }}</strong>
@if($errors->{$errorBag}->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->{$errorBag}->first($nameWithoutBrackets) }}</strong>
@else
<strong class="help-block">{{ $note }}</strong>
@endif
Expand Down
6 changes: 3 additions & 3 deletions src/views/checkbox/vertical.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="form-group{{ $errors->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="form-group{{ $errors->{$errorBag}->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
{{ Form::checkbox($name, $value, $checked) }} {{ $label }}
</label>
</div>
@if($inlineValidation)
@if($errors->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->first($nameWithoutBrackets) }}</strong>
@if($errors->{$errorBag}->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->{$errorBag}->first($nameWithoutBrackets) }}</strong>
@else
<strong class="help-block">{{ $note }}</strong>
@endif
Expand Down
6 changes: 3 additions & 3 deletions src/views/date/default.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="form-group{{ $errors->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="form-group{{ $errors->{$errorBag}->has($nameWithoutBrackets) ? ' has-error' : '' }}">
@if($label)
{{ Form::label($name, $label, ['class' => 'content-label']) }}
@endif

{{ Form::date($name, $value, array_merge(['class' => 'form-control'], $attributes)) }}

@if($inlineValidation)
@if($errors->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->first($nameWithoutBrackets) }}</strong>
@if($errors->{$errorBag}->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->{$errorBag}->first($nameWithoutBrackets) }}</strong>
@else
<strong class="help-block">{{ $note }}</strong>
@endif
Expand Down
6 changes: 3 additions & 3 deletions src/views/date/vertical.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="form-group{{ $errors->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="form-group{{ $errors->{$errorBag}->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="row">
@if($label)
{{ Form::label($name, $label, ['class' => 'content-label col-md-2']) }}
Expand All @@ -11,8 +11,8 @@
{{ Form::date($name, $value, array_merge(['class' => 'form-control'], $attributes)) }}

@if($inlineValidation)
@if($errors->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->first($nameWithoutBrackets) }}</strong>
@if($errors->{$errorBag}->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->{$errorBag}->first($nameWithoutBrackets) }}</strong>
@else
<strong class="help-block">{{ $note }}</strong>
@endif
Expand Down
6 changes: 3 additions & 3 deletions src/views/email/default.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="form-group{{ $errors->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="form-group{{ $errors->{$errorBag}->has($nameWithoutBrackets) ? ' has-error' : '' }}">
@if($label)
{{ Form::label($name, $label, ['class' => 'content-label']) }}
@endif

{{ Form::email($name, $value, array_merge(['class' => 'form-control'], $attributes)) }}

@if($inlineValidation)
@if($errors->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->first($nameWithoutBrackets) }}</strong>
@if($errors->{$errorBag}->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->{$errorBag}->first($nameWithoutBrackets) }}</strong>
@else
<strong class="help-block">{{ $note }}</strong>
@endif
Expand Down
6 changes: 3 additions & 3 deletions src/views/email/vertical.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="form-group{{ $errors->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="form-group{{ $errors->{$errorBag}->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="row">
@if($label)
{{ Form::label($name, $label, ['class' => 'content-label col-md-2']) }}
Expand All @@ -11,8 +11,8 @@
{{ Form::email($name, $value, array_merge(['class' => 'form-control'], $attributes)) }}

@if($inlineValidation)
@if($errors->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->first($nameWithoutBrackets) }}</strong>
@if($errors->{$errorBag}->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->{$errorBag}->first($nameWithoutBrackets) }}</strong>
@else
<strong class="help-block">{{ $note }}</strong>
@endif
Expand Down
6 changes: 3 additions & 3 deletions src/views/file/default.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="form-group{{ $errors->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="form-group{{ $errors->{$errorBag}->has($nameWithoutBrackets) ? ' has-error' : '' }}">
@if($label)
{{ Form::label($name, $label, ['class' => 'content-label']) }}
@endif

{{ Form::file($name, array_merge(['class' => 'form-control'], $attributes)) }}

@if($inlineValidation)
@if($errors->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->first($nameWithoutBrackets) }}</strong>
@if($errors->{$errorBag}->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->{$errorBag}->first($nameWithoutBrackets) }}</strong>
@else
<strong class="help-block">{{ $note }}</strong>
@endif
Expand Down
6 changes: 3 additions & 3 deletions src/views/file/vertical.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="form-group{{ $errors->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="form-group{{ $errors->{$errorBag}->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="row">
@if($label)
{{ Form::label($name, $label, ['class' => 'content-label col-md-2']) }}
Expand All @@ -11,8 +11,8 @@
{{ Form::file($name, array_merge(['class' => 'form-control'], $attributes)) }}

@if($inlineValidation)
@if($errors->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->first($nameWithoutBrackets) }}</strong>
@if($errors->{$errorBag}->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->{$errorBag}->first($nameWithoutBrackets) }}</strong>
@else
<strong class="help-block">{{ $note }}</strong>
@endif
Expand Down
6 changes: 3 additions & 3 deletions src/views/number/default.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="form-group{{ $errors->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="form-group{{ $errors->{$errorBag}->has($nameWithoutBrackets) ? ' has-error' : '' }}">
@if($label)
{{ Form::label($name, $label, ['class' => 'content-label']) }}
@endif

{{ Form::number($name, $value, array_merge(['class' => 'form-control'], $attributes)) }}

@if($inlineValidation)
@if($errors->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->first($nameWithoutBrackets) }}</strong>
@if($errors->{$errorBag}->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->{$errorBag}->first($nameWithoutBrackets) }}</strong>
@else
<strong class="help-block">{{ $note }}</strong>
@endif
Expand Down
6 changes: 3 additions & 3 deletions src/views/number/vertical.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="form-group{{ $errors->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="form-group{{ $errors->{$errorBag}->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="row">
@if($label)
{{ Form::label($name, $label, ['class' => 'content-label col-md-2']) }}
Expand All @@ -11,8 +11,8 @@
{{ Form::number($name, $value, array_merge(['class' => 'form-control'], $attributes)) }}

@if($inlineValidation)
@if($errors->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->first($nameWithoutBrackets) }}</strong>
@if($errors->{$errorBag}->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->{$errorBag}->first($nameWithoutBrackets) }}</strong>
@else
<strong class="help-block">{{ $note }}</strong>
@endif
Expand Down
6 changes: 3 additions & 3 deletions src/views/password/default.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="form-group{{ $errors->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="form-group{{ $errors->{$errorBag}->has($nameWithoutBrackets) ? ' has-error' : '' }}">
@if($label)
{{ Form::label($name, $label, ['class' => 'content-label']) }}
@endif

{{ Form::password($name, array_merge(['class' => 'form-control'], $attributes)) }}

@if($inlineValidation)
@if($errors->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->first($nameWithoutBrackets) }}</strong>
@if($errors->{$errorBag}->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->{$errorBag}->first($nameWithoutBrackets) }}</strong>
@else
<strong class="help-block">{{ $note }}</strong>
@endif
Expand Down
6 changes: 3 additions & 3 deletions src/views/password/vertical.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="form-group{{ $errors->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="form-group{{ $errors->{$errorBag}->has($nameWithoutBrackets) ? ' has-error' : '' }}">
<div class="row">
@if($label)
{{ Form::label($name, $label, ['class' => 'content-label col-md-2']) }}
Expand All @@ -11,8 +11,8 @@
{{ Form::password($name, array_merge(['class' => 'form-control'], $attributes)) }}

@if($inlineValidation)
@if($errors->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->first($nameWithoutBrackets) }}</strong>
@if($errors->{$errorBag}->has($nameWithoutBrackets))
<strong class="help-block">{{ $errors->{$errorBag}->first($nameWithoutBrackets) }}</strong>
@else
<strong class="help-block">{{ $note }}</strong>
@endif
Expand Down
Loading

0 comments on commit f535ba7

Please sign in to comment.