Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate an unique value with engine plugin #593

Open
xhava opened this issue May 14, 2013 · 31 comments
Open

validate an unique value with engine plugin #593

xhava opened this issue May 14, 2013 · 31 comments

Comments

@xhava
Copy link

xhava commented May 14, 2013

hi someone can help me please.i have in my database a table named users but the username is unique. so how i can validate this with validate engine in jtable please someone can help me

@Xinne
Copy link

Xinne commented May 14, 2013

Check out the ajaxselector jQuery validation engine documentation. jQuery's validation engine has a build-in method for this. (demo doesn't work, download from github and run on your own server to see it working!). There is some documentation available about implementing the validation engine into jTables. Although this doesn't cover a step-by-step how-to on the ajax request validation method, you should be able to reach your goal. If you need any further assistention, i'll be here. Have fun!

@xhava
Copy link
Author

xhava commented May 15, 2013

really a lot of thanks xinne. believe me i have been looking for this since couple weeks ago. so i feel that this is gonna be my solution . thnks. and im sorry if my english is pretty bad.

@xhava
Copy link
Author

xhava commented May 15, 2013

my friend i have achieved to validate that. but just a message shows me when i try insert a value wich already exists ... but when i insert another value. a valid value. and if i try to send my form to the server ... it does not work. i saw in the example some like this .... but i dunno how to use this with jtable plugin. please explain me a little about this.

<script> // This method is called right before the ajax form validation request // it is typically used to setup some visuals ("Please wait..."); // you may return a false to stop the request function beforeCall(form, options){ if (window.console) console.log("Right before the AJAX form validation call"); return true; } // Called once the server replies to the ajax form validation request function ajaxValidationCallback(status, form, json, options){ if (window.console) console.log(status); if (status === true) { alert("the form is valid!"); // uncomment these lines to submit the form to form.action // form.validationEngine('detach'); // form.submit(); // or you may use AJAX again to submit the data } } jQuery(document).ready(function(){ jQuery("#formID").validationEngine({ ajaxFormValidation: true, ajaxFormValidationMethod: 'post', onAjaxFormComplete: ajaxValidationCallback }); }); </script>

@Xinne
Copy link

Xinne commented May 15, 2013

I'll dive into it this afternoon!

@xhava
Copy link
Author

xhava commented May 15, 2013

thanks my friend u know i have some like this...

this next script is my jtable... and i add th class ,ajax[ajaxNombreTipoCursoCallPhp] to the field named nombre_tipo_curso.

$(function(){

        //Prepare jTable
        $('#tipocursotable').jtable({
            title: 'Tipos de Curso',
            actions: {
                listAction: 'php/tipocurso/acciontipocurso.php?action=list',
                createAction: 'php/tipocurso/acciontipocurso.php?action=create',
                updateAction: 'php/tipocurso/acciontipocurso.php?action=update',
                deleteAction: 'php/tipocurso/acciontipocurso.php?action=delete'
            },
            fields: {
                id_tipo_curso: {
                    key: true,
                    create: false,
                    edit: false,
                    list: false
                },
                nombre_tipo_curso: {
                    title: 'Tipo de Curso',
                    width: '50%',

                },
                abreviacion_tipo_curso: {
                    title: 'Abreviación',
                    width: '50%'
                }//,
                //RecordDate: {
                //  title: 'Record date',
                //  width: '30%',
                //  type: 'date',
                //  create: false,
                //  edit: false
                //}
            },
        //Initialize validation logic when a form is created
        formCreated: function (event, data) {
            data.form.find('input[name="nombre_tipo_curso"]').addClass('validate[required],custom[onlyLetterSp],custom[maxtipocurso],ajax[ajaxNombreTipoCursoCallPhp]');
            data.form.find('input[name="abreviacion_tipo_curso"]').addClass('validate[required],custom[onlyLetterSp],custom[maxabreviaciontipocurso]');

            data.form.validationEngine();
        },
        //Validate form when it is being submitted
        formSubmitting: function (event, data) {
            return data.form.validationEngine('validate');
        },
        //Dispose validation logic when form is closed
        formClosed: function (event, data) {
            data.form.validationEngine('hide');
            data.form.validationEngine('detach');
        }
        });

        //Load person list from server
        $('#tipocursotable').jtable('load');

    });

and then in my script to validate i have some like this

