Server side validation: highlight the missing fields

pat01 25 Sep, 2010
Hello

I use the server side validation (in my case all validation by JavaScript is and must be turned off!).
This works perfect so far ==> showing the error messages for the fields with missing data on top of the page and showing the whole form again.

But if the form is returned in case of empty fields, all the required fields with no value or with a wrong value should be highlighted, just like the validation by JS (a red border for example).

How can I do this?

My approach was to add some code to the files elements.php:

On top of the file:
<?php
if($_POST['name'] == '') {
$error-cs = "required";
}
?>


Inside the file:
<!--start_cf_textbox-->
[...]
<input class="{cf_class} $error-cs" maxlength="{cf_maxlength}" size="{cf_size}" title="{cf_title}" id="{cf_id}" name="{cf_name}" type="{cf_type}" />
[...]
<!--end_cf_textbox-->


But since this file is a template, it's not possible to read the POST-data(?).

Thank you very much.

Regards
Patrick
GreyHead 25 Sep, 2010
Hi pat01,

I wouldn't hack the core code but I think you can do it in the Form HTML.
<?php
if ( !$mainframe->isSite() ) { return; }
$task = JRequest::getString('task', '', 'get');
?>
. . .
<?php
$input_x = JRequest::getVar('input_x', '', 'post'); 
if ( $task && !$input_x ) {
  $style = "style='border: 1px solid red;'";
} else {
  $style = "style='border: 1px solid green;'";
}
?>
. . .
<input name='input_x' . . . <?php echo $style; ?> />
. . .


Bob
pat01 25 Sep, 2010
Hi Bob and thank you

This will work, as long as the form wizard is not used.

My goal is to find a solution where a form can be created and edited by the form wizard.
In this case I'm looking for a solution which will work globally, using the server side validation.
And of course, I don't want to hack the core code at all. This always gives to much problems when updating a component software 😫
I'm sorry, I didn't mention that before :?

Since server side validation allows the use of PHP, I'm sure there must be a way to add an extra style (by using a dynamic value) to fields which are required, but left empty.

I'm still trying, but any help is appreciated 😀

Regards
Patrick
GreyHead 26 Sep, 2010
Hi Patrick,

Hmmm . . . I'm not even sure that is possible with the constraints you have.

You can't do the style-change using only PHP in the server-side validation. You have to have code in the Form HTML box where the HTML to be served is created. Requiring the Wizard only effectively locks that.

Conceivably you could do it with a plug-in that re-parsed the Form HTML after it is created . . . ???

Bob
pat01 28 Sep, 2010
Hi Bob

You can't do the style-change using only PHP in the server-side validation. You have to have code in the Form HTML box where the HTML to be served is created. Requiring the Wizard only effectively locks that.



Yes, that's why I'm looking for a solution to add a dynamic parameter to the form template elements.php
==> <input class="{cf_class} $error-cs"

But to get this to work, it must be possible to pass a value from the server side validation which then will be used when the form template is loaded.

Or the other way around: Let elements.php fetch the posted data from the form fields.

But I have no idea how CF works technically and when elements.php is loaded. :?

Patrick
GreyHead 28 Sep, 2010
Hi Patrick,

I think that the Elements are only used when the Form Wizard is saved so there's no practical way to have them interact with the server-side info on a live form.

I'm really not sure this is possible with the constraints you have.

Bob
pat01 28 Sep, 2010
Hi Bob

Well, I guess yes, it's not possible.

I did try to add {cf_value} and {value} to elements.php, but that isn't working either 😟

Oh well, no highlighted form fields... Too bad, it would be nice to have this, like the same when using JS for validation.

But anyway, thank you very much for your help!

Patrick
This topic is locked and no more replies can be posted.