Fix: Общая группа теперь создаётся

 required_if валидация для organization_id
 JavaScript убирает required при выборе общей группы
 Очищает значение при переключении на общую группу

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
mirivlad 2026-03-30 11:12:50 +08:00
parent f1f1f4edde
commit c3ed5a0edb
3 changed files with 17 additions and 8 deletions

View File

@ -56,17 +56,12 @@ class GroupUserController extends Controller
$validated = $request->validate([
'group_type' => 'required|in:organization,general',
'organization_id' => 'nullable|exists:organizations,id',
'organization_id' => 'required_if:group_type,organization|nullable|exists:organizations,id',
'name' => 'required|string|max:255',
'description' => 'nullable|string',
'is_active' => 'boolean',
]);
// Для группы организации organization_id обязательна
if ($validated['group_type'] === 'organization' && empty($validated['organization_id'])) {
return back()->withErrors(['organization_id' => 'Выберите организацию для группы'])->withInput();
}
Group::create([
'organization_id' => $validated['group_type'] === 'organization' ? $validated['organization_id'] : null,
'name' => $validated['name'],

View File

@ -82,7 +82,21 @@
<script>
function toggleOrganizationField() {
const type = document.getElementById('groupType').value;
document.getElementById('organizationField').style.display = (type === 'organization') ? 'block' : 'none';
const orgField = document.getElementById('organizationField');
const orgSelect = orgField.querySelector('select');
const orgHidden = document.getElementById('organization_id');
if (type === 'organization') {
orgField.style.display = 'block';
orgSelect.setAttribute('required', 'required');
if (orgHidden) orgHidden.setAttribute('required', 'required');
} else {
orgField.style.display = 'none';
orgSelect.removeAttribute('required');
if (orgHidden) orgHidden.removeAttribute('required');
orgSelect.value = ''; // Очищаем значение
if (orgHidden) orgHidden.value = '';
}
}
document.addEventListener('DOMContentLoaded', function() {

View File

@ -1,6 +1,6 @@
@props(['name', 'url', 'placeholder' => 'Начните вводить...', 'value' => null, 'required' => false])
<input type="hidden" name="{{ $name }}" id="{{ $name }}" value="{{ $value }}">
<input type="hidden" name="{{ $name }}" id="{{ $name }}" value="{{ $value }}" {{ $required ? 'required' : '' }}>
<select id="{{ $name }}-select" class="form-select @error($name) is-invalid @enderror" {{ $required ? 'required' : '' }}></select>
@push('scripts')