by
KK » Sun Aug 13, 2017 6:59 pm
Hi,
The validation of fields in a data-bound form happens in two steps.
Let me explain. Following is an excerpt from the sample 'profile.php' of members addon -
- Code: Select all
<cms:if k_success >
<cms:db_persist_form />
<cms:if k_success >
<cms:set_flash name='success_msg' value='1' />
<cms:redirect k_page_link />
</cms:if>
</cms:if>
<cms:if k_error >
<font color='red'><cms:each k_error ><cms:show item /><br /></cms:each></font>
</cms:if>
Please notice in the code above that there are *two* <cms:if k_success> checks -
When the form is submitted, all regular <cms:input>s in the form (i.e. those which are not type 'bound') are subjected to validation and if that succeeds the first <cms:if k_success > executes.
In that block there is the <cms:db_persist_form /> tag that now actually persists all 'bound' inputs into the backend.
It is at this spot that the validation of 'bound' input takes place. If the validation succeeds, the second <cms:if k_success > block executes.
Please note that in case of failure of any of the two success blocks, the *same* <cms:if k_error > executes.
This way we have a unified method of reporting failures for all kind of inputs.
Ok, with that theory past us, let us come to your original question -
If the user leave it it blank and click submit, the error message didn't show up
A 'bound' field takes all its parameters from the actual <cms:editable> defined in the template. If that region has 'required' set to '1', the bound field will automatically be a 'required' field. If not, placing that parameter within the <cms:input type='bound'> would be futile as parameters placed here are not taken into consideration at all for 'bound' inputs.
That should explain why the following is not working in your form -
- Code: Select all
<cms:input type="bound" name="k_page_title" Label="Name" placeholder="Name" required="1" />
The 'k_page_title' is a core editable region and has its definition elsewhere in the system. By using 'bound' input you can only expose its existing functionality in the form but cannot otherwise change any parameters that have been originally defined (the 'required' parameter in your case).
Alright, so finally what can be done to make the title input as required on the form?
The answer is to use a normal <cms:input> of type 'text' with 'required' set to '1'.
This way the form won't submit (i.e. the first <cms:if k_success> block will not click) if the input is left empty.
Then upon successful submission of the form, push the value submitted though our normal input within the core 'k_page_title' field as follows -
- Code: Select all
<cms:if k_success >
<cms:db_persist_form
k_page_title = frm_my_title
/>
<cms:if k_success >
<cms:set_flash name='success_msg' value='1' />
<cms:redirect k_page_link />
</cms:if>
</cms:if>
The code above assumes your input is named 'my_title' and hence we can get its submitted value by prepending 'frm_' to its name making it 'frm_my_title'.
Here is complete working sample showing only the one input being discussed -
- Code: Select all
<cms:form
masterpage=k_member_template
mode='edit'
page_id=k_member_id
enctype="multipart/form-data"
method='post'
anchor='0'
>
<cms:if k_success >
<cms:db_persist_form
k_page_title=frm_my_title
/>
<cms:if k_success >
<cms:set_flash name='success_msg' value='1' />
<cms:redirect k_page_link />
</cms:if>
</cms:if>
<cms:if k_error >
<font color='red'><cms:each k_error ><cms:show item /><br /></cms:each></font>
</cms:if>
DisplayName:<br />
<cms:input type="text" name="my_title" label='DisplayName' style="width:200px;" required='1' /><br />
<input type="submit" name="submit" value="Save"/>
</cms:form>
Note the use of 'my_title' text input.
Hope it helps.