We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23018
    • 353 Posts
    Seems I'm missing something important here...

    I want to have a conditonal validation:

    [[!formIt?
    ....
    &validate=`spam:blank,
    attachment:required`
    ]]


    The attachment:required part should be flexible depending on the condition if [[+fi.uid]] was set or not.

    So far I tried the following things:

    &validate=`spam:blank,
    [[+fi.uid:notempty=``:default=`attachment:required`]]
    `
    ]]


    I also tried it uncached

    &validate=`spam:blank,
    [[!+fi.uid:notempty=``:default=`attachment:required`]]
    `
    ]] 


    Putting the whole validation in two chunks and loading them conditionally also did not help.

    [[!+fi.uid:notempty=`[[$val_not_required]]`:default=`[[$val_required]]`]]


    (cached AND uncached).

    I always end up with the required result.

    I'm sure that [[+fi.uid]] is available, so what is going wrong here?


    Kind regards,


    pepebe [ed. note: pepebe last edited this post 11 years, 1 month ago.]
      Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
      • 22427
      • 793 Posts
      Shouldn't it be just like that:
      [[+fi.uid:default=`attachment:required`]]
        • 23018
        • 353 Posts
        Quote from: ottogal at Feb 14, 2013, 07:28 AM
        Shouldn't it be just like that:
        [[+fi.uid:default=`attachment:required`]]

        Yes; that SHOULD work. BUT for some reasons it doesn't.

        &validate=`workemail:blank,
        attachment[[+fi.uid:notempty=``:default=`:required`]]:checkAttachment`


        [[+fi.uid]] is set. I can see it, BUT the attachment is still required.

        I really fail to see why.

        Thanks for your advice.

        Regards,

        pepebe
        [ed. note: pepebe last edited this post 11 years, 1 month ago.]
          Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
          • 23018
          • 353 Posts
          Funfact:

          [[+fi.uid:notempty=``:default=`:required`]] [[+fi.uid]]


          If the value of uid is 1234.

          I should get 1234 as a result.

          If uid is empty, I should get :required

          BUT what I get is: :required 1234

          How can it be empty AND !empty at the same time?

          I suppose this might be related to formit handles placeholders.

          Just for fun I encluded &store=`1` inside the formit call and added [[!FormItRetriever]] to my formit chunk. Didn't change anything.

          Is it pssible that formit placeholders don't work with output filters?

          Regards,

          pepebe [ed. note: pepebe last edited this post 11 years, 1 month ago.]
            Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
            • 4172
            • 5,888 Posts
            the placeholder [[+fi.uid]] isn't be setted before calling formit.

            You will need a snippet with something like that:

            <?php
            
            $validate = explode(',',$validate);
            
            $uid = $modx->getOption('uid',$_REQUEST,0);
            
            if (!empty($uid)){
                $validate[] = 'attachment:required';
            }
            
            $scriptProperties['validate'] = implode(',',$validate);
            
            return $modx->runSnippet('FormIt',$scriptProperties);
            

              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 23018
              • 353 Posts
              Good morning Bruno,

              Thanks for your advice.

              In other words, if I write:

              [[!formIt?
              ...
              ]]
              
              [[+fi.uid=``:default=`:required`]] - [[+fi.uid]]
              


              It should work because [[+fi.uid]] is set by formit?

              Because it doesn't.

              I still get true and false the same time. Also, I can output [[+fi.uid]] BEFORE the snippet call. Its just that the conditonal is ignoring it. Is this about MODX parsing order (inside to outside)?

              I'll try your snippet.

              Regards,

              pepebe
                Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
                • 23018
                • 353 Posts
                @Bruno: Thanks again for your advise.

                Here is my final solution:

                [[!formIt?
                ...
                &validate=`[[val_uid? &validate=`workemail:blank`]]`
                ]]
                
                <?php
                /*
                Conditional validation depending on formIt value
                Solution by: Bruno17
                Source: http://forums.modx.com/thread/82467/conditional-validation#dis-post-455090
                */
                
                $validate = explode(',',trim($validate));
                 
                $uid = $modx->getOption('uid',$_REQUEST,0);
                 
                if (!empty($uid)){
                    $validate[] = 'foto1:checkAttachment';
                    $validate[] = 'foto2:checkAttachment';
                }
                else{
                    $validate[] = 'foto1:required:checkAttachment';
                    $validate[] = 'foto2:required:checkAttachment';
                }
                 
                $output = implode(',',$validate);
                
                return $output;


                Works like a charm!

                Regards,

                pepebe
                  Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe