Form after select change neodosle onSuccess

Creator13
Člen | 18
+
0
-

Ahojte. Nasledujuci formular mi funguje sprave pre update a insert, ked mam predvyplnenu spolocnost. Ak vsak pridem ku zmene selectu Spolocnosti a nacita mi aktualne kontakty spolocnosti, tak mi formular neodosle.

Je k tomuto chovaniu nejake specificky dovod, alebo robim niekde chybu? Select box mi po zmene spolocnosti pekne naplni, ale formular mi po odoslani neurobi to, co od neho ocakavam.

	public function create(?int $companyId, ?int $meetingId = null): Form
	{
		$this->setCompanyMeetingEntity($meetingId);
		$this->setCompanyEntity($companyId);

		$companies = $this->companyDataProvider->getCompaniesToSelectBox(true);
		$companyContacts = $this->getCompanyContacts();
		$ratings = $this->ratingSelectBoxOptionsProvider->getDefaultOptions();
		$userId = $this->companyMeetingEntity === null ? $this->userDataProvider->getUserId() : $this->companyMeetingEntity->getDealerId();

		$sendButtonTitle = $this->companyMeetingEntity === null ? 'Odoslať' : 'Upraviť';

		$form = $this->baseFormFactory->createDefaultForm();

		$form->addSelect(CompanyMeetingTableMap::COLUMN_COMPANY_ID, 'Spoločnosť', $companies)
			->setDefaultValue($this->companyEntity?->getId())
			->setRequired('Je potrebné vybrať spoločnosť')
			->setHtmlAttribute('class', FormClass::FORM_SELECT->value . ' ' . FormClass::FORM_SELECT_WITH_SEARCH->value . ' company')
			->setDefaultValue($this->companyMeetingEntity?->getCompanyId() ?? $this->companyEntity?->getId());

		$form->addSelect(CompanyMeetingTableMap::COLUMN_CONTACT_ID, 'Kontakt spoločnosti', $companyContacts)
			->setRequired('Je potrebné vybrať kontakt spoločnosti')
			->setHtmlAttribute('class', FormClass::FORM_SELECT->value . ' company-contact')
			->setDefaultValue($this->companyMeetingEntity?->getContactId());

		$form->addSelect(CompanyMeetingTableMap::COLUMN_RATING, 'Rating', $ratings)
			->setRequired('Je potrebné vyplniť rating')
			->setHtmlAttribute('class', FormClass::FORM_SELECT->value)
			->setDefaultValue($this->companyMeetingEntity?->getRating());

		$form->addDate(CompanyMeetingTableMap::COLUMN_CREATED_AT, 'Dátum stretnutia')
			->setRequired('Je potrebné vyplniť dátum stretnutia')
			->setHtmlAttribute('class', FormClass::FORM_INPUT->value)
			->setDefaultValue($this->companyMeetingEntity?->getCreatedAt());

		$form->addTextArea(CompanyMeetingTableMap::COLUMN_TEXT, 'Priebeh stretnutia')
			->setRequired('Je potrebné vyplniť informácie o stretnutí')
			->setHtmlAttribute('class', FormClass::FORM_INPUT->value . ' h100')
			->setDefaultValue($this->companyMeetingEntity?->getText());

		$form->addDate(CompanyMeetingTableMap::COLUMN_REMINDER_AT, 'Pripomienka')
			->setHtmlAttribute('class', FormClass::FORM_INPUT->value)
			->setDefaultValue($this->companyMeetingEntity?->getReminderAt());

		$form->addSubmit('send', $sendButtonTitle)
			->setHtmlAttribute('class', FormClass::FORM_BUTTON->value);

		$form->addHidden(CompanyMeetingTableMap::COLUMN_DEALER_ID)
			->setDefaultValue($userId);

		$form->addHidden(CompanyMeetingTableMap::COLUMN_ID)
			->setDefaultValue($this->companyMeetingEntity?->getId());

		$form->onSuccess[] = [$this, 'onSuccess'];

		return $form;
	}

	public function onSuccess(Form $form, $values): void
	{
		if ($values['id'] > 0) {
			$this->companyMeetingRepository->update((array)$values);
			return;
		}

		$companyMeetingEntity = $this->companyMeetingRepository->insert((array) $values);
		$form->setValues(['id' => $companyMeetingEntity?->getId()]);
	}

$(document).on('change', '.company', function () {

    var companyId = $( this ).val();
    const apiEndpoint = '/api/company/company-contacts-select-box/';

    $.post(apiEndpoint + companyId, function (data) {
        var selectBox = $(".company-contact");

        selectBox.empty();

        $.each(data, function(id, email) {
            selectBox.append($('<option></option>').attr('value', id).text(email));
        });
    });
});
Kamil Valenta
Člen | 815
+
+2
-

Protože selže validace, protože se v selectu company-contact posílá „neznámá“ hodnota.

https://forum.nette.org/…m-nette-a-js

Creator13
Člen | 18
+
0
-

Dakujem. Vyriesene.