Форма работает нормально. Все выбранные и другие элементы формы работают нормально, когда я отправляюсь. Единственная проблема - это Community_id, которая зависит от района. Он всегда учитывался, что ввод не был найден в сенах . В консоли браузера форма отправляет сообщество_ид, но я не то, почему я продолжаю получать проблему сена:
District_id: 9C72F59E-F733-11EF-89E9-00155D016F00
Сообщество /> Форма: < /p>
Elements
// Add district selection
$this->add([
'name' => 'district_id',
'type' => Element\Select::class,
'options' => [
'label' => 'District',
'empty_option' => 'Select District',
'value_options' => $this->getDistrictOptions(),
'label_attributes' => [
'class' => 'required-field'
],
],
'attributes' => [
'id' => 'district_id',
'class' => 'form-control form-select',
'required' => true,
'data-dependent-url' => '/community/list'
],
]);
// Add community selection
$this->add([
'name' => 'community_id',
'type' => Element\Select::class,
'options' => [
'label' => 'Community',
'empty_option' => 'Select Community',
'value_options' => $this->getCommunityOptions(),
'label_attributes' => [
'class' => 'required-field'
],
],
'attributes' => [
'id' => 'community_id',
'class' => 'form-control form-select',
'required' => true,
'disabled' => true,
'data-dependent' => 'district_id',
'value' => ''
],
]);
*Inputfilters*
'district_id' => [
'required' => true,
'filters' => [
['name' => 'StringTrim'],
],
'validators' => [
[
'name' => 'NotEmpty',
'options' => [
'messages' => [
'isEmpty' => 'Please select a district'
],
],
],
[
'name' => 'Callback',
'options' => [
'callback' => [$this, 'validateDistrict'],
'messages' => [
'callbackValue' => 'Invalid district selected'
],
],
],
],
],
'community_id' => [
'required' => true,
'filters' => [
['name' => 'StringTrim'],
],
'validators' => [
[
'name' => 'NotEmpty',
'options' => [
'messages' => [
'isEmpty' => 'Please select a community or choose Other and specify a new community name'
],
],
],
[
'name' => 'Callback',
'options' => [
'callback' => [$this, 'validateCommunity'],
'messages' => [
'callbackValue' => 'Please select a valid community from the list or choose Other and specify a new community name'
],
],
],
],
],
*Validators*
public function validateDistrict($value)
{
$district = $this->entityManager->getRepository(\Grm\Entity\District::class)
->find($value);
return $district !== null;
}
public function validateCommunity($value)
{
// Log the submitted value
error_log("Submitted community_id: " . $value);
// Get the district ID
$districtId = $this->get('district_id')->getValue();
error_log("Selected district_id: " . $districtId);
if (empty($districtId)) {
error_log("District ID is empty.");
return false;
}
// Handle 'other' option
if ($value === 'other') {
$otherCommunity = $this->get('other_community')->getValue();
if (empty($otherCommunity)) {
error_log("Other community name is empty.");
return false;
}
return true;
}
// For numeric IDs, verify the community exists in the selected district
if (is_numeric($value)) {
try {
$community = $this->entityManager->getRepository(\Grm\Entity\Community::class)
->findOneBy([
'id' => $value,
'district' => $districtId
]);
if ($community === null) {
error_log("Community not found in the selected district.");
} else {
error_log("Community found: " . $community->getName());
}
return $community !== null;
} catch (\Exception $e) {
error_log("Error validating community: " . $e->getMessage());
return false;
}
}
error_log("Invalid community_id value: " . $value);
return false;
}
< /code>
Index.phtml:
< /code>
< /code>
*Scripts*
`// Handle dependent dropdowns and form fields
$(document).ready(function() {
// District -> Community dependency
$('#district_id').change(function() {
const districtId = $(this).val();
const communitySelect = $('#community_id');
const otherCommunityContainer = $('.other-community-container');
const otherCommunityField = $('#other_community');
// Reset and hide other community field
otherCommunityContainer.addClass('hidden');
otherCommunityField.val('').prop('required', false);
if (districtId) {
// Show loading indicator
communitySelect.prop('disabled', true).empty()
.append('Select Community');
$.ajax({
url: '/community/list/' + districtId,
method: 'GET',
dataType: 'json',
success: function(response) {
communitySelect.empty();
// Add empty option
communitySelect.append('Select Community');
if (response && response.length > 0) {
response.forEach(function(community) {
communitySelect.append(
$('')
.val(community.id)
.text(community.name)
);
});
}
// Add "Other" option
communitySelect.append('Other (Specify)');
communitySelect.prop('disabled', false);
},
error: function() {
communitySelect.empty()
.append('Error loading communities')
.append('Other (Specify)')
.prop('disabled', false);
}
});
} else {
communitySelect.empty()
.append('Select Community')
.append('Other (Specify)')
.prop('disabled', true);
// Reset other community field
otherCommunityContainer.addClass('hidden');
otherCommunityField.val('').prop('required', false);
}
});
// Handle Other Community selection
$('#community_id').on('change', function() {
const selectedValue = $(this).val();
const otherCommunityContainer = $('.other-community-container');
const otherCommunityField = $('#other_community');
if (selectedValue === 'other') {
otherCommunityContainer.removeClass('hidden');
otherCommunityField.prop('required', true).val('');
} else {
otherCommunityContainer.addClass('hidden');
otherCommunityField.prop('required', false).val('');
}
});
});`
Подробнее здесь: https://stackoverflow.com/questions/794 ... -input-was
Зависимый раскрывающийся список с использованием Laminas Framework и Dectrine -Orm - ввод не был найден в стоке сена ⇐ Php
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Зависимый раскрывающийся список, второй раскрывающийся список не заполнен, Codeigniter
Anonymous » » в форуме Php - 0 Ответы
- 37 Просмотры
-
Последнее сообщение Anonymous
-
-
-
CodeIgniter и Ajax: динамически зависимый раскрывающийся список не работает
Anonymous » » в форуме Php - 0 Ответы
- 18 Просмотры
-
Последнее сообщение Anonymous
-
-
-
CodeIgniter и Ajax: динамически зависимый раскрывающийся список не работает
Anonymous » » в форуме Jquery - 0 Ответы
- 20 Просмотры
-
Последнее сообщение Anonymous
-