Page 1 of 1
building a little forms module.
Posted: Fri Jun 27, 2008 9:07 am
by solaris23
Hello , I'm actually trying to build a little module with forms, and
I was wondering what should be the "action" of my forms displayed in the
smarty template. Also I'd like to know how I can get $params from
my DoAction method, I suppose it is an array, how does it work exactly ?
Thank you in advance.
(and sorry, english isn't my natural language)
Re: building a little forms module.
Posted: Fri Jun 27, 2008 9:10 am
by RonnyK
Cant you use FormBuilder, as that is an extensive module with forms....
Ronny
Re: building a little forms module.
Posted: Fri Jun 27, 2008 9:19 am
by solaris23
I have to do a control with a mysql table to validate some postal code, is it possible with
formbuilder ?
Anyway I still would like to have somes answers about the module construction.
Thanks.
Re: building a little forms module.
Posted: Fri Jun 27, 2008 2:49 pm
by solaris23
Ok I finally find a way to made a almost correct "form action" by using :
$this->CreateFormStart('default'); but I have now two problems :
When, for exemple, the submission validation failed, the user is redirect to the forms included
into the index.php page, and I would like it to stay on the same page and stop being redirecting
to index.php
And second prob, I still don't know how to pass parameters to the action method of my
module, for exemple I'd like to do : {cms_module module='formFournisseur' dest=
myname@mymail.com}
and get the "dest" param to use it in an action.
I'm waiting for advices.
Re: building a little forms module.
Posted: Fri Jun 27, 2008 3:14 pm
by calguy1000
if you look in lib/classes/class.module.inc.php:
Code: Select all
/**
* Returns the start of a module form
*
* @param string The id given to the module on execution
* @param string The action that this form should do when the form is submitted
* @param string The id to eventually return to when the module is finished it's task
* @param string Method to use for the form tag. Defaults to 'post'
* @param string Optional enctype to use, Good for situations where files are being uploaded
* @param boolean A flag to determine if actions should be handled inline (no moduleinterface.php -- only works for frontend)
* @param string Text to append to the end of the id and name of the form
* @param array Extra parameters to pass along when the form is submitted
* @param string Text to append to the <form>-statement, for instanse for javascript-validation code
*/
function CreateFormStart($id, $action='default', $returnid='', $method='post', $enctype='', $inline=false, $idsuffix='', $params = array(), $extra='')
{
$this->LoadFormMethods();
return cms_module_CreateFormStart($this, $id, $action, $returnid, $method, $enctype, $inline, $idsuffix, $params, $extra);
}
So to create a form start, that will call your action, you do something like:
$this->CreateFormStart($id,'myactionname',$returnid);
This will call the DoAction method with 'myactionname' as the action.
but it's better to not override DoAction and create an action.myactionname.php fiile and put your code in there.
Next: parameters:
a) all form elements are returned as parameters (except for checkboxes if they're not checked).
b) you can add hidden form elements to your form
c) you can pass additional parameters through the $params array in the CreateFormStart method.
Re: building a little forms module.
Posted: Fri Jun 27, 2008 3:37 pm
by solaris23
Excuse this stupid question but what are excatly the $id and $idreturn ?
I started it from the tutorial, it isn't finished at all, but maybe you could detect
somes error concerning the use of cmsms api...
Here is my code : setForms();
if($action == 'default'){
if(isset($_POST['nom'])&& isset($_POST['societe'])&&isset($_POST['email'])&&
isset($_POST['cp'])&&!empty($_POST['nom'])
&&!empty($_POST['societe'])&&!empty($_POST['mail'])&&!empty($_POST['cp'])){
if($this->setPrenom() && $this->setNom() && $this->setCom()
&& $this->setCpLoc() && $this->setTel() && $this->setMail() && $this->setSociete()){
$content = $this->commentaire . '
' . $this->adresse;
$cmsmailer =& $mod->GetModuleInstance('CMSMailer');
$this->mailDest = $params['dest'];
$this->cmsmailer->AddAdress($this->mailDest);
$this->cmsmailer->setBody($content);
$this->cmsmailer->isHTML(true);
$this->cmsmailer->SetSubject('mail fournisseur');
$this->cmsmailer->Send();
$this->message .= "Votre message a bien été envoyé.";
$this->smarty->assign('message', $message);
echo $this->ProcessTemplate('SendSuccess.tpl');
}else{
$this->smarty->assign(array(
'message'=>$this->message,
'nom'=>$this->nom,
'prenom'=>$this->prenom,
'telephone'=>$this->cp,
'mail'=>$this->mail,
'adresse'=>$this->adresse,
'commentaire'=>$this->commentaire
));
echo $this->smarty->ProcessTemplate('formuFourni.tpl');
}
}else{
if(empty($_POST['societe'])){
$this->message .= "Le champ société est vide
";
}
if(empty($_POST['nom'])){
$this->message .= "Le champ nom est vide
";
}
if(empty($_POST['cp'])){
$this->message .= "Le champ code postal est vide
";
}
if(empty($_POST['mail'])){
$this->message .= "Le champ mail est vide
";
}
if(empty($_POST['commentaire'])){
$this->message .='Le champ commentaire est vide
'.$this->mailDest;
}
$this->smarty->assign(array(
'message'=>$this->message,
'maildest'=>$this->mailDest,
'nom'=>$_POST['nom'],
'prenom'=>$_POST['prenom'],
'telephone'=>$_POST['telephone'],
'mail'=>$_POST['mail'],
'adresse'=>$_POST['adresse'],
'commentaire'=>$_POST['commentaire']
));
echo $this->processTemplate('formuFourni.tpl');
}
}
}
private function validEmail($email){
$atom = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]'; // caractères autorisés avant l'arobase
$domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; // caractères autorisés après l'arobase (nom de domaine)
$regex = '/^' . $atom . '+' . // Une ou plusieurs fois les caractères autorisés avant l'arobase
'(\.' . $atom . '+)*' . // Suivis par zéro point ou plus séparés par des caractères autorisés avant l'arobase
'@' . // Suivis d'un arobase
'(' . $domain . '{1,63}\.)+' . // Suivis par 1 à 63 caractères autorisés pour le nom de domaine séparés par des points
$domain . '{2,63}$/i'; // Suivi de 2 à 63 caractères autorisés pour le nom de domaine
// test de l'adresse e-mail
if (preg_match($regex, $email)) {
return true;
}
else {
return false;
}
}
private function validCodePostal($codepo){
$codepo = intval($codepo);
$db =& $this->GetDb();
$sql = "SELECT loc_nom FROM localite WHERE loc_cp = $codepo";
$dbresult = & $db->Execute($sql);
if($dbresult && $row = $dbresult->FetchRow()){
$this->localite = $row['loc_nom'];
return true;
}else{
return false;
}
}
private function veriftel($tel){
if(strlen($tel)== 10){
return true;
}else{
return false;
}
}
private function setMail(){
if(validMail($_POST["mail"])){
$this->mail = $_POST["mail"];
return true;
}else{
$this->message .= "Adresse email invalide
";
return false;
}
}
private function setPrenom(){
$this->prenom = $_POST['prenom'];
}
private function setNom(){
$this->nom = $_POST['nom'];
}
private function setCom(){
$this->commentaire = $_POST['commentaire'];
}
private function setCpLoc(){
if(valideCodePostal($_POST["cp"])){
$this->cp = $_POST["cp"];
}else{
$this->message .="Code postal invalide
";
return false;
}
}
private function setTel(){
if(veriftel($_POST['telephone'])){
$this->telephone = $_POST["telephone"];
return true;
}else{
$this->message .= "telephone invalide
";
return false;
}
}
private function setAddresse(){
$this->adresse = $_POST['adresse'];
}
private function setForms(){
$this->smarty->assign(array(
'formStart'=>$this->CreateFormStart($id,'default',$returnid),
'formEnd'=>$this->CreateFormEnd()
));
}
private function setSociete(){
$this->societe = $_POST['societe'];
}
}
?>
Re: building a little forms module.
Posted: Fri Jun 27, 2008 3:48 pm
by calguy1000
The $id is the form id. It's used on frontend displays when there are multiple forms on a single page, and also to specify where the output should be returned to (for inline forms).
The $returnid is the id of the destination page. On the frontend, it'll be the id of the page that the form was on that originated the request. In the admin section, it's empty.
I would suggest you
a) do not override doaction
use action.xxxxxx.php files where xxxxxx is the action that you want to handle.
b) use $params and pass them around all over the place if you have to, don't use $_POST in your functions
Re: building a little forms module.
Posted: Fri Jun 27, 2008 3:59 pm
by solaris23
Thank you very much for your advices.