? ? Pending

User tests: Successful: Unsuccessful:

avatar laoneo
laoneo
21 Jun 2021

Summary of Changes

The subform rule can get null as value to validate. This throws the following warning:
Warning: foreach() argument must be of type array|object, null given in /var/www/html/j3/libraries/src/Form/Rule/SubFormRule.php on line 52

@Fedik can you help here with a test case in core?

Testing Instructions

  • Add the following code to the file modules/mod_custom/mod_custom.xml after line 27:
<field type="subform" name="subform" multiple="true" label="subform" 
  validate="subform" filter="unset">
  <form>
    <field type="text" name="text" label="text" />
  </form>
</field>
  • Create a new custom module and save it

Actual result BEFORE applying this Pull Request

The warning mentioned above is added to the logs of the server.

Expected result AFTER applying this Pull Request

No Warning.

avatar laoneo laoneo - open - 21 Jun 2021
avatar laoneo laoneo - change - 21 Jun 2021
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 21 Jun 2021
Category Libraries
avatar laoneo laoneo - change - 21 Jun 2021
The description was changed
avatar laoneo laoneo - edited - 21 Jun 2021
avatar Fedik
Fedik - comment - 21 Jun 2021

@laoneo do you have a default value for subform field?

avatar Fedik
Fedik - comment - 21 Jun 2021

the error probably because of wrong JSON in default value :

// Subform field may have a default value, that is a JSON string
if ($value && is_string($value))
{
$value = json_decode($value, true);
// The string is invalid json
if (!$value)
{
return null;
}
}

you can get this error with this field:

<field type="subform" name="subform" multiple="true" label="subform" 
  validate="subform" default="{'blabla', 'bad json'}">
  <form>
    <field type="text" name="text" label="text" />
  </form>
</field>
avatar laoneo
laoneo - comment - 21 Jun 2021

No default value. The problem is that I do an filter="unset" before. So it is really NULL as value.

avatar Fedik
Fedik - comment - 21 Jun 2021

hmhm, maybe a good idea also modify

// The string is invalid json
if (!$value)
{
return null;
}

to:

// The string is invalid json
if (!$value)
{
  return $this->multiple ? [] : null;
}
avatar laoneo
laoneo - comment - 21 Jun 2021

The test function needs to return true or false.

avatar Fedik
Fedik - comment - 21 Jun 2021

The problem is that I do an filter="unset" before. So it is really NULL as value.

okay, I not sure we have it in core, but such may be used in other extensions also
then maybe this:

Place it in mod_custom:

<field type="subform" name="subform" multiple="true" label="subform" 
  validate="subform" filter="unset">
  <form>
    <field type="text" name="text" label="text" />
  </form>
</field>

Add dump($value);exit; before return here:

And try save the module,
Before : you will get warning, then dump result;
After: only dump result

avatar laoneo laoneo - change - 21 Jun 2021
The description was changed
avatar laoneo laoneo - edited - 21 Jun 2021
avatar laoneo laoneo - change - 21 Jun 2021
The description was changed
avatar laoneo laoneo - edited - 21 Jun 2021
avatar laoneo
laoneo - comment - 21 Jun 2021

Ok thanks, updated the testing instructions.

avatar laoneo laoneo - change - 21 Jun 2021
The description was changed
avatar laoneo laoneo - edited - 21 Jun 2021
avatar Fedik Fedik - test_item - 21 Jun 2021 - Tested successfully
avatar Fedik
Fedik - comment - 21 Jun 2021

I have tested this item successfully on 6a9ebdc


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/34585.

avatar chmst chmst - test_item - 23 Jun 2021 - Tested successfully
avatar chmst
chmst - comment - 23 Jun 2021

I have tested this item successfully on 6a9ebdc


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/34585.

avatar chmst chmst - change - 23 Jun 2021
Status Pending Ready to Commit
avatar chmst
chmst - comment - 23 Jun 2021

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/34585.

avatar HLeithner HLeithner - close - 29 Jun 2021
avatar HLeithner HLeithner - merge - 29 Jun 2021
avatar HLeithner HLeithner - change - 29 Jun 2021
Status Ready to Commit Fixed in Code Base
Closed_Date 0000-00-00 00:00:00 2021-06-29 20:18:09
Closed_By HLeithner
Labels Added: ? ?
avatar HLeithner
HLeithner - comment - 29 Jun 2021

Thanks!

Add a Comment

Login with GitHub to post a comment