Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Client Side Form Validation

edited June 2014 in Future

I want to be able to choose which fields in a form are required. For easy validation, I decided to take advantage of ElefentCMS form validation. The only hurdle I'm facing right now is the 'verify_values' method. I would like to be able to manipulate the validation 'rules'. Judging by the source code, 'rules' is an empty array in the method's options. Too bad its value is always replaced whatever I do.

Comments

  • Is it just me or is the Client-Side validation is redundant? I've tried modifying jquery.verify_values.js to match my specifications but it seems the server-side validation still fires.

  • Alright, modified the Form 'submit' method to receive a boolean value denoting whether server-side validation is fired or not.

  • Can you show a sample of what you're trying to do with verify_values? I'm not 100% sure what you mean.

    The validation is mirrored on the client-side so that it doesn't need a round trip to the server in order to do most validation (saves a page request and improves speed), but still validates server-side because you can't trust client-side validations in the backend.

  • Ok. Unfortunately, I can't show a sample but I can, however, explain what it's for. I created an app which has a feature to manipulate the form found in the app's Dynamic Object. The user can change the label & placeholder of the input elements as well as make it a required field or not. Since I don't want to dynamically generate the necessary files for the form, I marked everything as required. Now what I required was an option or the ability to choose which input to validate or not.

    Right now, I had the required input elements to bear a class name of "required" and had them be validated on the client-side while the server-side doesn't validate anything. I added a boolean parameter in the Form's submit method to either validate the form or not.

  • What about using a custom callback validation rule on them? For example:

    [field_a]
    callback = "myapp\Rules::field_a_or_b"
    
    [field_b]
    callback = "myapp\Rules::field_a_or_b"
    

    Assuming a third which_field parameter that had a a or b value, the validation class might look like this:

    <?php // apps/myapp/lib/Rules.php
    
    namespace myapp;
    
    class Rules {
        public static function field_a_or_b ($val) {
            if ($_POST['which_field'] === 'a') {
                // run validations against field_a
                return (isset ($_POST['field_a']) && ! empty ($_POST['field_a']));
            } else {
                // run validations against field_b
                return (isset ($_POST['field_b']) && ! empty ($_POST['field_b']));
            }
        }
    }
    
    ?>
    

    That hard-codes the $_POST fields, but should work. You can also use the Validator class directly in the callback method:

    if (! \Validator::validate ($value, 'email')) {
        // $value is not a valid email address
    } else {
        // $value is a valid email address
    }
    

    Hope that helps.

  • More about....Client side validation and server side validation

    http://net-informations.com/faq/asp/validation.htm

    Naggar

Sign In or Register to comment.