You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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);
`
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.
The text was updated successfully, but these errors were encountered: