0) ? true : false;}if($_POST['select']==""){ return "Per favore effettua una scelta";}if($_POST['name']==""){ return "Per favore inserisci il nome";}if($_POST['username']==""){ return "Per favore inserisci il tuo nome utente.";}if($_POST['city']==""){ return "Per favore inserisci il tuo luogo di residenza";}if($_POST['city']==""){ return "Per favore inserisci il tuo luogo di residenza";}if(!is_email_valid($_POST['email'])){ return "Hai inserito un indirizzo e-mail non valido!";}if(!is_url_valid($_POST['url'])){ return "Hai inserito un indirizzo web non valido!";}if($_POST['select2']==""){ return "Per favore effettua una scelta";}if($_POST['testo']==""){ return "Per favore inserisci qui la tua richiesta";}?>Thanks in advance for your help (if you can) :wink:"> Problem with server side validation - Forums

Forums

Problem with server side validation

Lucaniapolis 19 Jan, 2010
Hi all. First I'm sorry for my bad english and I hope to explain my problem. I've created my form and I used a php code to validate my form by server side validation but the problem is that the code doesn't validate all fields together but only one if I submit it. If I don't fill some fields, the code give me only one error (the first in the list) and not all. What's wrong? 😟
This is my form:

<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: 150px;">Stato</label>
    <select class="cf_inputbox validate-selection" id="select" size="1"   name="select" title="Scegli un'opzione">
    <option value="">Scegli un'opzione</option>
      <option value="Sig.">Sig.</option>
<option value="Sig.na">Sig.na</option>
<option value="Sig.ra">Sig.ra</option>

    </select>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Nome</label>
    <input class="cf_inputbox validate-alpha" maxlength="150" size="30"  id="name" name="name" title="Inserisci il tuo nome" type="text" />
    </div>
 <div class="cfclear"> </div>
</div>
  <div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Username</label>
    <input class="cf_inputbox validate-alphanum" maxlength="150" size="30"  id="username" name="username" title="Inserisci il tuo nome utente" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Citta'</label>
    <input class="cf_inputbox validate-alpha" maxlength="150" size="30"  id="city" name="city" title="Inserisci la città di residenza" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Sito Web</label>
    <input class="cf_inputbox validate-url" maxlength="150" size="30"  id="website" name="website" title="Inserisci un indirizzo web valido" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">E-mail</label>
    <input class="cf_inputbox validate-email" maxlength="150" size="30"  id="email" name="email" title="Inserisci un indirizzo e-mail valido" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>
<div class="form_element cf_dropdown">
    <label class="cf_label" style="width: 150px;">Motivo per cui ci contatti</label>
    <select class="cf_inputbox validate-selection" id="select2" size="1"  name="select2" title="Scegli un'opzione" >
    <option value="">Scegli un'opzione</option>
      <option value="Info per prodotti">Info per prodotti</option>
<option value="Info per preventivi">Info per preventivi</option>
<option value="Altre info">Altre info</option>
</select>
</div>
  <div class="cfclear"> </div>

<div class="form_item">
  <div class="form_element cf_textarea">
    <label class="cf_label" style="width: 150px;">Testo</label>
    <textarea class="cf_inputbox required" rows="3" id="testo" name="testo" title="Inserisci qui la tua richiesta" cols="30" ></textarea>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_captcha">
    <label class="cf_label" style="width: 150px;">Codice di verifica</label>
    <span>{imageverification}</span> 
    
    </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Invia" name="button_8" type="submit" /><input type="reset" name="reset" value="Reset"/>
  </div>
  <div class="cfclear"> </div>
</div>


I've made it with form wizard
and this is the code for validation:

<?php

function is_email_valid($email){
    return (preg_match("/[-a-zA-Z0-9_.+]+@[a-zA-Z0-9-]{2,}\.[a-zA-Z]{2,}/", $email) > 0) ? true : false;
}
function is_url_valid($url){
    return (preg_match("|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i", $url) > 0) ? true : false;
}
if($_POST['select']==""){
    return "Per favore effettua una scelta";
}
if($_POST['name']==""){
    return "Per favore inserisci il nome";
}
if($_POST['username']==""){
    return "Per favore inserisci il tuo nome utente.";
}
if($_POST['city']==""){
    return "Per favore inserisci il tuo luogo di residenza";
}
if($_POST['city']==""){
    return "Per favore inserisci il tuo luogo di residenza";
}
if(!is_email_valid($_POST['email'])){
    return "Hai inserito un indirizzo e-mail non valido!";
}
if(!is_url_valid($_POST['url'])){
    return "Hai inserito un indirizzo web non valido!";
}
if($_POST['select2']==""){
    return "Per favore effettua una scelta";
}
if($_POST['testo']==""){
    return "Per favore inserisci qui la tua richiesta";
}
?>


Thanks in advance for your help (if you can) :wink:
nml375 19 Jan, 2010
Hi,
The problem here could probably be best described as a logic issue. Once you execute "return" the current script ends, and no further tests are done. The solution is to add the errors to an array, and in the end see if the array is empty or not. If it's not empty, return all the errors in the array. You'll probably find a few better examples on the forum, but hopefully this one should get you started:

<?php
function is_email_valid($email){
    return (preg_match("/[-a-zA-Z0-9_.+]+@[a-zA-Z0-9-]{2,}\.[a-zA-Z]{2,}/", $email) > 0) ? true : false;
}
function is_url_valid($url){
    return (preg_match("|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i", $url) > 0) ? true : false;
}

$errors = array();

#Using empty to check for an empty result. Using =="" should do fairly well too.
#Further, using the JRequest::getString method to retrieve the data instead of directly accessing the $_POST array. 
if (empty(JRequest::getString('select', '')))
  $errors[] = "Per favore effettua una scelta";

#The non-Joomla version would look as below:
if ($_POST['name']=="")
  $errors[] = "Per favore inserisci il nome";

...
#once we've done all the checks (joomla or native style), lets see if there's any errors:
#if there is, combine all the errors (using implode) into a single string, and return this.
if (!empty($errors))
  return implode("<br />", $errors);


/Fredrik
Lucaniapolis 19 Jan, 2010
I tried your code with my fields but when I try to submit it gives me this error:

Fatal error: Can't use function return value in write context in components/com_chronocontact/libraries/chronoform.php(258) : eval()'d code on line 9
😲

Is there a documentation for Joomla version PHP? or for server side validation code? I read some posts here in forum but they didn't help me too much ... I only have more confusion now :?
nml375 19 Jan, 2010
Hi,
Could you post the actual code you used? The error usually means that you've got a single equal-sign when you intended to use two (assign value instead of comparison), where the first operand is a function:

/* This is bad! */
if (function() = "value") ...

/* This is proper */
if (function() == "value";) ...


To get a reference manual of the Joomla API, have a look at api.joomla.org

/Fredrik
GreyHead 19 Jan, 2010
Hi both,

It may be this line in Fredrik's code:
if (empty(JRequest::getString('select', '')))

Try replacing it with
$select = JRequest::getString('select', '');
if ( empty($select) )

Bob
nml375 19 Jan, 2010
My bad, thanks for pointing it out, Bob.

/Fredrik
GreyHead 19 Jan, 2010
Hi Fredrik,

No problem, I do it frequently enough to know what the error message is !!

Bob
Lucaniapolis 19 Jan, 2010
Thank you so much bob! Now it works! Thanks to fredrik too for your help 🙂
Now I know that for Joomla the things are a bit different ... I must to study this too!! If you know any manual for this please let me know thanks🙂
Really thanks again! Bye
This topic is locked and no more replies can be posted.