PHP code example of mamadali / laravel-validation-models

1. Go to this page and download the library: Download mamadali/laravel-validation-models 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/ */

    

mamadali / laravel-validation-models example snippets


class Form
{
    use ModelValidationTrait;

class User extends Model
{
    use EloquentModelValidationTrait;


    public function validateRules(): array
    {
        return [
            'title' => ['

Route::post('/test', function (Request $request) {
    // you can pass data from Request or array from user data to newModel function 
    $model = Form::newModel($request);
    $validate = $model->validate();

    // if $validate false $model->hasErrors() return false
    $hasErrors = $model->hasErrors();

    // you can get all error message as array with $model->getErrorMessages()
    $errors = $model->getErrorMessages();

    // you can get first error as string with $model->getFirstError()
    $firstErrorMessage = $model->getFirstError();

Route::post('/create-user', function (Request $request) {
    // you can pass data from Request or array from user data to newModel function 
    $model = User::newModel($request);

    $model->save();

    $model->save(['validate' => false]);

    $models = Form::newMultipleModel($request);
    // $models array from your form model with data received in request

    // your model
    
    public string $title;

    // validate rules
    .......

     /**
     * @param string $attribute in this example 'title'
     * @param mixed $value value of your attribute
     * @param array $options options passed in validate function
     * @param string $scenario model scenario
     */
    public function validateTitle($attribute, $value, $options, $scenario)
    {
        // your custom validation here
        // and you can add error to the model like this
        $this->addError($attribute, 'error message');
    }

    public function scenarios() : array
        [
          'scenario1' => ['attribute11', 'attribute12', ...],
          'scenario2' => ['attribute21', 'attribute22', ...],
          ......
        ]
    }

    $model = Form::newModel($request, 'scenario1');
    $models = Form::newMultipleModel($request, 'scenario2');

    $model = new Form();
    $model->setScenario('scenario1');
    $model->loadModel($request);

    $model->getScenario();

    public function validateRules(): array
    {
        return [
            'title' => Rule::when($this->getScenario() == 'scenario1', ['

    return $model->responseAsJson();

    return $model->responseNoContent();

    public function fields(): array
    {
        // list of fields in response
        return [
            // field id cast to integer
            'id:int',
            // field title cast to string
            'title:string',
            // you can use your database relation like this (return relation model. you can custom fields in relation model)
            'dbRelation',
            // you can use specific field from your db relation
            'dbRelation.price',
            // and you can write custom field like this
            'custom:float' => function (self $model) {
                return $this->custom();
            },
        ];
    }

    public function extraFields(): array
    {
        // list of fields in response
        return [
            'extraTitle',
            ....
        ];
    }

    public function beforeValidate() : bool
    {
        ... your code here ...
        return true;
    }

    public function afterValidate(): void
    {
        .... your code here ....
    }

    public function beforeSave(array $options = []): bool
    {
        ... your code here ...
        return true;
    }

    public function afterSave(array $options = []): void
    {
        ... your code here ...
    }