Removing Empty Fields from ARRAY_SIMPLE input

Gossamer

Active member
I'm currently working on an input form that creates an array of urls. However, it's apparently saving a bunch of empty fields into that array that I don't want.

This is my template code:
Code:
<xen:title>{xen:phrase goss_member_awards_of_x, 'username={$user.username}'}</xen:title>

<xen:form action="{xen:adminlink 'member-awards/save', $user}" class="AutoValidator" data-redirect="on">
    <fieldset>
        <xen:controlunit label="{xen:phrase goss_member_award_link}:">
            <ul class="AwardList">
                <xen:foreach loop="$memberAwardUrls" key="$choice" value="$text">
                    <li>
                        <input type="text" name="goss_memberaward_urls[]" value="{$text}" class="textCtrl" placeholder="{xen:phrase image_url}" size="50" />
                    </li>
                </xen:foreach> 
                <li>
                    <input type="text" name="goss_memberaward_urls[]" value="{$text}" class="textCtrl" placeholder="{xen:phrase image_url}" size="50" />
                </li> 
           
            </ul>
            <input type="button" value="{xen:phrase goss_member_awards_add_additional_url}" class="button smallButton FieldAdder" data-source="ul.AwardList li" />

        </xen:controlunit>
    </fieldset>
   
    <xen:submitunit save="{xen:phrase save_field}">

    </xen:submitunit>
</xen:form>

My ControllerAdmin:
PHP:
    public function actionUser()
    {
        $userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);
       
        $userModel = $this->_getUserModel();
        $user = $userModel->getUserById($userId);
       
        $urls = $user['goss_memberaward_urls'];
       
        if (!is_array($urls))
        {
            $urls = ($urls ? XenForo_Helper_Php::safeUnserialize($urls) : array());
        }   
       
        $viewParams = array(
            'user' => $user,
            'memberAwardUrls' => $urls
        );
       
        return $this->responseView(null, 'goss_memberawards_user', $viewParams);
    }
   
    public function actionSave()
    {       
        $userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);
       
       
        $awardurl = $this->_input->filter(array(
            'goss_memberaward_urls' => XenForo_Input::ARRAY_SIMPLE
        ));
       
        $dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
       
        $dw->setExistingData($userId);
       
        $dw->bulkSet($awardurl);
       
        $dw->save();
       
        //Send a response to the user, so he know that everything went fine with this action
        return $this->responseRedirect(
                    XenForo_ControllerResponse_Redirect::SUCCESS,
                    $this->getDynamicRedirect()
        );
    }

And this is what it looks like:
e535573a65c6095394dc2929ad14ba79.png


How can I make sure it's not saving information from the empty fields?
 
I think you'd just want to loop through the input and remove the entries that are empty strings (or alternatively, only include those that are valid).

In terms of your filtering, BTW, ARRAY_SIMPLE isn't technically what you want. You want:
Code:
array(XenForo_Input::STRING, 'array' => true)
Which allows an array of strings.
 
Thanks! That worked. Here is my updated code:

ControllerAdmin
PHP:
    public function actionUser()
    {
        $userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);
       
        $userModel = $this->_getUserModel();
        $user = $userModel->getUserById($userId);
       
        $urls = $user['goss_memberaward_urls'];
       
        if (!is_array($urls))
        {
            $urls = ($urls ? XenForo_Helper_Php::safeUnserialize($urls) : array());
        }   
       
        $viewParams = array(
            'user' => $user,
            'memberAwardUrls' => $urls
        );
       
        return $this->responseView(null, 'goss_memberawards_user', $viewParams);
    }
   
    public function actionSave()
    {       
        $userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);
       
       
        $awardurls = $this->_input->filterSingle('goss_memberaward_urls', XenForo_Input::STRING, array('array' => true));
       
       
        $dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
       
        $dw->setExistingData($userId);
       
        $dw->setMemberAwardUrls($awardurls);
       
        $dw->save();
       
        //Send a response to the user, so he know that everything went fine with this action
        return $this->responseRedirect(
                    XenForo_ControllerResponse_Redirect::SUCCESS,
                    $this->getDynamicRedirect()
        );
    }

DataWriter
PHP:
    public function setMemberAwardUrls(array $urls)
    {
        $cleanUrls = array_filter($urls);
       
        foreach ($cleanUrls AS $url)
        {
            if ($url === '')
            {
                unset($url[$value]);
                continue;
            }
           
            if (strlen($url) > 200)
            {
                $this->error(new XenForo_Phrase('please_enter_value_using_x_characters_or_fewer', array('count' => 200)));
                return false;
            }
        }
       
        $this->set('goss_memberaward_urls', $cleanUrls);
           
        return true;
    }

One last question. How can I validate that the value entered is a url?
 
Top Bottom