,
"ajaxNombreTipoCursoCallPhp":{
"url": "php/tipocurso/validar.php",
// error
"alertTextOk": "* This name is available",
"alertText": "* This name is already taken",
"alertTextLoad": "* Validating, please wait"
},

and this messages are showed correctly.

but when i try to send the form if all values are ok .... it does not work

@Xinne
Copy link

Xinne commented May 15, 2013

What does happen when you send the form? Does the validation engine show an error?

@Xinne
Copy link

Xinne commented May 15, 2013

By the way, you're not using the provided add validation class syntax. Instead of using

        formCreated: function (event, data) {
            data.form.find('input[name="nombre_tipo_curso"]').addClass('validate[required],custom[onlyLetterSp],custom[maxtipocurso],ajax[ajaxNombreTipoCursoCallPhp]');
            data.form.find('input[name="abreviacion_tipo_curso"]').addClass('validate[required],custom[onlyLetterSp],custom[maxabreviaciontipocurso]');

            data.form.validationEngine();
        },

try using

        formCreated: function (event, data) {
            data.form.find('input[name="nombre_tipo_curso"]').addClass('validate[required,custom[onlyLetterSp],custom[maxtipocurso],ajax[ajaxNombreTipoCursoCallPhp]]');
            data.form.find('input[name="abreviacion_tipo_curso"]').addClass('validate[required,custom[onlyLetterSp],custom[maxabreviaciontipocurso]]');

            data.form.validationEngine();
        },

@TroyWitthoeft
Copy link

I've frequented to Validation Engine forums. Like many others, I also had issue with AJAX validation. So, I am definately paying attention to this conversation to see if a clear example comes from it. In the mean time, my comment here describes how to use jTable to prevent duplicate values in your database.

@Xinne
Copy link

Xinne commented May 15, 2013

@Runamok81 Jups, this'll work fine if you want to validate the 'uniqueness' after the user has submitted the form (Wich you want to do, since the clientside validation is easy to get around as a wannabee hacker). The problem here is not something like 'how to check if record with key x' exists, what appears to be the problem is that the validationengine starts crying (even tough the engine should return valid, since the form is valid) when the user tries to submit it. The chances of it being a server side issue are very slim, the inline (onblur) validation does his job without problems..

@xhava Can i find your jtable + form somewhere online?

@TroyWitthoeft
Copy link

@Xinne I've already gone a few rounds with Validation Engine's AJAX issues. I swam in source and found this problem. Doing a search for AJAX issues will reveal whole host of similar things.

@xhava A server-side solution isn't ideal. But it won't hurt, and it can be built upon when/if someone provides a working example of jTable playing nice with Validation Engine's AJAX logic. If you bright fellows can help sort out the AJAX logic at Validation Engine and get a working example of it playing nice with jTable, then I'll gladly supply the beers.

@Xinne
Copy link

Xinne commented May 15, 2013

@Runamok81 Ah, didn't notice that. Never tried to submit a form directly after leaving a field that had an ajax validator attached. In my little inside-brainstormsession some possible solutions popped up, im going to have some fun with them tonight (usually, 90% of those solutions appear to be completely nonsense if i think them trough, so to not make myself look like a complete fool ill filter them before sharing with you ) :-)

By the way, concerning the server side validation, why one should always.

@xhava
Copy link
Author

xhava commented May 16, 2013

my friend @Xinne i have done what u told me. i put this
instead of what i had.

but i does not work... if i click on the button save. and the value is a value wich is already in my database. the message is showed, the message tells that i am using a name wich is not available. but in the button appears this next message 'saving' ... and i have to close the form and again put the values

formCreated: function (event, data) {
data.form.find('input[name="nombre_tipo_curso"]').addClass('validate[required,custom[onlyLetterSp],custom[maxtipocurso],ajax[ajaxNombreTipoCursoCallPhp]]');
data.form.find('input[name="abreviacion_tipo_curso"]').addClass('validate[required,custom[onlyLetterSp],custom[maxabreviaciontipocurso]]');

        data.form.validationEngine();
    },

@xhava
Copy link
Author

xhava commented May 16, 2013

but thanks @Xinne i will try to fix this.... i hope u can give me any idea what is happening here

@xhava
Copy link
Author

xhava commented May 16, 2013

@Runamok81 I like your proposal, a few beers would be nice

