We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14883 ☆ A M B ☆
    • 450 Posts
    Is there a way to tell whether a placeholder hasn’t been set at all vs. whether it has been set with an empty value?

    I have a web form that updates a database record.

    I’m using FormIt to validate the form.

    When the page loads the first time (with an existing database record), each field should be populated by the value from the database.

    If the user edits and submits the form, but FormIt stops it & kicks it back due to validation errors, each field should be populated by the value that the user just tried to submit.

    Here’s what I’ve come up with so far for achieving this:
    <textarea name="hairstyle:required" id="hairstyle" class="inputclass">[[+fi.hairstyle:empty=`[[+hairstyle]]`]]</textarea> <span class="error">[[+fi.error.hairstyle]]</span>


    What I want this placeholder syntax to mean is "if FormIt has captured a user-entered value for this field, use that... otherwise use the value stored in the database".

    It works right on first load - [[+fi.hairstyle]] is empty, so it uses the [[+hairstyle]] value from the db instead.

    However, if the user clears out this field (a required field), FormIt stops the submission and kicks out a "This field is required" message to [[+fi.error.hairstyle]]. But - since [[+fi.hairstyle]] is "empty", the value from the database is displayed in the field. What I really want in this case is the field to be empty, so that the user can see that they left the field empty & the error message they are seeing makes sense.

    If there were a filter that triggered when the placeholder hasn’t been set at all (as opposed to when its set with an empty string), I think that would be the perfect solution here.

    If it is possible to distinguish between "not set" and "empty", I’d like to propose a :notset filter be added to the core list of filters.

    If it isn’t possible to distinguish... any suggestions for how to do this?



      • 28554
      • 201 Posts
      Yeah that’s very much possible. Download "If" from the Package repository, then try this:

      [[If
        ?subject=`[[+fi.hairstyle]`
        &operator=`==`
        &operand=``
        &then=`[[+hairstyle]]
        &else=`[[+fi.hairstyle]]`
      ]]
      


      Should do the trick smiley
        • 14883 ☆ A M B ☆
        • 450 Posts
        I can’t get If to work either. There doesn’t seem to be any way to differentiate whether [[+fi.hairstyle]] has been passed an empty value vs. whether it hasn’t been passed anything.

        I think I’ll have to maybe use a hidden field like "beensubmitted" - that would be present if the form had been submitted, but absent if it hasn’t been. Then use that combined with "If".

        I’m having more difficulties with If as well. Trying to use it to put a ’checked="checked"’ attribute on checkbox inputs if the value=1.
        [[If? &subject=`[[+hair_willingtocut]]` &operator=`==` &operand=`1` &then=`checked="checked"` &else=`[[+hair_willingtocut]]`]]


        This isn’t working when the value is 1. It may be a string vs. int problem, I suppose.


          • 28554
          • 201 Posts
          Try this:

          <input type="text" name="name" value="[[!+fi.hairstyle:empty=`[[+hairstyle]]`]]" />
          
            • 14883 ☆ A M B ☆
            • 450 Posts
            That didn’t make a difference either. I got it going with this though.

            In the form:
            <input type="hidden" name="submitted" value="yes" />


            To display the value:
            <textarea name="hairstyle:required" id="hairstyle">
            [[!If? &subject=`[[+fi.submitted]]`   
             &operator=`==`
             &operand=`yes`
             &then=`[[!+fi.hairstyle]]`
             &else=`[[+hairstyle]]`
            ]]
            </textarea> 
              • 3749
              • 24,544 Posts
              I’m not positive about this, but I think you could use a snippet placed earlier on the page (above FormIt) that would set the placeholders as you want them set.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 14883 ☆ A M B ☆
                • 450 Posts
                I’m not positive about this, but I think you could use a snippet placed earlier on the page (above FormIt) that would set the placeholders as you want them set.
                That sounds promising and possible... I’d be curious at the technique for doing that.

                In the meantime, this is the fairly reliable solution I’ve come up with.
                <?php
                #'sub' - a wrapper for 'If' snippet- checks if the form has been submitted,and displays the right placeholder accordingly
                if (!(isset($field))) { return ''; }
                $ifargs = array(
                   'subject'=>'[[+fi.submitted]]' 
                  ,'operator'=>'=='
                  ,'operand'=>'yes'
                  ,'then'=>'fi.'.$field
                  ,'else'=>$field
                );
                return $modx->runSnippet('If', $ifargs);
                ?>

                Each place I need to display a field’s value, I use this:
                [[!+[[!sub?&field=`hairstyle`]]]]


                Basically its just shorthand, to not have to write out the entire If syntax each time. But its working well, and that’s the main thing.