<![CDATA[ Update Profile Validation - MODX Community Forums]]> https://forums.modx.com/thread/?thread=103770 <![CDATA[Update Profile Validation]]> https://forums.modx.com/thread/103770/update-profile-validation?page=2#dis-post-558168 Login 1.9.5

I have a ClassExtender field (screenName) that requires a minimum of three characters which correctly throws an error if less than three. The problem is the form still submits and the field is updated despite the error.
[[!ExtUserUpdateProfile]]
[[!UpdateProfile?
	&preHooks=`FullName`
	&excludeExtended=`email:required:email,login-updprof-btn,mediaFolder`
	&validate=`nospam:blank,
		username:required,
		firstName:required,
		lastName:required,
		screenName:required:minLength=^3^,
		email:required:email`
	&useExtended=`0`
]]
[[!SetUserPlaceholders]]

<label for="screenName">Screen Name [[!+error.screenName]]</label>
<input type="text" name="screenName" id="screenName" value="[[+screenName]]">
]]>
todd.b Apr 23, 2018, 03:27 PM https://forums.modx.com/thread/103770/update-profile-validation?page=2#dis-post-558168
<![CDATA[Re: Update Profile Validation]]> https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558258 andytough Apr 27, 2018, 10:10 AM https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558258 <![CDATA[Re: Update Profile Validation]]> https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558223
Note that it's normal to see an empty array when the form is first loaded. It's what happens on submit that counts.

echo "POST: " . print_r($_POST, true);
]]>
BobRay Apr 25, 2018, 09:54 PM https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558223
<![CDATA[Re: Update Profile Validation]]> https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558222 andytough Apr 25, 2018, 09:51 PM https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558222 <![CDATA[Re: Update Profile Validation]]> https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558221
if ( (! isset($_POST['firstName'])) || empty($_POST['firstName'])) {
      return "";
}
]]>
BobRay Apr 25, 2018, 09:31 PM https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558221
<![CDATA[Re: Update Profile Validation]]> https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558203
To prevent the existing data in four ClassExtender fields from being overwritten I have added the following code. (field names are : firstName, lastName, businessAddressCounty, businessAddressPostcode)

It seems to work, but I wonder if there is a better way of writing the code, rather than repeating the whole thing four times?

if ($submission) {
if (strlen($_POST['firstName']) < 1) {
      return "";
   }
if (strlen($_POST['lastName']) < 1) {
      return "";
   }
if (strlen($_POST['businessAddressCounty']) < 1) {
      return "";
   }
if (strlen($_POST['businessAddressPostcode']) < 1) {
      return "";
   }
    $modx->request->sanitizeRequest();
    $dirty = false;
    foreach ($fields as $key => $value) {
        if (isset($_POST[$key])) {
            if ($value !== $_POST[$key]) {
                $data->set($key, $_POST[$key]);
                $dirty = true;
            }
        }
    }

    if ($dirty) {
        $data->save();
    }
}

return '';


Or should I be using something different like
if (empty($_POST['firstName'])) {
      return "";
   }
]]>
andytough Apr 25, 2018, 08:44 AM https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558203
<![CDATA[Re: Update Profile Validation]]> https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558185 BobRay Apr 24, 2018, 03:47 PM https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558185 <![CDATA[Re: Update Profile Validation]]> https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558176
Just so I understand, if/when ClassExtendeder is updated this will be overwritten, correct?]]>
todd.b Apr 24, 2018, 12:19 AM https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558176
<![CDATA[Re: Update Profile Validation (Best Answer)]]> https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558175
Add the three extra lines to the ExtUserUpdateProfile snippet, just below if ($submission) {.
if ($submission) {
   if (strlen($_POST['screenName']) < 3) {
      return "";
   }

... 
}


That way, the custom field won't be updated and UpdateProfile will display the error message for you.]]>
BobRay Apr 23, 2018, 10:28 PM https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558175
<![CDATA[Re: Update Profile Validation]]> https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558174
Is PHP also a validator option? The problem with js is that it's easily disabled.]]>
todd.b Apr 23, 2018, 10:12 PM https://forums.modx.com/thread/103770/update-profile-validation#dis-post-558174