Fix: Общая группа теперь создаётся
✅ required_if валидация для organization_id ✅ JavaScript убирает required при выборе общей группы ✅ Очищает значение при переключении на общую группу Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
parent
f1f1f4edde
commit
c3ed5a0edb
|
|
@ -56,17 +56,12 @@ class GroupUserController extends Controller
|
||||||
|
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'group_type' => 'required|in:organization,general',
|
'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',
|
'name' => 'required|string|max:255',
|
||||||
'description' => 'nullable|string',
|
'description' => 'nullable|string',
|
||||||
'is_active' => 'boolean',
|
'is_active' => 'boolean',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Для группы организации organization_id обязательна
|
|
||||||
if ($validated['group_type'] === 'organization' && empty($validated['organization_id'])) {
|
|
||||||
return back()->withErrors(['organization_id' => 'Выберите организацию для группы'])->withInput();
|
|
||||||
}
|
|
||||||
|
|
||||||
Group::create([
|
Group::create([
|
||||||
'organization_id' => $validated['group_type'] === 'organization' ? $validated['organization_id'] : null,
|
'organization_id' => $validated['group_type'] === 'organization' ? $validated['organization_id'] : null,
|
||||||
'name' => $validated['name'],
|
'name' => $validated['name'],
|
||||||
|
|
|
||||||
|
|
@ -82,7 +82,21 @@
|
||||||
<script>
|
<script>
|
||||||
function toggleOrganizationField() {
|
function toggleOrganizationField() {
|
||||||
const type = document.getElementById('groupType').value;
|
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() {
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
@props(['name', 'url', 'placeholder' => 'Начните вводить...', 'value' => null, 'required' => false])
|
@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>
|
<select id="{{ $name }}-select" class="form-select @error($name) is-invalid @enderror" {{ $required ? 'required' : '' }}></select>
|
||||||
|
|
||||||
@push('scripts')
|
@push('scripts')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue