I am trying to validate the input of my form on server side, but I always get true from the validator. What am I doing wrong?
.../templates/customer_registration.xml
<?xml version="1.0" encoding="UTF-8"?>
<form name="customer_registration_form" title="COM_CANTEEN_CUSTOMER_REGISTRATION_TITLE" description="COM_CANTEEN_CUSTOMER_REGISTRATION_DESCRIPTION" buttonLabel="COM_CANTEEN_CUSTOMER_REGISTRATION_BUTTON_LABEL">
<fields name="customer_registration">
<fieldset name="customer_registration">
<field name="name" type="text" class="required validate-username" size="80" minLength="4" maxLength="255"
label="COM_CANTEEN_CUSTOMER_REGISTRATION_NAME_LABEL" description="COM_CANTEEN_CUSTOMER_REGISTRATION_NAME_DESCRIPTION" message="COM_CANTEEN_CUSTOMER_REGISTRATION_NAME_MESSAGE" />
<field name="age" type="integer" class="required" default="6" first="1" last="150" step="1"
label="COM_CANTEEN_CUSTOMER_REGISTRATION_AGE_LABEL" description="COM_CANTEEN_CUSTOMER_REGISTRATION_AGE_DESCRIPTION" message="COM_CANTEEN_CUSTOMER_REGISTRATION_AGE_MESSAGE" />
<field name="permanent_orderer" type="checkbox" default="1"
label="COM_CANTEEN_CUSTOMER_REGISTRATION_PERMANENT_ORDERER_LABEL" description="COM_CANTEEN_CUSTOMER_REGISTRATION_PERMANENT_ORDERER_DESCRIPTION" message="COM_CANTEEN_CUSTOMER_REGISTRATION_PERMANENT_ORDERER_MESSAGE" />
</fieldset>
</fields>
</form>
.../templates/basic_form_layout.php
<?php
extract($displayData);
JHtml::_('behavior.tooltip');
?>
<form action="<?php echo JRoute::_('index.php?option=com_canteen&controller=main&action=form_test'); ?>"
method="post" name="<?php echo $form->getAttribute('name'); ?>" class="<?php echo $form->getAttribute('class'); ?>">
<legend><?php echo JText::_($form->getAttribute('title')); ?></legend>
<?php
echo JText::_($form->getAttribute('description'));
foreach (array_keys($form->getFieldsets()) as $fieldset)
echo $form->renderFieldset($fieldset);
?>
<div>
<?php echo JHtml::_('form.token'); ?>
<button type="submit"><?php echo JText::_($form->getAttribute('buttonLabel')); ?></button>
</div>
</form>
.../MainController.php
/**
* @action
* @allow("public")
*/
public function formTest(iRequest $request, iResponse $response){
$templateDir = JPATH_COMPONENT . '/Canteen/presentation/templates';
$form = JForm::getInstance('customer_registration', $templateDir . '/customer_registration.xml');
$formData = $request->getPostData()->getArray();
$filtered = $form->filter($formData);
foreach ($filtered as $fieldset => $fields)
foreach ($fields as $field => $value)
$form->setValue($field, $fieldset, $value);
$result = $form->validate($filtered);
$content = new HtmlFragment();
$content->addTemplate($response->getTemplate('basic_form_layout.php'), array('form' => $form));
$content->addText(var_export($result, true));
$response->setContent($content);
}
I checked the $filtered
variable; it contains the filtered data without the CSRF token, something like: {'customer_registration': {name: "", age: "6", permanent_orderer: "1"}}
. Actually no matter what data I give to the $form->validate($data)
, it always returns true. I'd like to use JForm
to validate and display error messages. Any idea why this does not work?