Validation of HTML Form Arrays using Data::FormValidator - forms

Salutations!
I come to you with a little bit of an issue I'm experiencing with using Data::FormValidator at the moment. I will start with the code that I am currently working with; this is the template file I use to generate the form:
<form method="post" target="/">
text[] <input type="text" name="text[]" /><br />
text[] <input type="text" name="text[]" /><br />
text[] <input type="text" name="text[]" /><br />
text[] <input type="text" name="text[]" /><br />
<br />
abc <input type="radio" name="abc" value="1" /><br />
abc <input type="radio" name="abc" value="2" /><br />
abc <input type="radio" name="abc" value="3" /><br />
abc <input type="radio" name="abc" value="4" /><br />
abc <input type="radio" name="abc" value="5" /><br />
<br />
herp <input type="checkbox" name="herp" value="abc"><br />
dee <input type="checkbox" name="dee" value="dd"><br />
derp <input type="checkbox" name="derp" value="beri"><br />
<br />
<input type="submit" value="Submit!!!!" name="submit" /><br />
</form>
and this is the current code I'm using to test the array validation issues:
#!/usr/bin/env perl
use Carp;
use Data::Dumper;
use Template;
use Data::FormValidator;
use Data::FormValidator::Constraints qw(:closures);
use Dancer;
set logger => "file";
setting log_path => "./";
get '/' => sub {
template 'index.tt';
};
post '/' => sub {
my $self = shift;
my $par = params;
print Dumper($par);
my $profile = {
required => [ qw( text[] ) ],
optional => [ qw( abc herp dee derp ) ],
constraint_methods => {
'text[]' => sub {
my $self = shift;
my $val = shift;
print "Hello world. I'm validating text[], value = $val\n";
my $ret;
if($val =~ /^a/i) {
$ret = 1;
} else {
$ret = 0;
}
print "And I got: $ret\n";
return $ret;
},
},
};
print Dumper(Data::FormValidator->check($par,$profile));
return;
};
dance;
The output that I'm getting is showing that text[] is entirely invalid, which is understandable; what I'm trying to figure out is there any way to constrain the array as a whole and only remove the elements that don't match? I'm presuming that it might be doable using a filter, but I want to make sure I'm not just being silly. Any help would be appreciated.

Depending on how you're having your parameters parsed and presented to you, text[] could indeed be "entoirely invalid". I myself would not name a HTML form field 'text', just to avoid potential interactions with reserved words, and to alleviate confusion.
That being said, I would just call the field 'text' instead of 'text[]' and rely on your param parser to present $text or its equivalent as an array of values. Then iterate over them in your constraint method.
There will be a couple of gotchas, depending on the param parser that you use. For instance, CGI.pm will present multivariate fields in an array if there is more than one value submitted, but will present the same field as a scalar if only one value is submitted.

Related

Perl - how to handle multiple check boxes?

I´am searching for a solution to handle a form in Perl with multiple checkboxes. Following issue:
<input type="checkbox" name="checkbox" value="1">
<input type="checkbox" name="checkbox" value="2">
.....
<input type="checkbox" name="checkbox" value="500">
In the script:
$q = new CGI;
my $checkbox = $q->param("checkbox");
foreach ( CHECKED_CHECKBOXES )
{ do something }
I have no idea how to read out all checked checkboxes. May somebody an idea for me?
Call param in list context.
my #checkboxes = $q->param("checkbox");

sagepay form integration - Not able to decrypt the $_GET['crypt'] string returned

I have placed a test transaction using following encryption -
<?php
function pkcs5_pad($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function encryptFieldData($input)
{
$key = "use your SagePAY encryption key here";
$iv = $key;
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, "", MCRYPT_MODE_CBC, "");
if (mcrypt_generic_init($cipher, $key, $iv) != -1)
{
$cipherText = mcrypt_generic($cipher,$input );
mcrypt_generic_deinit($cipher);
$enc = bin2hex($cipherText);
}
return $enc;
}
$str = "Currency=GBP";
$datapadded = pkcs5_pad($str,16);
$cryptpadded = "#" . encryptFieldData($datapadded);
?>
<html>
<form name="pp_form" action="SagePay test url" method="post">
<input name="VPSProtocol" type="hidden" value=3.00 />
<input name="TxType" type="hidden" value=PAYMENT />
<input name="Vendor" type="hidden" value="YOUR SAGEPAY ACCOUNT NAME HERE" />
<input name="Crypt" type="hidden" value=<?php echo $cryptpadded;?> />
<p>Click here to submit
<input type="submit" value="here">
</p>
</form>
</html>
But when the SagePay returns encrypted string, I am not sure how to decrypt this encrypted data as we will have to do decrypt it while creating order at our end. Can someone help?
Thanks in advance.

