Last active
December 20, 2019 07:55
-
-
Save seabrizz/c44833dcb7bb4e4b32419b86d04dff7e to your computer and use it in GitHub Desktop.
modx formit validation phone, upload file, capthaV2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[!FormIt? | |
&hooks=`recaptchav2,email` | |
&emailTpl=`sendEmail` | |
&emailSubject=`test` | |
&emailTo=`xxxxxxxxx@xxxxxxxxx` | |
&emailFrom=`xxxxxxxxxxx@xxxxxxxxxxx` | |
&redirectTo=`2` | |
&customValidators=`validFile,validPhone` | |
&validate=`name:required, | |
email:email:required, | |
phone:validPhone, | |
attach:validFile` | |
]] | |
<form method="post" enctype="multipart/form-data"> | |
<div> | |
<label for="contact_name">Name</label><br /> | |
<input id="contact_name" name="name" value="[[!+fi.name]]"/> | |
[[!+fi.error.name]] | |
</div> | |
<div> | |
<label for="contact_phone">Phone</label><br /> | |
<input id="contact_phone" name="phone" value="[[!+fi.phone]]" /> | |
[[!+fi.error.phone]] | |
</div> | |
<div> | |
<label for="contact_email">Email</label><br /> | |
<input id="contact_email" name="email" value="[[!+fi.email]]" /> | |
[[!+fi.error.email]] | |
</div> | |
<div> | |
<label for="contact_attach">File</label><br /> | |
<input type="file" id="contact_attach" name="attach" value="[[!+fi.attach]]" /> | |
[[!+fi.error.attach]] | |
</div> | |
<div> | |
<label for="contact_message">Message</label><br /> | |
<textarea id="contact_message" name="message" value="[[!+fi.message]]" /></textarea> | |
[[!+fi.error.message]] | |
</div> | |
[[!recaptchav2_render]] | |
[[!+fi.error.recaptchav2_error]] | |
<noscript> | |
<div> | |
<div style="width: 302px; height: 422px; position: relative;"> | |
<div style="width: 302px; height: 422px; position: absolute;"> | |
<iframe src="https://www.google.com/recaptcha/api/fallback?k=your-site-key" | |
frameborder="0" scrolling="no" | |
style="width: 302px; height:422px; border-style: none;"> | |
</iframe> | |
</div> | |
</div> | |
<div style="width: 300px; height: 60px; border-style: none; | |
bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; | |
background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;"> | |
<textarea id="g-recaptcha-response" name="g-recaptcha-response" | |
class="g-recaptcha-response" | |
style="width: 250px; height: 40px; border: 1px solid #c1c1c1; | |
margin: 10px 25px; padding: 0px; resize: none;" > | |
</textarea> | |
</div> | |
</div> | |
</noscript> | |
<div> | |
<input name="submit" type="submit" value="Submit"/> | |
</div> | |
[[+fi.success:is=`1`:then=` | |
<div class="alert alert-success">good [[+fi.successMessage]]</div> | |
`]] | |
[[+fi.validation_error:is=`1`:then=` | |
<div class="alert alert-danger">false [[+fi.validation_error_message]]</div> | |
`]] | |
</form> | |
----------------------------------------------------------------- | |
//validFile | |
<?php | |
$output = true; | |
// type file | |
$allowedExt = array('jpg','png'); | |
// max file size (512 Кбайт) | |
$maxFileSize = 512 * 1024; | |
// file name | |
$fileName = basename( $_FILES[$key]['name'] ); | |
// size | |
$fileSize = filesize( $_FILES[$key]['tmp_name'] ); | |
// type file | |
$fileExt = mb_strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); | |
// if file name is not empty | |
if ($fileName != '') { | |
if(!in_array($fileExt, $allowedExt)) { | |
// the file is of an invalid type | |
$errorMsg = 'Файл ' . $fileName . ' error file type'; | |
$validator->addError($key, $errorMsg); | |
$output = false; // return false | |
} | |
if($fileSize > $maxFileSize) { | |
// файл имеет размер больше максимального | |
$errorMsg = 'Размер файла '. $fileName .' up to 512KB'; | |
$validator->addError($key,$errorMsg); | |
$output = false; // return false | |
} | |
} | |
return $output; | |
------------------------------------------------------------ | |
//validPhone | |
<?php | |
$success = true; | |
$value = trim($value); | |
if (!preg_match('/^\s*(?:\+?(\d{1,3}))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$/', $value)) { | |
$success = false; | |
$validator->addError($key, 'enter phone number'); | |
} | |
return $success; | |
-------------------------------------------------------------- | |
//send email | |
name: [[+name]] <br> | |
phone: [[+phone]] <br> | |
email: [[+email]] <br> | |
file: [[+attach]] <br> | |
message: [[+message]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//mask phone
+42 555.123.4567
+1-(800)-123-4567
+7 555 1234567
+7(926)1234567
(926) 1234567
+79261234567
926 1234567
9261234567
1234567
123-4567
123-89-01
495 1234567
469 123 45 67
89261234567
8 (926) 1234567
926.123.4567
415-555-1234
650-555-2345
(416)555-3456
202 555 4567
4035555678
1 416 555 9292