volosoft / jtable

A JQuery plugin to create AJAX based CRUD tables.
http://www.jtable.org
1.1k stars 506 forks source link

validate an unique value with engine plugin #593

Open xhava opened 11 years ago

xhava commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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.

Xinne commented 11 years ago

I'll dive into it this afternoon!

xhava commented 11 years ago

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 commented 11 years ago

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

Xinne commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

@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 commented 11 years ago

@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 commented 11 years ago

@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 commented 11 years ago

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 commented 11 years ago

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

xhava commented 11 years ago

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

xhava commented 11 years ago

@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 commented 11 years ago

@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 commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

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 commented 11 years ago

It ain't easy. :-(

xhava commented 11 years ago

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 commented 11 years ago

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

LeKP commented 11 years ago

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 commented 11 years ago

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

xhava commented 11 years ago

Claro cualquier.duda.aqui estamos

--- Mensaje Original ---

Desde: "jramosramirez" notifications@github.com Enviado: 17 de junio de 2013 16:46 Para: "hikalkan/jtable" jtable@noreply.github.com Cc: "xhava" salvador89_1@hotmail.com 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: https://github.com/hikalkan/jtable/issues/593#issuecomment-19577226

Xinne commented 11 years ago

Sorry?

hbernal commented 11 years ago

@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 commented 10 years ago

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

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 commented 10 years ago

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 commented 10 years ago

thanks @bleuscyther it works perfectly . (y)

Irishson commented 10 years ago

@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 =)