Skip to content

Instantly share code, notes, and snippets.

@ysanmiguel
Forked from seabrizz/modx formit
Created December 20, 2019 07:55
Show Gist options
  • Save ysanmiguel/3c526693fe394245006b88d88d9d4d97 to your computer and use it in GitHub Desktop.
Save ysanmiguel/3c526693fe394245006b88d88d9d4d97 to your computer and use it in GitHub Desktop.
modx formit validation phone, upload file, capthaV2
[[!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