'Simple' PHP script shows error in line 10, what have I done wrong? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
My freshly updated website has a contact form with php mail script which I based on the form script here.
The script is saved on my webserver, but when data is submitted, no mails are sent as there is a fault in line 10.
I honestly don't understand enough to pinpoint my error - can someone help a newbie out?
I found a temporary replacement by using Bravenet, but I'd like to use my unbranded version, if simple php scripts are as simple as they seem…
My script (kontakt.php) looks like this:
<?php
/* Set e-mail recipient */
$myemail = "edw#rdturner.co.uk";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Ihr Name");
$email = check_input($_POST['email'], "Ihre E-Mail-Adresse");
$kontaktnummer = check_input($_POST['kontaktnummer']);
$thema = check_input($_POST['them']);
$message = check_input($_POST['message']), "Worum geht's?");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail-Adresse ungültig");
}
/* Let's prepare the message for the e-mail */
$message = "Hallo!
Ihr Form ist unterwegs…:
Name: $name
E-Mail-Adresse: $email
Kontaktnummer: $kontaktnummer
Frage zum Thema? $thema
Nachricht: $message
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: danke.htm');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Hier stimmt was nicht - bitte prüfen!</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
and my submission form like this:
<form method="post" enctype="multipart/form-data" action="http://pub14.bravenet.com/emailfwd/senddata.php" accept-charset="utf-8">
<input type="hidden" name="usernum" value="1126560145">
<input type="hidden" name="cpv" value="2">
<ol><li>
<label for="name">Name (Erförderlich)</label>
<input id="name" name="name" class="text" />
</li><li>
<label for="email">E-Mail-Adresse (Erförderlich)</label>
<input id="email" name="email" class="text" />
</li><li>
<label for="kontaktnummer">Kontaktnummer (Erförderlich)</label>
<input id="kontaktnummer" name="kontaktnummer" class="text" />
</li><li>
<label for="thema">Fragen zum Thema (Erförderlich)</label></br>
<input type="checkbox" name="thema" value="unterricht" /> Unterricht</br>
<input type="checkbox" name="thema" value="übersetzungen" /> Übersetzungen</br>
<input type="checkbox" name="thema" value="dolmetschen" /> Dolmetschen</br>
<input type="checkbox" name="thema" value="faß" /> Englsich vom Faß</br>
<input type="checkbox" name="thema" value="anders" /> Andere
</li><li>
<label for="message">Worum geht's? (Erförderlich)</label>
<textarea id="message" name="message" rows="8" cols="50"></textarea>
</li><li>
<input type="image" name="imageField" id="imageField" src="images/submit.gif" class="send" />
<div class="clr"></div>
</li></ol>
</form>
Naturally I'll need to tweak the html to reflect the newly working script… but how?
Thanks in advance
Edd Turner
$message = check_input($_POST['message']), "Worum geht's?");
to
$message = check_input($_POST['message'], "Worum geht's?");
There is a ) in the wrong place.
$message = check_input($_POST['message']), "Worum geht's?");
Excessive ), try $message = check_input($_POST['message'], "Worum geht's?");'
Check out the extra ')'. The error is in this line.
Change
$message = check_input($_POST['message']), "Worum geht's?");
to
$message = check_input($_POST['message'], "Worum geht's?");

Submit check boxes doesn't reach as array to php

I want to submit a form that have many checkboxes with different values, using firebug I am sure that the checkboxes values are send to the server, but when I print_r the values of the post variable, only the last value is printed and igoring the other values.
<input type="checkbox" name="fruits" value="apple" />
<input type="checkbox" name="fruits" value="orange" />
<input type="checkbox" name="fruits" value="banana" />
and in the server when I print the request parameters
$formData = $this->getRequest()->getPost();
print_r($formData);
Only the last option is printed even I choose all of them and firebug shows them all!
I solve it by making [] in the name of the checkboxex
<input type="checkbox" name="fruits[]" value="apple" />
<input type="checkbox" name="fruits[]" value="orange" />
<input type="checkbox" name="fruits[]" value="banana" />
This is ZF way of doing the same
$fruits = new Zend_Form_Element_MultiCheckbox('fruits', array(
'multiOptions' => array(
'apple' => 'Label for apple',
'orange' => 'I am good orange',
'banana' => 'I am banana'
);
));
echo $fruits;

associated array elements in zend form

Been trying to find a solution for this for a while with not much luck...
Need to render a form with an array of checkboxes each having an associated textbox.
given an array of array('Dave'=>23,'Pete'=>12,'Si'=>43);
the resulting mark-up should yield:
<div>
<label><input type="checkbox" name="user_id[]" id="user_id-1" value="23" />Dave</label>
<label for="job-1">Job:</label><input type="text" name="job[]" id="job-1" />
</div>
<div>
<label><input type="checkbox" name="user_id[]" id="user_id-2" value="12" />Pete</label>
<label for="job-2">Job:</label><input type="text" name="job[]" id="job-2" />
</div>
<div>
<label><input type="checkbox" name="user_id[]" id="user_id-3" value="43" />Si</label>
<label for="job-3">Job:</label><input type="text" name="job[]" id="job-3" />
</div>
Complete zend noob so any help appreciuated (including decorators etc.)
Thanks peeps
Can't be done without custom elements. I'd suggest watching http://www.zendcasts.com/writing-composite-zend_form-elements/2010/03/
just create a custom decorator, extends from Zend_Form_Decorator_Abstract and define the function render, which returns the html that you define inside, for example you can do:
$html = ''; // some code html
$i = 0;
foreach ($element->getMultiOptions() as $value => $label){
$html .= '<label><input type="checkbox" name="'.$element->getName().'[]" id="'$element->getName()'-'.$i.'" value="'.$value.'" />'.$label.'</label>';
$i++;
}
return $html;