-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
hasMany form validation not working on single submit click #5816
Comments
|
Hi @alexoleynik0, Good morning
This is the form structure I have created: During the First submission form it does not show an error After clicking the submit button a second time it shows the error |
Ok, now I see the issue on my side too.. Strangely, but I'm not sure we can fix it easily. In the PR #4976 it almost fixed by @shacky , so you can create your custom HasMany field based on this PR like so: <?php
namespace App\Admin\Extensions\Form;
use Encore\Admin\Form\Field\HasMany;
use Encore\Admin\Form\NestedForm;
use Exception;
use Illuminate\Database\Eloquent\Relations\HasMany as Relation;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Arr;
class HasManyCustom extends HasMany
{
/**
* Build Nested form for related data.
*
* @return array
*
* @throws Exception
*/
protected function buildRelatedForms()
{
if (is_null($this->form)) {
return [];
}
$model = $this->form->model();
$relation = call_user_func([$model, $this->relationName]);
if (! $relation instanceof Relation && ! $relation instanceof MorphMany) {
throw new Exception('hasMany field must be a HasMany or MorphMany relation.');
}
$forms = [];
/*
* If redirect from `exception` or `validation error` page.
*
* Then get form data from session flash.
*
* Else get data from database.
*/
if ($values = old($this->column)) {
foreach ($values as $key => $data) {
if (1 == $data[NestedForm::REMOVE_FLAG_NAME]) {
continue;
}
$model = $relation->getRelated()->replicate()->forceFill($data);
$forms[$key] = $this->buildNestedForm($this->column, function ($form) use ($key) {
$form->setKey($key);
call_user_func($this->builder, $form);
}, $model)
->fill($data)
;
}
} else {
if (empty($this->value)) {
return [];
}
foreach ($this->value as $data) {
$key = Arr::get($data, $relation->getRelated()->getKeyName());
$model = $relation->getRelated()->replicate()->forceFill($data);
$forms[$key] = $this->buildNestedForm($this->column, $this->builder, $model)
->fill($data)
;
}
}
return $forms;
}
} Then declare it's use in \Encore\Admin\Form::extend('hasManyCustom', \App\Admin\Extensions\Form\HasManyCustom::class); And use it instead of default $form->hasManyCustom('orders', __('Orders'), function (Form\NestedForm $nestedForm) {
$nestedForm->text('active', 'active')->rules('required');
}); |
Unfortunately, that does not fix the issue with wrong But that is a separate issue to yours. |
Description:
Hi Team,
I am using the hasMany form and I use the
->rules('required')
for the form required field when I click the submit button the first time the validation error does not throw the error message when I click the second time it shows the error. Do you know how I can fix this? I attached my code as below.This is form data
And model
Can anyone help me to fix this bug?
The text was updated successfully, but these errors were encountered: