Magic file validation, but only for certain groups

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
User avatar
jmcgov
Veteran Member
Posts: 79
Joined: 2018-12-19 01:31
Location: Northern Ireland

Magic file validation, but only for certain groups

Post by jmcgov » 2019-07-20 21:17

Hi AppGini'rs
Another wee question, please... I have created form validation files, using the guidelines suggested in Udemy course, in essence
- creating magic files to do the input checks that calls
- an error function in the hooks footer file
It works nicely, but I want to side step the checks if I am logged in as the admin - is this possible (but only by using the hooks facility)
TIA, James

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Magic file validation, but only for certain groups

Post by pbottcher » 2019-07-22 17:42

Hi, did you do your validation via the javascript magic files?
I think you could do it directly via the hook function (footer)to add the javascript only for certain groups.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
jmcgov
Veteran Member
Posts: 79
Joined: 2018-12-19 01:31
Location: Northern Ireland

Re: Magic file validation, but only for certain groups

Post by jmcgov » 2019-07-23 10:20

Hi Pböttcher
Yes, I used javascript in magic files for validation, and placed the actual function in the footer. I'm unsure I follow your suggestion, though - could you share a sample code suggestion, that includes an alert, please?
TIA, James

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Magic file validation, but only for certain groups

Post by pbottcher » 2019-07-23 14:00

Hi,

can you share a part of your script used in the .js file for generating the alert.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
jmcgov
Veteran Member
Posts: 79
Joined: 2018-12-19 01:31
Location: Northern Ireland

Re: Magic file validation, but only for certain groups

Post by jmcgov » 2019-07-24 10:13

(1) Table is u02_person (the following is heavily based on Udemy tutorials, and works well)

(2) In hooks/u02_person-dv.js

Code: Select all

$j(function(){
	$j('#update, #insert').click(function(){
		var regLetters = /^[a-zA-Z\s]+$/;
		var regNumbers = /^[0-9\s]+$/;
		var regAlphaNum = /^[a-zA-Z0-9 _.,!"\/']+$/;
		var regEmail = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		var regURL = /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)$/;
	
		var forename = $j('#forename').val();
		var surname = $j('#surname').val();
		var mobile = $j('#mobile').val();
		var email = $j('#email').val();
		
		if(forename.length < 2 || !regLetters.test(forename)){
			return show_error('forename', 'There is an error in the "Forename" field.');
		}
		if(surname.length < 2 || !regLetters.test(surname)){
			return show_error('surname', 'There is an error in the "Surname" field.');
		}
		if(mobile.length < 11 || !regNumbers.test(mobile)){
			return show_error('mobile', 'There is an error in the "Mobile" field, format should be "07* **** ****".');
		}
		if(email.length < 7 || !regEmail.test(email)){
			return show_error('email', 'There is an error in the "Email" field, format should be [email protected]');
		}
	});
})
(3) At end of hooks/footer-extra.php, just after php close tag

Code: Select all

<script>
	function show_error(field, message){
		modal_window({
			message: '<div class="alert alert-danger">' + message + '</div>',
			title: 'Error',
			close: function(){
				$j('#' + field).focus();
				$j('#' + field).parents('.form-group').addClass('has-error'); 
			}
		});
		return false;
	}	
</script>

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Magic file validation, but only for certain groups

Post by pbottcher » 2019-07-27 09:53

Hi,

thanks for providing the code.

You could add the code that you have in the u02_person-dv.js in the hooks/u02_person.php u02_person_dv function.

Code: Select all

function u02_person_dv($selectedID, $memberInfo, &$html, &$args){
	if(isset($_REQUEST['dvprint_x'])) return;		
	ob_start(); ?>
	<script>
		$j(function(){
		$j('#update, #insert').click(function(){
		var regLetters = /^[a-zA-Z\s]+$/;
		var regNumbers = /^[0-9\s]+$/;
		var regAlphaNum = /^[a-zA-Z0-9 _.,!"\/']+$/;
		var regEmail = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		var regURL = /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)$/;
	
		var forename = $j('#forename').val();
		var surname = $j('#surname').val();
		var mobile = $j('#mobile').val();
		var email = $j('#email).val();
		
		if(forename.length < 2 || !regLetters.test(forename)){
			return show_error('forename', 'There is an error in the "Forename" field.');
		}
		if(surname.length < 2 || !regLetters.test(surname)){
			return show_error('surname', 'There is an error in the "Surname" field.');
		}
		if(mobile.length < 11 || !regNumbers.test(mobile)){
			return show_error('mobile', 'There is an error in the "Mobile" field, format should be "07* **** ****".');
		}
		if(email.length < 7 || !regEmail.test(email)){
			return show_error('email', 'There is an error in the "Email" field, format should be [email protected]');
		}
	});
})		

	</script>
		
<?php
		
	$new_layout = ob_get_contents();
	ob_end_clean();
		
	$html .= $new_layout;
}
This way you can add a check to use the $memberInfo array to check if the user belongs to admin group and do your needed action accordingly.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
jmcgov
Veteran Member
Posts: 79
Joined: 2018-12-19 01:31
Location: Northern Ireland

Re: Magic file validation, but only for certain groups

Post by jmcgov » 2019-07-29 14:20

Thanks Pböttcher, that is a nice straightforward solution. Very best, James

User avatar
jmcgov
Veteran Member
Posts: 79
Joined: 2018-12-19 01:31
Location: Northern Ireland

Re: Magic file validation, but only for certain groups

Post by jmcgov » 2019-08-06 08:56

Thanks Pböttcher, this worked great - a little code feedback, to show how I differentiated between groups using PHP mixed with JS within the hook/function you directed me to.

Code: Select all

						$j(function(){
							$j('#update, #insert').click(function(){
								var regLetters = /^[a-zA-Z\s]+$/;
								var regNumbers = /^[0-9\s]+$/;
								var regAlphaNum = /^[a-zA-Z0-9 _.,!"\/']+$/;
								var regEmail = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
								var regURL = /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)$/;
	
								var forename = $j('#forename').val();
								var surname = $j('#surname').val();
								<?php if($memberInfo[groupID] == 3){ ?> 
									var quantityMax = $j('#quantityMax').val();
								<?php } ?>
								var terms =  document.getElementById("terms").checked;
	
								if(forename.length < 2 || !regLetters.test(forename)){
									return show_error('forename', 'There is an error in the "Forename" field.');
								}
								if(surname.length < 2 || !regLetters.test(surname)){
									return show_error('surname', 'There is an error in the "Surname" field.');
								}
												
								<?php if($memberInfo[groupID] == 3){ ?> 
									if(isNaN(quantityMax) || quantityMax < 0 || quantityMax > 2000){
										return show_error('quantityMax', 'Capacity', 'There is an error in the "Capacity" field &ndash; enter the capacity in litres. If you are unsure, estimate it at about 500 and you can update it later.');
									}
									if(note4Driver.length < 7 || !regAlphaNum.test(note4Driver)){
										return show_error('note4Driver', 'There is an error in the "Note for Driver" field &ndash; if you do not need to leave a note, enter "No note". Enter letters and digits only, with simple punctuation.');
									}
								<?php } ?>
								
								if(terms === false){
									return show_error('terms', 'You cannot proceed until you "accept our terms of service".');
								}
							});
						})

Post Reply