Problems, need help? Have a tip or advice? Post it here.
2 posts Page 1 of 1
Question : All i want to do is push some extra variable(<cms:set something_unique... />) values with databound forms.
Code: Select all
<cms:db_persist_form
      _invalidate_cache='0'
      k_page_title="<cms:show selected_month /><cms:show selected_year />"

      <cms:pages masterpage="emp/index.php" >
        pt_<cms:show emp_id />_prev_pending="<cms:get 'some_calculated_stuff_for_<cms:show emp_id />' >"
      </cms:pages>
  />


Error : ERROR! ATTRIB_NAME: Invalid char "<" (line: 19 char: 1167)
But db_persist or db_persist_form doesn't work with cms:pages or anything like this "<"


Elabrating the question for better understanding.

Code: Select all
<cms:template title='Payment Management' clonable='1'>
    <cms:editable name='pt_month_year' required='1' type="text" />

    <cms:pages masterpage="emp/index.php">
    <cms:editable name="pt_<cms:show emp_id />_prev_pending"  type='text' />
    <cms:editable name="pt_<cms:show emp_id />_salary"  type='text' />
    <cms:editable name="pt_<cms:show emp_id />_paid" type='text' />
    </cms:pages>

</cms:template>


Now if i have 20 employees, there will be 20x3 different editable regions here (as i used cms:pages tag, this editable region should be dynamic, like if i add more employees it should automatically create his/her own uniqure editable region. as emp_id is unique for each employee. )

Now moving forward.

I calculated somewhere that his (emp_id=99) previous due is
Code: Select all
<cms:show prev_due_99 /> == <cms:show topay_to_emp_99 /> - <cms:show actually_paid_to_99 />


and to implement it dynamically.
Code: Select all
<cms:pages masterpage="emp/index.php" >
<cms:set prev_due_<cms:show emp_id />="
<cms:sub "topay_to_<cms:show emp_id />" "actually_paid_to_<cms:show emp_id />" />
" scope='global' />
</cms:pages>


NOW TAKE A LOOK AT MY CODE SIRJI.
Code: Select all
  ###IF SUCCESS CONDITION 
<cms:db_persist
      _invalidate_cache='0'
      k_page_title="<cms:show selected_month /><cms:show selected_year />"
      pt_month_year="<cms:show selected_month /><cms:show selected_year />"

      <cms:pages masterpage="emp/index.php" limit='2'>
        pt_<cms:show emp_id />_prev_pending="<cms:show emp_id />.50"
      </cms:pages>
  />

### DYNAMIC FORM FIELDS
<cms:set msg ='Plant1 | Plant2 |Ladies|Foundry' />
<cms:each msg sep='|' >
  <table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
   <tr><td colspan="7"><cms:show item /></td></tr>
   <tr>
          <th>ID</th>
          <th>Name</th>
          <th>Pre. Due</th>
          <th>Salary</th>
          <th>Gross Salary</th>
          <th>To Pay</th>
          <th>Paid</th>
      </tr>
    <cms:pages masterpage="emp/index.php" order="asc" custom_field="emp_department==<cms:show item />" limit='2'>
      <tr>
        <td><cms:show emp_id /></td>
        <td><cms:show emp_firstname /> <cms:show emp_lastname /></td>
        <td>Prev Due Calculation.</td>
        <td>Prev Month Salary Calculation.</td>
        <td>Gross Salary Calculation</td>
        <td>To Pay Calculation</td>
        <td><cms:editable name="pt_<cms:show emp_id />_paid" type="bound" /></td>
    </tr>
    </cms:pages>
  </table>
</cms:each>
Manu, this can be solved with some effort. I prefer the second solution though.

(1) - via building a string with dynamic code and then executing it in a separate command. This approach is based on the fact CouchCMS runs only syntactically valid xml tags and without the first bracket '<' a tag is not treated as tag -

Code: Select all
<cms:capture into='my_code' >
    cms:db_persist
        _masterpage=k_template_name
        _mode='create'
        _auto_title='1'
        field_<cms:show '123' /> = <cms:show 'value' />
    />
</cms:capture>

<cms:set my_code = "<cms:concat '<' my_code />" />

Above example builds valid code as a string - you can check if it is okay -

Code: Select all
<br/>
Check if code is what we expect: <cms:html_encode><cms:show my_code /></cms:html_encode>
<br/>

my_code should be:
<cms:db_persist _masterpage=k_template_name _mode='create' _auto_title='1' field_123 = value />


Next, we execute that code -
Code: Select all
<cms:embed code=my_code />



(2) via modding tag db_persist. I personally used approach (1) a lot in the past and know its great limitations.. So, I wrote something to finally solve this longstanding problem.

Update: this mod has been adopted by CouchCMS. It is possible to supply dynamic params as array of fields via parameter _fields - <cms:db_persist _mode='..' _masterpage='..' _fields='' />

With above, we have a new parameter _fields, that can be used like this -
<cms:db_persist ... _fields = my_dynamic_fields >

And the value assigned to _fields must be a valid array -
Code: Select all
<cms:capture into='my_dynamic_fields' is_json='1'>
{
    "field1" : "value1",
    "field2" : "value2",
    "field3" : "value3"
}
</cms:capture>

<cms:db_persist ... _fields = my_dynamic_fields >


So, first build the array of fields and values (using cms:pages and some calculations in your case), then supply tag's _field parameter with the resulting array. Amen.

Edit: Since 02.03.2020 CouchCMS supports array of _fields as a permanent solution
2 posts Page 1 of 1