I've been using JForm recently and I am fairly new to using it, but from my testing JForm Validation doesn't work great.
Showon is great, but when you set a field required which is showon based of another fields value it is always required even when not displayed.
Required lists are not required...
Am I being really daft and missing something obvious? Or is this a flaw in the JForm Validation?
Labels |
Added:
?
|
@Bakual but surely it's fairly simple to validate again with showon? Whilst a display feature as you call it, it should un-require fields that are hidden surely?
Required lists, I am using:
<field name="term_of_agreement" type="list" required="true" label="COM_LOANSQUOTE_FORM_LBL_QUOTE_TERM_OF_AGREEMENT" description="COM_LOANSQUOTE_FORM_DESC_QUOTE_TERM_OF_AGREEMENT" hint="COM_LOANSQUOTE_FORM_LBL_QUOTE_TERM_OF_AGREEMENT" > <option value="-1">COM_LOANSQUOTE_QUOTES_TERM_OF_AGREEMENT_OPTION_0</option> <!--<option value="6">COM_LOANSQUOTE_QUOTES_TERM_OF_AGREEMENT_OPTION_6</option>--> <option value="12">COM_LOANSQUOTE_QUOTES_TERM_OF_AGREEMENT_OPTION_12</option> <option value="24">COM_LOANSQUOTE_QUOTES_TERM_OF_AGREEMENT_OPTION_24</option> <option value="36">COM_LOANSQUOTE_QUOTES_TERM_OF_AGREEMENT_OPTION_36</option> <option value="48">COM_LOANSQUOTE_QUOTES_TERM_OF_AGREEMENT_OPTION_48</option> <option value="60">COM_LOANSQUOTE_QUOTES_TERM_OF_AGREEMENT_OPTION_60</option> </field>
for instance and then rendering the field with
<?php echo $this->form->renderField($field->fieldname); ?>
Textboxes are required but lists are not.
I've just written a custom list validator using class="validate-required-list" but still, It's not required with the default required.
It's not that simple. The JavaScript handlers for showon would have to add/remove the required attribute based on the show state and the PHP code handling the submission reverse engineer the showon value to determine if the field is in a state where it should be required.
As said, in its current design it is a display attribute and nothing more. I don't think it'd be a good idea to expand this in a way where it could alter the form definition.
On a side note, a feature that allows conditional required fields would be nice.
It's not that simple. The JavaScript handlers for showon would have to add/remove the required attribute based on the show state and the PHP code handling the submission reverse engineer the showon value to determine if the field is in a state where it should be required.
The shown code is better to add a boolean data-property to the target field (i think this is not difficult, you just put this, at the same place that show / hide code is executed)
-- thus the validation handlers for all cases: required , "validate-*", ?other? can make simple boolean check and skip its validation (here you will need to update all handling code to do the boolean check)
but then i want to remind our discussion in another issue in this tracker:
even if you do the above, that is even if you consider showon during JS validation (client side)
I don't understand the question. What do you mean with "blank"?
I think it just needs an option with an empty value (no value specified) since if you only have options with values the "required" attribute doesn't make much sense. You wouldn't be able to "not select anything" then (at least not without altering the HTML in your browser).
Ok I've run some tests and I see the issue now. It's a user error due to lack of docs.
Required lists require the field option value to me ="" i.e. nothing. Whereas I was setting my default value in the list of '-1' and then setting the first option to be '-1' with text Please select assuming the validation script was checking my default value and making sure it was not the same.
I've update JDocs for future users.
Thanks
Tony
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2017-01-03 06:07:02 |
Closed_By | ⇒ | tonypartridge |
showon
is only a display feature.If you require a field conditionally, then you need to check that yourself in your code as far as I know. There is no API for that.
That should work, if not that would be a bug.