Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autogenerate Model messages #1106

Open
Silviobramas opened this issue Sep 3, 2024 · 0 comments
Open

Autogenerate Model messages #1106

Silviobramas opened this issue Sep 3, 2024 · 0 comments

Comments

@Silviobramas
Copy link

Would be good if laravel generator could generate messages for a model like it do for rules.
I changed the ModelGenerator.php

Add function
`
protected function generateMessages(): array
{
$dont_require_fields = config('laravel_generator.options.hidden_fields', [])
+ config('laravel_generator.options.excluded_fields', $this->excluded_fields);

    $messages = [];

    foreach ($this->config->fields as $field) {
        if (!$field->isPrimary && !in_array($field->name, $dont_require_fields)) {
            if ($field->isNotNull && empty($field->validations)) {
                $field->validations = 'required';
            }

            /**
             * Generate some sane defaults based on the field type if we
             * are generating from a database table.
             */
            if ($this->config->getOption('fromTable')) {
                $rule = empty($field->validations) ? [] : explode('|', $field->validations);

                if (!$field->isNotNull) {
                    $rule[] = 'nullable';
                }

                switch ($field->dbType) {
                    case 'integer':
                        $rule[] = 'integer';
                        break;
                    case 'boolean':
                        $rule[] = 'boolean';
                        break;
                    case 'float':
                    case 'double':
                    case 'decimal':
                        $rule[] = 'numeric';
                        break;
                    case 'string':
                    case 'text':
                        $rule[] = 'string';

                        // Enforce a maximum string length if possible.
                        if ((int) $field->fieldDetails->getLength() > 0) {
                            $rule[] = 'max:'.$field->fieldDetails->getLength();
                        }
                        break;
                }

                $field->validations = implode('|', $rule);
            }
        }

        if (!empty($field->validations)) {
            if (Str::contains($field->validations, 'unique:')) {
                $rule = explode('|', $field->validations);
                // move unique rule to last
                usort($rule, function ($record) {
                    return (Str::contains($record, 'unique:')) ? 1 : 0;
                });
                $field->validations = implode('|', $rule);
            }

            foreach($rule AS $r){
                switch ($r) {
                    case 'required':
                        $messages[] = "'".$field->name.".".$r."' => 'Please fill following field: ".str_replace('_',' ',ucfirst($field->name))."'";
                        break;
                    case 'string':
                        $messages[] = "'".$field->name.".".$r."' => 'The following field must be a string: ".str_replace('_',' ',ucfirst($field->name))."'";
                        break;
                    case 'float':
                        $messages[] = "'".$field->name.".".$r."' => 'The following field must be a float: ".str_replace('_',' ',ucfirst($field->name))."'";
                        break;
                }

            }

        }
    }

    return $messages;
}

`

Add one line in variables array
'messages' => implode(','.infy_nl_tab(1, 2), $this->generateMessages()),

And also add the following to model.blade.php
public static array $messages = [ {!! $messages !!} ];

Can youintegrate this? It's a lot of work to add messages manually every time for each model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant