PHP code example of illuminatech / validation-composite
1. Go to this page and download the library: Download illuminatech/validation-composite 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/ */
illuminatech / validation-composite example snippets
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
public function update(Request $request)
{
$validatedData = $request->validate([
'password' => ['
namespace App\Rules;
use Illuminatech\Validation\Composite\CompositeRule;
class PasswordRule extends CompositeRule
{
protected function rules(): array
{
return ['string', 'min:8', 'max:200'];
}
}
class AvatarRule extends CompositeRule
{
protected function rules(): array
{
return ['file', 'mimes:png,jpg,jpeg', 'max:1024'];
}
}
namespace App\Http\Controllers;
use App\Rules\AvatarRule;
use App\Rules\PasswordRule;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
public function update(Request $request)
{
$validatedData = $request->validate([
'password' => ['
use App\Rules\PasswordRule;
use Illuminate\Support\Facades\Validator;
$validator = Validator::make(
['password' => 'short'],
[
'password' => ['
namespace App\Providers;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Support\ServiceProvider;
use Illuminatech\Validation\Composite\DynamicCompositeRule;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
$this->app->extend('validator', function (Factory $validatorFactory) {
$validatorFactory->extend('password', function ($attribute, $value) {
return (new DynamicCompositeRule(['string', 'min:8', 'max:200']))->passes($attribute, $value);
});
$validatorFactory->extend('avatar', function ($attribute, $value) {
return (new DynamicCompositeRule(['file', 'mimes:png,jpg,jpeg', 'max:1024']))->passes($attribute, $value);
});
return $validatorFactory;
});
// ...
}
}
namespace App\Rules;
use Illuminatech\Validation\Composite\CompositeRule;
class PasswordRule extends CompositeRule
{
protected function rules(): array
{
return ['string', 'min:8', 'max:200'];
}
protected function messages(): array
{
return [
'string' => 'Only string is allowed.',
'min' => ':attribute is too short.',
'max' => ':attribute is too long.',
];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.