This topic is locked
[SOLVED]

  How to add your own validation plugins HELP

4/20/2011 11:59:40 AM
PHPRunner General questions
F
fcbinfo author

Hello.
From: http://xlinesoft.com/phprunner/docs/validation_types.htm
According to the above address to create your own validation rules you must do what is there. That was done and it worked, but when you use this rule the field becomes required even if you do not click required.
The validation of email for example, is only validate ​​when typing something in the email field, but in case you use the example above the IP, even without typing anything in the field it becomes required.
How to do this only works if something is entered, the same way that works for email?
If type anything... validate it, if type nothing... just pass without validate.
I await a help ... Thank you.

F
fcbinfo author 4/20/2011

From: http://xlinesoft.com/phprunner/docs/validation_types.htm
How to add your own validation plugins
To add new validation type, you should create a file with javascript function that implements data validation and store it at <PHPRunner install folder>\source\include\validate folder, where <PHPRunner install folder> is a folder PHPRunner is installed to.
You can add any javascript code to your validation plugins. The javascript function should return nothing if the value was validated successfully and return error message if the value was not validated.
yellowbulbNote: The file name should match the function name.
Example:
As an example, let us create validation plugin that validates ip address.
Step 1. Create file ip_address.js.
Step 2. Add the following javascript function to this file:
function ip_address(sVal){

var regexp = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/;

if(!sVal.match(regexp))

return 'This field should be a valid IP address';

else

return "";

}
Step 3. Store file ip_address.js to the <PHPRunner install folder>\source\include\validate folder. In our case the folder is C:\Program Files\PHPRunner5.3\source\include\validate.
Step 4. Start PHPRunner. Now ip_address validation type is available in the list of validation types and you can use it in your projects.

N
nti 4/20/2011

I am guessing you may have the field set as required in DB...
Null: No = required

Null: Yes = Not Required.

Sergey Kornilov admin 4/20/2011

I guess you need to add code to your plugin that prevents validation from happening if nothing is entered.

N
nti 4/20/2011

I'm guessing now that Admin guess is much better than my guess!

F
fcbinfo author 4/20/2011

PHPRunner is not for me... i cant do that... dont know how !
But thanks for your helps.

F
fcbinfo author 4/20/2011

Actually what I need...
I'm from Brazil and in Brazil we have two types of identification document. One to individuals people and another for business companys.
Types are:
CPF with the mask: 999999999-99 (individual peoples)

CNPJ with mask: 99.999.999/9999-99 (business companys)
In scriptcase there a button where I set it, put the field as CPF and CNPJ and it all happens automatically.
What I needs to be done...
Start with the CPF mask: "..-" While typing the numbers if have 11 or < 11 characters stay in the CPF mask if it exceeds 11 characters to replace the mask to CNPJ ".._/__-__".
If it dont have 11 or 14 characters validate stop and show error message "wrong data"
If you have 11 characters perform validation below (I use it with jquery and jquery validator plugin)



jQuery.validator.addMethod("verificaCPF", function(value, element) {

value = value.replace('.','');

value = value.replace('.','');

cpf = value.replace('-','');

while(cpf.length < 11) cpf = "0"+ cpf;

var expReg = /^0+$|^1+$|^2+$|^3+$|^4+$|^5+$|^6+$|^7+$|^8+$|^9+$/;

var a = [];

var b = new Number;

var c = 11;

for (i=0; i<11; i++){

a[i] = cpf.charAt(i);

if (i < 9) b += (a[i] * --c);

}

if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }

b = 0;

c = 11;

for (y=0; y<10; y++) b += (a[y] * c--);

if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }

if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]) || cpf.match(expReg)) if($(element).val().length==0) return true; else return false;

}, "Wrong CPF Data");

}


or...
If you have 14 characters perform validation below



jQuery.validator.addMethod("verificaCNPJ", function(value, element) {

cnpj = value.replace(/\D/g,"");

while(cnpj.length < 14) cnpj = "0"+ cnpj;

var expReg = /^0+$|^1+$|^2+$|^3+$|^4+$|^5+$|^6+$|^7+$|^8+$|^9+$/;

var a = [];

var b = new Number;

var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];

for (i=0; i<12; i++){

a[i] = cnpj.charAt(i);

b += a[i] * c[i+1];

}

if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }

