Attention: We have experienced a large volume of forum spam that none of the Vanilla Forums anti-spam add-ons have managed to fix. Until we have the time to migrate to another forum platform, new members will require approval. However, our applicant list contains over 20k spambots alone, so please post a chat message here to have your membership approved. You may also use the chat to post questions directly.

Thanks for your patience,
Johnny (lead developer of Elefant)
Dynamic form, validation and submission
  • Hey, I have been bashing away for a few hours and cannot for the life of me think of a good solution.

    I want to build a form where I am able to add multiple members to the database with a single button click. It starts with a row for 1 member, with the ability to add more rows to the page by clicking a [+] button.

    The /forms/members.php file only validates a single row on submission from the views/members.html file. I can't think of any way to handle arrays of strings/etc properly with the validation.

    I've been adding the rows with javascript, which is why I think i'm having problems. If i made each field in php would me life be a bit easier?

    Can anyone point me in the correct direction? Thanks.

    (something a bit like this) http://i.imgur.com/1EAfI.png

  • What I would probably do is use AJAX calls to save the data through a REST API (http://www.elefantcms.com/wiki/RESTful-APIs), instead of the usual form submission process for something like this.

    For validation, you can call $.verify_value() on individual fields like this:

    http://jsfiddle.net/KLQsr/

    See the onblur of the two form inputs in the HTML box (the JavaScript box is just a dump of the js/jquery.verify_values.js file). You can use the Form::verify_value() method on the server side in pretty much exactly the same way too, for example:

    if (! Form::verify_value ($_POST['name'], 'not empty')) {
        // validation failed
    }
    

    I do something kind of similar in Elefant's forms app (https://github.com/jbroadway/form), which does much of the dynamic stuff through knockout.js though.

    Let me know how it goes!

  • Thinking about this further, you could use an ordinary form and define a callback function to perform the validation, like this:

    https://gist.github.com/2306494

    The downside is that the form must submit to the server to be verified, which means if you've got an arbitrary number of rows to regenerate that would be more complicated than the example.

    I wonder though if the built-in validations could optionally be made array-aware, and that could then be duplicated client side in the corresponding client-side $.verify_value() function... That may allow you to solve the problem without the complex regeneration at all. I'll have to think a bit on how best that could be done.

  • I just committed a small change to Form::verify_value() and $.verify_value() that adds a new each prefix to any rule so you can validate each element of an array value against that rule. So if you take the example from the gist in my last post and use it in the latest Elefant from the master branch, you can now do this in the validation rules:

    ; <?php /*
    
    [name]
    
    type = array
    each not empty = 1
    
    [email]
    
    type = array
    each email = 1
    
    ; */ ?>
    

    This will verify that all names are not empty, and that all emails pass the email validator. Works client and server side. That should give more options in how dynamic forms can be validated now too.

  • Good lord sir, thank you! I've been hacking away at quite a few things and decided to leave my dynamic forms until I had a greater grasp of the system.

    The ability to validate arrays is fantastic :)

    :)

  • A note on the new array validation, I think I'm getting a bug when I set it up like this.

    [name]

    type = array

    each not empty = 1

    [email]

    type = array

    each skip_if_empty = 1

    each email = 1

    the 'each_skip_if_empty = 1' seems to be causing a little bother and causes it to fail.

  • Ah yes, skip_if_empty is a special rule that skips the others if the value is empty. I'll have to make a couple changes in order for the two to work together, probably very minor, but will need testing. I should have some time tomorrow for that :)

  • Just pushed two new commits that modify lib/Form.php, tests/FormTest.php, and js/jquery.verify_values.js so that you should now be able to do this:

    [name]
    
    type = array
    each not empty = 1
    
    [email]
    
    type = array
    skip_if_empty = 1
    each email = 1
    

    This should now correctly ensure that names are not empty, and that email addresses are optional, but valid if they've been entered. Note that it's just skip_if_empty=1 with no each. Let me know how it works out for you :)

Howdy, Stranger!

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

Note: You can use Markdown formatting in your comments.