How to remove contact_subject from the contact form?

General questions relating to Joomla! 4.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Post Reply
gmarra83
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Tue Nov 06, 2018 7:20 pm

How to remove contact_subject from the contact form?

Post by gmarra83 » Wed Oct 27, 2021 1:48 pm

Hello everyone I created a custom form although in the new xml created there is no longer the "subject of the message" field after the submit the message does not start because it keeps telling me that the field is mandatory(I believe it is due to the server-side form validation). Place the code here. I thank all those who want to help me.

Code: Select all

<?php
/**
Air Override
 */
defined('_JEXEC') or die;

use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;


HTMLHelper::_('behavior.keepalive');
HTMLHelper::_('behavior.formvalidator');/*Client Side Form Validation*/

$this->form->reset( true ); // to reset the form xml loaded by the view
$this->form->loadFile( dirname(__FILE__) . '/custom_lavoro.xml'); // to load in our own version of contact.xml*/
/*$this->form->removeField('contact_subject');
$this->form->removeField('contact_message');*/
					
?>
<div class="row">
<div class="col-md-6">
<div class="com-contact__form contact-form">
	<form id="contact-form" action="<?php echo Route::_('index.php'); ?>" method="post" class="form-validate form-horizontal well">
		<?php foreach ($this->form->getFieldsets() as $fieldset) : ?>
			<?php if ($fieldset->name === 'captcha' && !$this->captchaEnabled) : ?>
				<?php continue; ?>
			<?php endif; ?>
			<?php $fields = $this->form->getFieldset($fieldset->name); ?>
			<?php if (count($fields)) : ?>
				<fieldset class="m-0">
					<?php if (isset($fieldset->label) && ($legend = trim(Text::_($fieldset->label))) !== '') : ?>
						<legend><?php echo $legend; ?></legend>
					<?php endif; ?>
					<?php foreach ($fields as $field) : ?>
					<?php echo $field->renderField(); ?>
					<?php endforeach; ?>
		        </fieldset>
			<?php endif; ?>
		<?php endforeach; ?>
		<div class="control-group">
			<div class="controls">
				<button class="btn btn-primary validate" type="submit"><?php echo Text::_('COM_CONTACT_CONTACT_SEND'); ?></button>
				<input type="hidden" name="option" value="com_contact">
				<input type="hidden" name="task" value="contact.submit">
				<input type="hidden" name="return" value="<?php echo $this->return_page; ?>">
				<input type="hidden" name="id" value="<?php echo $this->item->slug; ?>">
				<?php echo HTMLHelper::_('form.token'); ?>
			</div>
		</div>
	</form>
</div>
</div>
<div class="col-md-6">
<?php
jimport('joomla.application.module.helper');
// this is where you want to load your module position
$modules = JModuleHelper::getModules('Lavoro'); 
foreach($modules as $module)
{echo JModuleHelper::renderModule($module);}
?>
</div>
</div>
File xml custom_lavoro

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<form>
	<fieldset name="lavoro" addruleprefix="templates/cassiopeia/html/com_contact/contact" label="COM_CONTACT_CONTACT_DEFAULT_LABEL">
		<field
			name="spacer"
			type="spacer"
			label="COM_CONTACT_CONTACT_REQUIRED"
			class="text"
		/>

		<field
			name="contact_name"
			type="text"
			label="COM_CONTACT_CONTACT_EMAIL_NAME_LABEL"
			id="contact-name"
			size="30"
			filter="string"
			required="true"
		/>

		<field
			name="contact_email"
			type="email"
			label="COM_CONTACT_EMAIL_LABEL"
			id="contact-email"
			size="30"
			filter="string"
			validate="ContactEmail"
			autocomplete="email"
			required="true"
		/>
	    <field name="attivita" type="list" default="" required="true" label="Tipologia attività" description="">
        <option value="">Seleziona</option>
        <option value="0">Option 1</option>
        <option value="1">Option 2</option>
        </field>
		
		<field name="telefono" type="text" default="" label="Telefono" description="" filter="string" />

<field name="proprieta" type="radio" default="" label="Sei il proprietario dell'immobile o in locazione?" description="" class="btn-group" required="true">
  <option value="1">Proprietario</option>
  <option value="0">Locatario</option>
</field>

<field name="indirizzo" type="text" default="" label="Indirizzo attività" description="" required="true" filter="string"/>

		<field
			name="contact_email_copy"
			type="checkbox"
			label="COM_CONTACT_CONTACT_EMAIL_A_COPY_LABEL"
			id="contact-email-copy"
			default="0"
		/>
	</fieldset>

	<fieldset name="captcha">
		<field
			name="captcha"
			type="captcha"
			label="COM_CONTACT_CAPTCHA_LABEL"
			validate="captcha"
			namespace="contact"
		/>
	</fieldset>
</form>

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2018
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: How to remove contact_subject from the contact form?

Post by SharkyKZ » Wed Oct 27, 2021 5:19 pm

This needs to be done in a plugin using onContentPrepareForm event, not in the layout.

gmarra83
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Tue Nov 06, 2018 7:20 pm

Re: How to remove contact_subject from the contact form?

Post by gmarra83 » Thu Oct 28, 2021 8:09 am

SharkyKZ wrote:
Wed Oct 27, 2021 5:19 pm
This needs to be done in a plugin using onContentPrepareForm event, not in the layout.
Good morning,
In the meantime, thanks for the reply. Can you kindly give me an example?

I can't understand how despite having reset the xml file and adding a new one where there is no longer the contact_subject field, it is not possible to pass the server-side validation.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2018
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: How to remove contact_subject from the contact form?

Post by SharkyKZ » Thu Oct 28, 2021 8:27 am

https://docs.joomla.org/Adding_custom_f ... g_a_plugin
I can't understand how despite having reset the xml file and adding a new one where there is no longer the contact_subject field, it is not possible to pass the server-side validation.
It's because you're altering the form in the view (presentation layer) while validation is triggered by the controller and performed by the model.

gmarra83
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Tue Nov 06, 2018 7:20 pm

Re: How to remove contact_subject from the contact form?

Post by gmarra83 » Thu Oct 28, 2021 8:51 am

SharkyKZ wrote:
Thu Oct 28, 2021 8:27 am
https://docs.joomla.org/Adding_custom_f ... g_a_plugin
I can't understand how despite having reset the xml file and adding a new one where there is no longer the contact_subject field, it is not possible to pass the server-side validation.
It's because you're altering the form in the view (presentation layer) while validation is triggered by the controller and performed by the model.
Thanks, I'll try.

Sure a nice ride to remove a field from a form. It should be as simple as deciding whether or not to insert the recaptcha. You will tell me to use extensions to create custom forms, which is also right for complex processes but for simple forms com_contact should suffice.

My goal was to create a custom form using the additional Joomla fields. The contact_subject and contact_message fields should have been deleted from the components / com_contact / forms file but at the next update everything would have been skipped, so I decided to create a custom xml view file by deleting the aforementioned fields and even this was not enough.

I hope to be able to share the solution with everyone, I believe that the topic in question may be of interest to the whole Joomla community.


Post Reply

Return to “General Questions/New to Joomla! 4.x”