b = 0;

for (y=0; y<13; y++) {

b += (a[y] * c[y]);

}

if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }

if ((cnpj.charAt(12) != a[12]) || (cnpj.charAt(13) != a[13]) || cnpj.match(expReg) ) if($(element).val().length==0) return true; else return false;

return true;

}, "Wrong CNPJ Data");


I'm here at the computer since 7 o'clock in the morning. I tried everything I could. I believe that without dominating javascript will not be able to do it so obviously, I'm asking for help.

F
fcbinfo author 4/20/2011

Imagens from scriptcase doing that...



with correct data



Automatic change mask



with correct data cnpj



Can someone help-me with this? Im realy not able to do that !

Sergey Kornilov admin 4/20/2011

PHPRunner doesn't have input mask. It can do validation though.
If you have a valid support contract post your application to Demo Account and open a ticket at http://support.xlinesoft.com sending your Demo Account URL. 'Demo Account' button can be found on the last screen in the program.

F
fcbinfo author 4/21/2011

ATTENTION: This will not work if you put your add and edit page to "SHOW IN POPUP" option

Atenção: Isto não vai funcionar se voce escolher para que suas paginas de adicionar e editar sejam abertas em popup.
I have solved that problem and will share with all, because i know "Brazilians need it".

Portuguese: Eu resolvi o problema, porque sei que todos os brasileiros precisam disso.
First... do that: http://www.asprunner.com/forums/topic/16037-veja-como-adicionar-mascara-no-campo-com-phprunner-53/

Portuguese: Primeiro faça o que esta no link acima.
Thanks to Erich.
Your cpf_cnpj field need to stay with this name: cpf_cnpj (for this tutorial works)

Portuguese: Seu campo cpf_cnpj precisa ter este nome... cpf_cnpj
Add a field in your form called: fisica_juridica (for do that, create a new row in your customers database... fisica_juridica tinyint(4) DEFAULT NULL )

Portuguese: Adicione um campo em seu formulário chamado fisica_juridica (Para fazer isto, crie uma nova linha em seu banco de dados de clientes... fisica_juridica tinyint(4) DEFAULT NULL )
now, create another table

Portuguese: agora crie mais uma tabela dentro de seu banco de dados



CREATE TABLE `config_fis_jur` (

`cod_fis_jur` int(2) NOT NULL AUTO_INCREMENT,

`tipo` varchar(10) DEFAULT NULL,

PRIMARY KEY (`cod_fis_jur`)

) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------

-- Records

-- ----------------------------

INSERT INTO `config_fis_jur` VALUES ('1', 'Fisica');

INSERT INTO `config_fis_jur` VALUES ('2', 'Juridica');


Go to your project in phprunner and put the field fisica_juridica with Radio Button, lookup table, table: config_fis_jur, link field: cod_fis_jur, display field: tipo and mark horizontal layout if you want.

Portuguese: No seu phprunner vá até seu projeto e coloque o novo campo fisica_juridica como Radio Button, lookup table, table: config_fis_jur, link field: cod_fis_jur, display field: tipo e marque a opção horizontal layout para ficar com um visual melhor.
Create a new file called: pass_cpf.js

Portuguese: Crie um novo arquivo chamado: pass_cpf.js



function pass_cpf(value, element) {

if (value.length < 15){ //it´s 15 because get characters of mask too

value = value.replace('.','');

value = value.replace('.','');

cpf = value.replace('-','');

while(cpf.length < 11) cpf = "0"+ cpf;

var expReg = /^0+$|^1+$|^2+$|^3+$|^4+$|^5+$|^6+$|^7+$|^8+$|^9+$/;

var a = [];

var b = new Number;

var c = 11;

for (i=0; i<11; i++){

a[i] = cpf.charAt(i);

if (i < 9) b += (a[i] * --c);

}

if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }

b = 0;

c = 11;

for (y=0; y<10; y++) b += (a[y] * c--);

if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }

