?
avatar tonypartridge
tonypartridge
22 Dec 2016

I've been using JForm recently and I am fairly new to using it, but from my testing JForm Validation doesn't work great.

  1. 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.

  2. Required lists are not required...

Am I being really daft and missing something obvious? Or is this a flaw in the JForm Validation?

avatar tonypartridge tonypartridge - open - 22 Dec 2016
avatar joomla-cms-bot joomla-cms-bot - change - 22 Dec 2016
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 22 Dec 2016
avatar Bakual
Bakual - comment - 22 Dec 2016

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.

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.

Required lists are not required...

That should work, if not that would be a bug.

avatar tonypartridge
tonypartridge - comment - 22 Dec 2016

@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.

avatar mbabker
mbabker - comment - 22 Dec 2016

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.

avatar ggppdk
ggppdk - comment - 22 Dec 2016

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)

  • the value of the field used in showon is not considered by server-side validation, thus you will have a validation failure there
avatar Bakual
Bakual - comment - 22 Dec 2016

The required attribute in list fields seem to work fine:
required
xml

It may depend on the browser of course. That is tested in Chrome on Windows.

avatar tonypartridge
tonypartridge - comment - 29 Dec 2016

@bakual so does the first option need to be blank? Or have a value off ""?

avatar Bakual
Bakual - comment - 1 Jan 2017

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).

avatar tonypartridge
tonypartridge - comment - 3 Jan 2017

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

avatar tonypartridge tonypartridge - change - 3 Jan 2017
Status New Closed
Closed_Date 0000-00-00 00:00:00 2017-01-03 06:07:02
Closed_By tonypartridge
avatar tonypartridge tonypartridge - close - 3 Jan 2017

Add a Comment

Login with GitHub to post a comment