@xhava
Copy link
Author

xhava commented May 16, 2013

@Xinne yes when i send my form a message apears that this name for this field is being using yet. but the button to send the form says saving. so the validation is ok. because if i put a value wich is no in my database a message is showed that that name is available. so i thinks that the plugin validation engine is working very good ,but tothe form does not matter if is a value avalaible or not. and this try to send the values regardless of whether it is right or not

@Xinne
Copy link

Xinne commented May 16, 2013

@xhava Allright! Can you confirm that the record is indeed saved n your database, even if the value is not unique? I mean, the button says it's saving, but is it really?

Some urgent issue popped up in one of my projects, after i've finished fixing that ill get back to you.

(By the way, could you check out the way to put code in a comment? It makes it much more readable. Thanks!)

@xhava
Copy link
Author

xhava commented May 17, 2013

my friend the value is not indeed saved in my database. so that part works very well. well even if i dont validate if the name is being using, the value is not saved in my database.

but i dont know if i am doing well this. cause the value is validate, and a message is showed that the name has been taken and it is not avalaible. but i in my form the button says saving. but nothing happens.....

and i can not update the value wich i put and save it. because the button says saving and saving. and i have to close the form and then i have to open it again to insert news values.

@xhava
Copy link
Author

xhava commented May 17, 2013

check this my friend i did some testing. and i did this next..

this is my html/javascrip code.

so here i have 2 values which are :
nombre_tipo_curso and abraviacion_tipo_curso.

and just the value named nombre_tipo_curso has to be an unique value in my database.

<link href="scripts/jtable/themes/metro/darkgray/jtable.css" rel="stylesheet" type="text/css" />
      <script src="scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
      <script src="scripts/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
      <script src="scripts/jtable/jquery.jtable.js" type="text/javascript"></script>
      <script src="scripts/jtable/localization/jquery.jtable.tr.js"></script>
      <link href="scripts/validationstyles.css" rel="stylesheet">
      <script src="scripts/validationjtable.js"></script>
      <script src="scripts/validation-en.js"></script>
      <div id="tipocursotable" style="width: 600px; margin: 10px auto;"></div>
      <script type="text/javascript">
      $(function(){

            //Prepare jTable
            $('#tipocursotable').jtable({
                title: 'Tipos de Curso',
                actions: {
                    listAction: 'php/tipocurso/acciontipocurso.php?action=list',
                    createAction: 'php/tipocurso/acciontipocurso.php?action=create',
                    updateAction: 'php/tipocurso/acciontipocurso.php?action=update',
                    deleteAction: 'php/tipocurso/acciontipocurso.php?action=delete'
                },
                fields: {
                    id_tipo_curso: {
                        key: true,
                        create: false,
                        edit: false,
                        list: false
                    },
                    nombre_tipo_curso: {
                        title: 'Tipo de Curso',
                        width: '50%',

                    },
                    abreviacion_tipo_curso: {
                        title: 'Abreviación',
                        width: '50%'
                    }//,
                    //RecordDate: {
                    //  title: 'Record date',
                    //  width: '30%',
                    //  type: 'date',
                    //  create: false,
                    //  edit: false
                    //}
                },
            //Initialize validation logic when a form is created
             formCreated: function (event, data) {
            data.form.find('input[name="nombre_tipo_curso"]').addClass('validate[required,custom[onlyLetterSp],custom[maxtipocurso],ajax[ajaxNombreTipoCursoCallPhp]]');
            data.form.find('input[name="abreviacion_tipo_curso"]').addClass('validate[required,custom[onlyLetterSp],custom[maxabreviaciontipocurso]]');

            data.form.validationEngine();
        },

            //Validate form when it is being submitted
            formSubmitting: function (event, data) {
                return data.form.validationEngine('validate');
            },
            //Dispose validation logic when form is closed
            formClosed: function (event, data) {
                data.form.validationEngine('hide');
                data.form.validationEngine('detach');
            }
            });

            //Load person list from server
            $('#tipocursotable').jtable('load');

        });

    </script>

and to do the validation i send the values to this script php.

@xhava
Copy link
Author

xhava commented May 17, 2013

this script makes the validation ..

so here im not using my database. i just tell that if the value inserted in the field nombre_tipo_curso is numberone the value is true and it means that this value is avalaible.

