<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
theriddleofenigma / laravel-model-validation example snippets
use Enigma\ValidatorTrait;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use ValidatorTrait;
public array $validationRules = [
'name' => 's saved.
static::validateOnSaving();
}
}
static::validateOnSaving(); // fires on create and update
static::validateOnCreating(); // fires on create only
static::validateOnUpdating(); // fires on update only
$validated = $user->validate();
class User extends Model
{
use ValidatorTrait;
public array $validationMessages = [
'name.ttributes = [
'name' => 'User Name',
];
public function validationRules(): array
{
return [
'name' => '
/**
* @param array<string, mixed> $data The value of $this->getAttributes().
* @return array<string, mixed>
*/
public function validationData(array $data): array
{
$data['name'] = strtolower($data['name']);
return $data;
}
$user = new User(['name' => 'Kumar']);
$user->skipValidation()->save();
// or, for a one-off save that leaves the instance's state untouched:
$user->saveWithoutValidation();
$user->skipValidation(); // subsequent saves are not validated
$user->skipValidation(false); // validation is back on
User::withoutValidation(function () {
User::create(['name' => 'Kumar']); // not validated
});
// the return value of the callback is passed through
$user = User::withoutValidation(fn () => User::create(['name' => 'Kumar']));
public function beforeValidation(): void
{
// Normalise attributes, set defaults, etc.
}
public function afterValidation(): void
{
// Anything that should run once validation succeeds.
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.