if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]) || cpf.match(expReg)) return 'CPF ou CNPJ Invalido';

return true;

}else{

cnpj = value.replace(/\D/g,"");

while(cnpj.length < 14) cnpj = "0"+ cnpj;

var expReg = /^0+$|^1+$|^2+$|^3+$|^4+$|^5+$|^6+$|^7+$|^8+$|^9+$/;

var a = [];

var b = new Number;

var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];

for (i=0; i<12; i++){

a[i] = cnpj.charAt(i);

b += a[i] * c[i+1];

}

if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }

b = 0;

for (y=0; y<13; y++) {

b += (a[y] * c[y]);

}

if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }

if ((cnpj.charAt(12) != a[12]) || (cnpj.charAt(13) != a[13]) || cnpj.match(expReg)) return 'CPF ou CNPJ Invalido';

return true;

};

};


Now, copy this file to your C:\PHPRUNNER INSTALATION PATH\source\include\validate

Portuguese: Agora copie este arquivo para C:\PHPRUNNER INSTALATION PATH\source\include\validate
Go to your phprunner editor and put the field cpf_cnpj to type text and validate as pass_cpf (if you do all things, it will appear in your validate as panel).

Portuguese: Va no editor de seu phprunner, e coloque o campo cpf_cnpj como tipo texto e validate as escolha a opção pass_cpf (se voce fez tudo certo, esta opção vai aparecer la).
In the JavaScript OnLoad event put this

Portuguese: No Javascript onload event, coloque isto:



$(document).ready(function(){//Equivalente ao window.onload porem mais rapido

$("input[name=value_cpf_cnpj_1]").mask("999.999.999-99",{placeholder:'_'});//Inicia o campo como CPF

$("input[name=radio_fisica_juridica_1]:radio").change(function(){

$('input[name=value_cpf_cnpj_1]').unmask();//Remove a mascara

if($(this).val()=="1"){//Acaso seja CPF

$("input[name=value_cpf_cnpj_1]").mask("999.999.999-99",{placeholder:'_'});

} else {//Acaso seja Cnpj

$("input[name=value_cpf_cnpj_1]").mask("99.999.999/9999-99",{placeholder:'_'});

}

})

});


Build the aplication.. and BE HAPPY! =P

Portuguese: Clique novamente no build, e SEJA FELIZ.
It´s get 2 full days for i do that. AND I SHARE =P. IM A GOOD PERSON!

Portuguese: 2 dias de sofrimento, tentando tudo que era possivel para fazer isto... e estou compartilhando meu trabalho. Eu sou um cara legal =P
Sorry about the bad english, and i belive this will help you!

Portuguese: Desculpem pelo ingles ruim. (e o portugues também). Foda-se, não sou professor de linguas mesmo =P

F
fcbinfo author 4/21/2011



PHPRunner doesn't have input mask. It can do validation though.
If you have a valid support contract post your application to Demo Account and open a ticket at http://support.xlinesoft.com sending your Demo Account URL. 'Demo Account' button can be found on the last screen in the program.


I'm using the demo version, but I think I'll buy PHPRunner a few days. I'm saving money for this because it is expensive and I do not have all this money. (more US$ 160 only for complete money).
I seriously believe that with this tutorial that I posted above, you will sell many phprunners for Brazilians. Because Brazil needs it for any application that will develop.
Thank you for your attention.

F
fcbinfo author 4/21/2011



I'm guessing now that Admin guess is much better than my guess!


If you do not like to help, please do not make stupid posts like this. Their responses discourage people.

Sergey Kornilov admin 4/21/2011

FcbInfo,
calling other people stupid is not the best way to get help. Make sure you being respectful to other members.

F
fcbinfo author 4/22/2011



FcbInfo,
calling other people stupid is not the best way to get help. Make sure you being respectful to other members.


It´s very simple...
If he is not ready to help, why post? To discourage people with such ignorant responses?
My problem is solved... and i share all of that here.
Thanks.