but when i click on the save button the message apears but if this value is not in my database the values are saving in my database.

i hope u are undertanding me. i really dont speak english very well but i try

<?php

/* RECEIVE VALUE */
$validateValue=$_REQUEST['fieldValue'];
$validateId=$_REQUEST['fieldId'];


$validateError= "This username is already taken";
$validateSuccess= "This username is available";



    /* RETURN VALUE */
    $arrayToJs = array();
    $arrayToJs[0] = $validateId;

if($validateValue =="numberone"){       // validate??
    $arrayToJs[1] = true;           // RETURN TRUE
    echo json_encode($arrayToJs);           // RETURN ARRAY WITH success
}else{
    for($x=0;$x<1000000;$x++){
        if($x == 990000){
            $arrayToJs[1] = false;
            echo json_encode($arrayToJs);       // RETURN ARRAY WITH ERROR
        }
    }

}

?>

@TroyWitthoeft
Copy link

It ain't easy. :-(

@xhava
Copy link
Author

xhava commented May 27, 2013

hi,someone knows how I can add a new text field depending on whether a checkbox is true or hide that text field if the checkbox is false @Runamok81 @Xinne

@Xinne
Copy link

Xinne commented May 27, 2013

Please create a new request! Is the validation-thingy working now?

@LeKP
Copy link

LeKP commented Jun 7, 2013

Hi Everyone,

Try this thing, it's work for me and i don't guarantee about if this think is suck

Go to Line 1463 And emty this line
which is

if (options.eventTrigger == "submit")

and when you attack event please cancel the submitting action by validation engine

like this

data.form.validationEngine('attach', {
onValidationComplete: function () {
return false;
}
});

Please tell me if this work! Thanks

@jramosramirez
Copy link

Gracias a los amigos del post, su informacion es de gran utilidad

@xhava
Copy link
Author

xhava commented Jun 18, 2013

Claro cualquier.duda.aqui estamos

--- Mensaje Original ---

Desde: "jramosramirez" [email protected]
Enviado: 17 de junio de 2013 16:46
Para: "hikalkan/jtable" [email protected]
Cc: "xhava" [email protected]
Asunto: Re: [jtable] validate an unique value with engine plugin (#593)

Gracias a los amigos del post, su informacion es de gran utilidad


Reply to this email directly or view it on GitHub:
#593 (comment)

@Xinne
Copy link

Xinne commented Jun 18, 2013

Sorry?

@hbernal
Copy link

hbernal commented Sep 12, 2013

@Runamok81 @Xinne @LeKP @jramosramirez @xhava

I have read the comments, I agree, the Validation Engine don't work, If you have a valid value for the field, and do click on the botton "Save", nothing happens, I join to the @xhava proposal :

@xhava A server-side solution isn't ideal. But it won't hurt, and it can be built upon when/if someone provides a working example of jTable playing nice with Validation Engine's AJAX logic. If you bright fellows can help sort out the AJAX logic at Validation Engine and get a working example of it playing nice with jTable, then I'll gladly supply the beers.

I'll gladly supply the beers, too.

@bleuscyther
Copy link

Same issue, i could not get the Form to submit even if the 'partial' ajax validation are ok.
One case where it worked :

  • on submit i evaluate the ajax validated field separately
  • I remove the validate[***] class on these fields i just evaluated
  • evaluate the form
  • put the class back
  • and execute the rest of the code

The other solution would be to create a totally independent function to evaluate the the fields ( without the validation class) and use showPrompt () to show the alerts. this function would be fired on field blur and on form submit .

I was hoping the issue was with the async true on the ajax call but putting it to false didn't fix anything.

@bleuscyther
Copy link

I see that the issue occurs when using the validation engine with another library , especially when they are both listening to the submit event. It becomes worse when you are also using the submit button with an event on submit->prevent default.
I removed the other listeners or limited their action and it worked well after that ;). Hope it will help solve the problem for you guys also.

@xhava
Copy link
Author

xhava commented Nov 26, 2013

thanks @bleuscyther it works perfectly . (y)

@Irishson
Copy link

Irishson commented Jan 2, 2014

@xhava ,Same problem as yours how to validate duplicated value when saving a record i want to have a validation that will prompt that the record is already used or exist.do you already solve this issue? i really appreciate your help pls post some sample codes.thanks and have a good day =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants