Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore null values in subform rule #34585

Merged
merged 1 commit into from
Jun 29, 2021
Merged

Ignore null values in subform rule #34585

merged 1 commit into from
Jun 29, 2021

Conversation

laoneo
Copy link
Member

@laoneo laoneo commented Jun 21, 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.

@Fedik
Copy link
Member

Fedik commented Jun 21, 2021

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

@Fedik
Copy link
Member

Fedik commented Jun 21, 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>

@laoneo
Copy link
Member Author

laoneo commented Jun 21, 2021

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

@Fedik

This comment has been minimized.

@laoneo
Copy link
Member Author

laoneo commented Jun 21, 2021

The test function needs to return true or false.

@Fedik
Copy link
Member

Fedik commented Jun 21, 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

@laoneo
Copy link
Member Author

laoneo commented Jun 21, 2021

Ok thanks, updated the testing instructions.

@Fedik
Copy link
Member

Fedik commented Jun 21, 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.

1 similar comment
@chmst
Copy link
Contributor

chmst commented Jun 23, 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.

@chmst
Copy link
Contributor

chmst commented Jun 23, 2021

RTC


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

@chmst chmst removed the PR-staging label Jun 23, 2021
@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Jun 23, 2021
@HLeithner HLeithner merged commit f325a12 into joomla:staging Jun 29, 2021
@HLeithner
Copy link
Member

Thanks!

@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Jun 29, 2021
@HLeithner HLeithner added this to the Joomla! 3.9.28 milestone Jun 29, 2021
@laoneo laoneo deleted the patch-4 branch June 30, 2021 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants