Problems, need help? Have a tip or advice? Post it here.
8 posts Page 1 of 1
...went back to the beginner's tutorial Databound form "Application form"
http://www.couchcms.com/docs/concepts/databound-forms.html

I'm trying to make a one-to-many relationship between the application form and usernames, what I'm trying to achieve is that each username has his own application form cloned since each time there's an application filled, it will only be seen by the user, who is the owner.

Code from application.php:

Code: Select all
<cms:template title='Application Form' clonable='1'>
    <cms:editable name='first_name' required='1' type='text' />
    <cms:editable name='last_name' required='1' type='text' />
    <cms:editable name='email' required='1' validator='email' type='text' />
    <cms:editable name='portfolio' validator='url' type='text' />
    <cms:editable name='position' required='1' type='dropdown'
        opt_values=' Please choose=- | Interface Designer | Software Engineer | Systems Administrator | Office Manager'
    />
    <cms:editable name='salary' type='text' search_type='decimal' />
    <cms:editable name='resume' required='1' allowed_ext='pdf, doc, docx' max_size='1024' type='securefile' />
<cms:editable type='relation' name=applications' masterpage='users/index.php' reverse_has='one' />
</cms:template>


Am I going the right way?
Any help appreciated
Hi,

First of all - are you sure you want a 'one-to-many' relation between an application and the users? Because this would translate to "a user can have only one application and that application could also be related to many other users".

If you wish that a user can create only one application and that application should belong to only this user, 'one-to-one' is what would be required.

With the above arrangement, a user won't be able to have multiple applications. If that is what you wish, it is fine.
If, however, the requirement is that a user may submit multiple applications
and those applications are always related to her alone you need the classical 'many-to-one'.

I feel the last option is what you'd need.
Can you please clarify?

Thanks.
Hi, yes, I think I got it all backwards :oops:

Here's an example:
many to one.png
many to one.png (41.4 KiB) Viewed 3270 times


Any visitor can arrive at any form and submit it.
In the backend, each form is owned by an user
i.e.: Form1 is owned by User1
That's ok :) It's easy to get confused when it comes to table relations.

Anyway, you only need to modify the relation field in 'application' so that it becomes a 'many-to-one'. It is almost exactly what our advanced tutorial uses for both 'pads' as well as 'notes' (both the entities are related to one user - the one who created them).

Quoting from the advanced tutorial (http://www.couchcms.com/docs/advanced-t ... notes.html), following is the definition of notes -
Code: Select all
<cms:editable
    name='note_owner'
    type='relation'
    has='one'
    masterpage='users/index.php'
    label='Owner'
    required='1'
    no_guix='1'
/>

I think the tutorial mentioned above will be very helpful to you in implementing your solution.

Hope it helps.
I added the line

Code: Select all
<cms:editable
    name='note_owner'
    type='relation'
    has='one'
    masterpage='users/index.php'
    label='Owner'
    required='1'
    no_guix='1'
/>

to the application form,

I also added the dump tag, and now I'm seeing a "required field" error,

I'm also discovering that I need to:
Pass the owner as a querystring parameter in the URL, so it must be something like


These are the errors:
k_error_application_owner: Required field cannot be left empty
k_error: Owner: Required field cannot be left empty
k_persist_error: Owner: Required field cannot be left empty




The code so far, works if I remove the owner line:


Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title='Application Form' clonable='1'>

<cms:editable
    name='application_owner'
    type='relation'
    has='one'
    masterpage='users/index.php'
    label='Owner'
    required='1'
    no_gui='1'
/>

    <cms:editable name='first_name' required='1' type='text' />
    <cms:editable name='last_name' required='1' type='text' />
    <cms:editable name='email' required='1' validator='email' type='text' />
    <cms:editable name='portfolio' validator='url' type='text' />
    <cms:editable name='position' required='1' type='dropdown'
        opt_values=' Please choose=- | Interface Designer | Software Engineer | Systems Administrator | Office Manager'
    />
    <cms:editable name='salary' type='text' search_type='decimal' />
    <cms:editable name='resume' required='1' allowed_ext='pdf, doc, docx' max_size='1024' type='securefile' />
</cms:template>
<!doctype html>
<html>
<head>
   <meta charset="utf-8">
   <meta content="width=device-width, initial-scale=1.0" name="viewport">
   <title>Employment Application</title>
   <link href="css/bootstrap.min.css" media="screen" rel="stylesheet">
   <link href="css/styles.css" media="screen" rel="stylesheet">
</head>
<body>
<div class="container">
   <div class="header">
      <h3>Employment Application</h3>
   </div>
   <div class="body">
   
        <cms:set submit_success="<cms:get_flash 'submit_success' />" />
        <cms:if submit_success >
            <div class="alert alert-success"><strong>Success:</strong> Your application has been submitted.</div>
        </cms:if>
           
      <cms:form
            masterpage=k_template_name
            mode='create'
            enctype='multipart/form-data'
            method='post'
            anchor='0'
            >
           
            <cms:if k_success >
           
                <cms:check_spam email=frm_email />
               
                <cms:db_persist_form
                    _invalidate_cache='0'
                    _auto_title='1'
                />
               
                <cms:if k_success >
                    <cms:set_flash name='submit_success' value='1' />
                    <cms:redirect k_page_link />
                </cms:if>
            </cms:if>
           
            <cms:if k_error >
                <div class="alert alert-danger"><strong>Error:</strong>
                    <cms:each k_error >
                        <br><cms:show item />
                    </cms:each>
                </div>
            </cms:if>
           
         <div class="form-group <cms:if k_error_first_name || k_error_last_name >has-error</cms:if>">
            <label class="control-label" for="first-name">Name <span class="required">*</span></label>
            <div class="row">
               <div class="col-xs-6">
                  <cms:input class="form-control" id="first-name" name="first_name" placeholder="First" type="bound" value="" />
               </div>
               <div class="col-xs-6">
                  <cms:input class="form-control" id="last-name" name="last_name" placeholder="Last" type="bound" value="" />
               </div>
            </div>
         </div>
         <div class="form-group <cms:if k_error_email >has-error</cms:if>">
            <label class="control-label" for="email">Email Address <span class="required">*</span></label>
            <cms:input class="form-control" id="email" name="email" type="bound" value="" />
         </div>
         <div class="form-group <cms:if k_error_portfolio >has-error</cms:if>">
            <label class="control-label" for="portfolio">Portfolio Website</label>
            <cms:input class="form-control" id="portfolio" name="portfolio" type="bound" placeholder="http://" />
         </div>
         <div class="form-group <cms:if k_error_position >has-error</cms:if>">
            <label class="control-label" for="position">Which position are you applying for? <span class="required">*</span></label>
            <cms:input type='bound' class="form-control" id="position" name="position"  />   
         </div>
         <div class="form-group <cms:if k_error_salary >has-error</cms:if>">
            <label class="control-label" for="salary">Salary Requirements</label>
            <div class="row">
               <div class="col-sm-6">
                  <div class="input-group">
                     <span class="input-group-addon">$</span>
                     <cms:input class="form-control" id="salary" name="salary" type="bound" value="" />
                  </div>
               </div>
            </div>
         </div>
         <div class="form-group <cms:if k_error_resume >has-error</cms:if>">
<cms:dump />

            <label class="control-label" for="resume">Attach a Copy of Your Resume <span class="required">*</span></label>
            <cms:input id="resume" name="resume" type="bound" />
            <p class="help-block">Word or PDF Documents Only</p>
         </div>
            <div class="form-group <cms:if k_error_human >has-error</cms:if>">
            <label class="control-label" for="human">What colour is a blue apple? (4 characters required)</label>
            <cms:input class="form-control" id="human" name="human" type="text" required='1' validator='regex=/^blue$/i' />
         </div>
           
            <cms:if "<cms:not submit_success />" >
                <button class="btn btn-primary" type="submit">Submit Application</button>
            </cms:if>
           
      </cms:form>
   </div>
   <div class="footer">
      <p>&copy; <a href="http://www.couchcms.com/">CouchCMS</a> 2013</p>
   </div>
</div>
</body>
</html>
<?php COUCH::invoke(); ?>   



Any help or guidance much appreciated
ok, reading the advanced tutorial, i think I need to add routable='1'
and also add a route definition that will be used as the user id, correct?
and it will also need a file named owns_applicationform.html, right?

Code so far:

Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title='Application Form' clonable='1' routable='1'>

<cms:editable
    name='application_owner'
    type='relation'
    has='one'
    masterpage='users/index.php'
    label='Owner'
    required='1'
    no_gui='1'
/>

<cms:route
    name='who is the owner of the application'
    path='{:id}/userid'
    filters='owner'
    >
   
    <cms:route_validators
        id='non_zero_integer'
    />
</cms:route>


    <cms:editable name='first_name' required='1' type='text' />
    <cms:editable name='last_name' required='1' type='text' />
    <cms:editable name='email' required='1' validator='email' type='text' />
    <cms:editable name='portfolio' validator='url' type='text' />
    <cms:editable name='position' required='1' type='dropdown'
        opt_values=' Please choose=- | Interface Designer | Software Engineer | Systems Administrator | Office Manager'
    />
    <cms:editable name='salary' type='text' search_type='decimal' />
    <cms:editable name='resume' required='1' allowed_ext='pdf, doc, docx' max_size='1024' type='securefile' />
</cms:template>
<!doctype html>
<html>
<head>
   <meta charset="utf-8">
   <meta content="width=device-width, initial-scale=1.0" name="viewport">
   <title>Employment Application</title>
   <link href="css/bootstrap.min.css" media="screen" rel="stylesheet">
   <link href="css/styles.css" media="screen" rel="stylesheet">
</head>
<body>
<div class="container">
   <div class="header">
      <h3>Employment Application</h3>
   </div>
   <div class="body">
   
        <cms:set submit_success="<cms:get_flash 'submit_success' />" />
        <cms:if submit_success >
            <div class="alert alert-success"><strong>Success:</strong> Your application has been submitted.</div>
        </cms:if>
           
      <cms:form
            masterpage=k_template_name
            mode='create'
            enctype='multipart/form-data'
            method='post'
            anchor='0'
            >
           
            <cms:if k_success >
           
                <cms:check_spam email=frm_email />
               
                <cms:db_persist_form
                    _invalidate_cache='0'
                    _auto_title='1'
                />
               
                <cms:if k_success >
                    <cms:set_flash name='submit_success' value='1' />
                    <cms:redirect k_page_link />
                </cms:if>
            </cms:if>
           
            <cms:if k_error >
                <div class="alert alert-danger"><strong>Error:</strong>
                    <cms:each k_error >
                        <br><cms:show item />
                    </cms:each>
                </div>
            </cms:if>
           
         <div class="form-group <cms:if k_error_first_name || k_error_last_name >has-error</cms:if>">
            <label class="control-label" for="first-name">Name <span class="required">*</span></label>
            <div class="row">
               <div class="col-xs-6">
                  <cms:input class="form-control" id="first-name" name="first_name" placeholder="First" type="bound" value="" />
               </div>
               <div class="col-xs-6">
                  <cms:input class="form-control" id="last-name" name="last_name" placeholder="Last" type="bound" value="" />
               </div>
            </div>
         </div>
         <div class="form-group <cms:if k_error_email >has-error</cms:if>">
            <label class="control-label" for="email">Email Address <span class="required">*</span></label>
            <cms:input class="form-control" id="email" name="email" type="bound" value="" />
         </div>
         <div class="form-group <cms:if k_error_portfolio >has-error</cms:if>">
            <label class="control-label" for="portfolio">Portfolio Website</label>
            <cms:input class="form-control" id="portfolio" name="portfolio" type="bound" placeholder="http://" />
         </div>
         <div class="form-group <cms:if k_error_position >has-error</cms:if>">
            <label class="control-label" for="position">Which position are you applying for? <span class="required">*</span></label>
            <cms:input type='bound' class="form-control" id="position" name="position"  />   
         </div>
         <div class="form-group <cms:if k_error_salary >has-error</cms:if>">
            <label class="control-label" for="salary">Salary Requirements</label>
            <div class="row">
               <div class="col-sm-6">
                  <div class="input-group">
                     <span class="input-group-addon">$</span>
                     <cms:input class="form-control" id="salary" name="salary" type="bound" value="" />
                  </div>
               </div>
            </div>
         </div>
         <div class="form-group <cms:if k_error_resume >has-error</cms:if>">
<cms:dump />

            <label class="control-label" for="resume">Attach a Copy of Your Resume <span class="required">*</span></label>
            <cms:input id="resume" name="resume" type="bound" />
            <p class="help-block">Word or PDF Documents Only</p>
         </div>
            <div class="form-group <cms:if k_error_human >has-error</cms:if>">
            <label class="control-label" for="human">What colour is a blue apple? (4 characters required)</label>
            <cms:input class="form-control" id="human" name="human" type="text" required='1' validator='regex=/^blue$/i' />
         </div>
           
            <cms:if "<cms:not submit_success />" >
                <button class="btn btn-primary" type="submit">Submit Application</button>
            </cms:if>
           
      </cms:form>
   </div>
   <div class="footer">
      <p>&copy; <a href="http://www.couchcms.com/">CouchCMS</a> 2013</p>
   </div>
</div>
</body>
</html>
<?php COUCH::invoke(); ?>   
Hi,

The routes part is not necessary unless you want to use non-default URLs.

That said, I think we are missing a fundamental point in your solution -
by trying to associate a submitted application with a user we are assuming that the user has a valid account on the site (else what do we associate the application with?).

That doesn't seem to be the case on your site.
The visitors submitting the applications are anonymous (i.e. are not logged-in and don't have an account to login with). You now wish to somehow associate the submitted content with such users. Right?

If so, the solutions we have been discussing won't work because if the user is not logged-in she is an unknown entity to the system. In other words, all visitors look the same to the system and it cannot differentiate between two visitors unless they are logged-in.

So how do we allow an anonymous visitor the ability to see/edit the submission he made?
One way this is done in the real world is by creating a unique random token for the submitted page and giving it to the user. If the user wants to edit the page he'll need to provide the token.

Let me know if you'd want to go that way.
KK wrote: Hi,
The routes part is not necessary unless you want to use non-default URLs.

That said, I think we are missing a fundamental point in your solution -
by trying to associate a submitted application with a user we are assuming that the user has a valid account on the site (else what do we associate the application with?).

That doesn't seem to be the case on your site.
The visitors submitting the applications are anonymous (i.e. are not logged-in and don't have an account to login with). You now wish to somehow associate the submitted content with such users. Right?

Right, I'd like to associate anonymous visitor's submit a form and assign it to a specific user, defined i.e by a querystring parameter in the URL

KK wrote: If so, the solutions we have been discussing won't work because if the user is not logged-in she is an unknown entity to the system. In other words, all visitors look the same to the system and it cannot differentiate between two visitors unless they are logged-in.

So how do we allow an anonymous visitor the ability to see/edit the submission he made?
One way this is done in the real world is by creating a unique random token for the submitted page and giving it to the user. If the user wants to edit the page he'll need to provide the token.

Let me know if you'd want to go that way.

No, to keep it simple, I think there's no need for a visitor to see or edit his submission.

Thanks KK!
8 posts Page 1 of 1