PHP code example of laraeast / laravel-bootstrap-forms
1. Go to this page and download the library: Download laraeast/laravel-bootstrap-forms library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
laraeast / laravel-bootstrap-forms example snippets
use Laraeast\LaravelBootstrapForms\Rules\PhoneNumber;
// ...
public function rules(): array
{
return [
'phone' => ['
// ...
public function update(Request $request, User $user)
{
// Example: if you select EGYPT country and phone number is: 01098135318
$user->phone = $request->phone('phone');
// $user->phone = '+201098135318';
$user->save();
// Or
$user->update($request->allWithPhoneNumber('phone'));
// Or
$user->update($request->validatedWithPhoneNumber('phone'));
// ...
}
'phone' => [
/**
* The list of supported countries that will be displayed in phone input.
*
* If you want to use all countries, You can use Country::all(),
*/
'countries' => [
Country::SA,
Country::KW,
Country::IQ,
Country::AE,
Country::BH,
Country::EG,
],
/**
* The format of countries list.
*
* Examples:
* "{FLAG} {COUNTRY_CODE} ({DEAL_CODE})" => "🇸🇦 SA (+966)"
* "{FLAG} {COUNTRY_NAME} ({DEAL_CODE})" => "🇸🇦 Saudi Arabia (+966)"
*/
'countries_list_format' => '{FLAG} {COUNTRY_CODE} ({DEAL_CODE})',
],
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'phone_country' => Country::class,
// ...
];
}
return [
'attributes' => [
'username' => 'User Name',
'email' => 'E-mail Address',
'phone' => 'Phone Number',
],
'notes' => [
'username' => 'Example: Ahmed Fathy',
'email' => 'Example: [email protected]',
'phone' => 'Example: +02xxxxxxxxxxx',
],
'placeholders' => [
'username' => 'Please enter your name.',
'email' => 'Please enter your e-mail address.',
'phone' => 'Please enter your phone number.',
],
...
];
// Default error bag
BsForm::errorBag('default');
// Other error bag
BsForm::errorBag('create');
namespace App\Form\Components;
use Laraeast\LaravelBootstrapForms\Components\BaseComponent;
class ImageComponent extends BaseComponent
{
/**
* The component view path.
*/
protected string $viewPath = 'components.image';
/**
* The image file path.
*/
protected string $file;
/**
* Initialized the input arguments.
*/
public function init(...$arguments): self
{
$this->name = $arguments[0] ?? null;
$this->file = ($arguments[1] ?? null) ?: 'https://placehold.co/200x200/000000/FFF';
//$this->setDefaultLabel();
//$this->setDefaultNote();
//$this->setDefaultPlaceholder();
return $this;
}
/**
* Set the file path.
*/
public function file($file): self
{
$this->file = $file;
return $this;
}
/**
* The registered variables in view component.
*/
protected function viewComposer(): array
{
return [
'file' => $this->file,
];
}
}
namespace App\Providers;
use App\Form\Components\ImageComponent;
use Illuminate\Support\ServiceProvider;
use Laraeast\LaravelBootstrapForms\Facades\BsForm;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
BsForm::registerComponent('image', ImageComponent::class);
//...
}
//...