We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22427
    • 793 Posts
    Hi all,

    here's my code:
    [[!FormIt?
       &hooks=`myHook,redirect`
       &redirectTo=`35`
       &store=`1`
       &validate=`
          field1:required,
          field2:required,
    ]]
    
    <form action="[[~[[*id]]]]" method="post">
    
        <label for="field1">
            Field1:
        </label>
        <select name="field1"> 
    	  <option value="option1">option1</option>
        </select>
        
        <label for="field2">
            Field2:
        </label>
        <select name="field2"> 
    	  <option value="1">One</option>
    	  <option value="2">Two</option>
    	  <option value="3">Three</option>
        </select> 
    
        <input type="submit" value="Go!" /><br />
    </form>


    The hook snippet myHook aspects the properties field1 and field2 and uses them like this:
    $hook = $scriptProperties['hook'];
    $field1 = $hook->getValue('field1');
    $field2 = $hook->getValue('field2');
    //do something
    
    and works perfectly.

    Originally the field1 was a select field with several options, but now it's reduced to just one. So I tried to omit this field and use its now fix value directly - without changing the myHook snippet.

    I tried this code:
    [[!FormIt?
       &field1=`option1`                <<<<
       &hooks=`myHook,redirect`
       &redirectTo=`35`
       &store=`1`
       &validate=`
          field2:required,
    ]]
    
    <form action="[[~[[*id]]]]" method="post">
    
        <p>Field1 has value [[+fi.field1]]</p>    
    	
        <label for="field2">
            Field2:
        </label>
        <select name="field2"> 
    	  <option value="1">One</option>
    	  <option value="2">Two</option>
    	  <option value="3">Three</option>
        </select> 
    
        <input type="submit" value="Go!" /><br />
    </form>
    

    But this does't work; obviously myHook does not receive the value of field1.
    What am I doing wrong?

      • 4172
      • 5,888 Posts
      what about:

      $field1 = $modx->getOption('field1',$scriptProperties,'');

      in your hook - snippet

      or a hidden field with the value:

      [[!FormIt?
         &hooks=`myHook,redirect`
         &redirectTo=`35`
         &store=`1`
         &validate=`
            field2:required,
      ]]
       
      <form action="[[~[[*id]]]]" method="post">
       
          <p>Field1 has value [[+fi.field1]]</p>   
         <input type="hidden" value="option1" /> 
         <label for="field2">
              Field2:
          </label>
          <select name="field2">
            <option value="1">One</option>
            <option value="2">Two</option>
            <option value="3">Three</option>
          </select>
       
          <input type="submit" value="Go!" /><br />
      </form>
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 22427
        • 793 Posts
        I already found the solution with the hidden input field, it works. (Before I used a workaround, hiding label and select element for field1 by style display:none.)

        In the hook, what is the advantage of
        $field1 = $modx->getOption('field1',$scriptProperties,'');
        over
        $hook = $scriptProperties['hook'];
        $field1 = $hook->getValue('field1');
        (I didn't try the getOption way.)

        And just for understanding: Why it is not enough to handover the value of field1 as a property to the FormIt snippet call?

          • 3749
          • 24,544 Posts
          I think the values are nested under the 'hook' object. The getOption() method assumes a one-dimensional array, so it wouldn't be able to get them for you, unless you converted the hook object to an array and called getOption() with that as the second argument.

            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