Therefor I made two radio buttons ("upload" and "post"). Checking one of the radio buttons is required.
If "upload" is checked a file-upload field is shown and required.
If "Post" is checked the file-upload field is not shown and should not be required.
I am not a developer so after a whole day of searching these forums I came up with this. (I know 'tables', shame on me) ;-)
Form code (only the part for this forum thread)
<?php
$doc =& JFactory::getDocument();
$script = "window.addEvent('domready', function() {
$('fileupload').setStyle('display', 'none');
$('upload').addEvent('click', function(event) {
if ( $('upload').checked === true ) {
$('fileupload').setStyle('display', 'block');
}
});
$('post').addEvent('click', function(event) {
if ( $('post').checked === true ) {
$('fileupload').setStyle('display', 'none');
}
});
});
";
$doc->addScriptDeclaration( $script );
?>
<table class="cform" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="cf_titlefield" valign="top">Naam van de hond</td>
<td valign="top"><input class="cf_inputbox required" maxlength="150" size="40" id="naamhond" name="naamhond" type="text" value="<?php echo $_POST['naamhond']; ?>" /></td>
</tr>
<tr>
<td class="cf_titlefield" valign="top">Kopie van de stamboom</td>
<td valign="top">
<input value="Upload" class="radio validate-one-required" id="upload" name="stamboom" type="radio" /> als bijvoegsel uploaden <input class="cf_inputbox" size="20" id="fileupload" name="fileupload" type="file"><br />
<input value="Post" class="radio" id="post" name="stamboom" type="radio" />
per post opgestuurd</td>
</tr>
<tr>
<td class="cf_titlefield" valign="top">Geslacht </td>
<td valign="top"><input value="Reu" class="radio validate-one-required" id="reu" name="geslacht" type="radio" /> Reu<br />
<input value="Teef" class="radio" id="teef" name="geslacht" type="radio" /> Teef</td>
</tr>
<tr>
<td class="cf_titlefield" valign="top"> </td>
<td valign="top"><input value="Verzend" name="undefined" type="submit" /></td>
</tr>
</table>
Server Side validation Code: (Validation tap)
<?php
if(!is_file($_FILES['fileupload']['tmp_name'])) return "you must upload some file";
?>
2 Problems occur.
Problem 1: The file upload field is allways required now. How can I make it only required when the "upload" radio is checked.
Problem 2: I managed to repopulate the "naamhond" field when the file upload error returned an empty form but how to do that with the radio buttons that where already checked by the user.
Someone, please help me out?