<p>Looks like I needed to set <code>$user-&gt;username = craft()-&gt;request-&gt;getPost('email');</code> for my attempt to work.</p>

CC BY-SA 3.0
2016-02-19T14:35:24.573Z
2016-02-19T14:35:24.573Z
James
<p>What’s the best way to validate a user registration form to make sure that at least one of two fields is not empty? </p>

<p>I’ve tried a very simple controller but can’t get this to work past the point of trying to save the user. Any pointers with this method or for a different method entirely?</p>

<pre><code>public function actionIndex() {

    $this-&gt;requirePostRequest();

    $fields = craft()-&gt;request-&gt;getPost('fields');

    $customField1 = $fields['customField1'];
    $customField2 = $fields['customField2'];

    $user = new UserModel();
    $user-&gt;email = craft()-&gt;request-&gt;getPost('email');
    $user-&gt;password = craft()-&gt;request-&gt;getPost('password');
    $user-&gt;setContentFromPost('fields');

    if ($customField1 != '' || $customField2 != '') {

        // RETURN CUSTOM ERROR

    }

    // OTHERWISE SUBMIT USER REGISTRATION REQUEST 
    // AND RETURN ANY ERRORS

    craft()-&gt;users-&gt;saveUser($user);
    $this-&gt;redirectToPostedUrl();

}
</code></pre>

CC BY-SA 3.0
2016-02-19T11:45:24.610Z
What if you changed the `&&` (and) to an `||` (or). So your statement would now be `if customField` is not empty OR customField2 is not empty`...
Damon
CC BY-SA 3.0
2016-02-19T11:52:44.817Z
@Damon good spot but it was a typo with the example, sorry. Edited the question. The validation part works it seems to be the saveUser part here that’s not checking out.
James
CC BY-SA 3.0
2016-02-19T12:49:39.637Z
James, please re-read Damon's suggestion, I think it should in fact solve your problem. And @Damon, I think you should add it as a proper answer?!
carlcs
CC BY-SA 3.0
2016-02-19T13:01:48.340Z
@carlcs the conditional is not the problem here. I didn’t set the example properly.
James
<plugin-development><users><plugins><registration><usermodel>
Conditional for user registration fields
# Conditional for user registration fields

(Asked by: username_0 on 2016-02-19)

What’s the best way to validate a user registration form to make sure that at least one of two fields is not empty? 
I’ve tried a very simple controller but can’t get this to work past the point of trying to save the user. Any pointers with this method or for a different method entirely?
public function actionIndex() {

    $this->requirePostRequest();

    $fields = craft()->request->getPost('fields');

    $customField1 = $fields['customField1'];
    $customField2 = $fields['customField2'];

    $user = new UserModel();
    $user->email = craft()->request->getPost('email');
    $user->password = craft()->request->getPost('password');
    $user->setContentFromPost('fields');

    if ($customField1 != '' || $customField2 != '') {

        // RETURN CUSTOM ERROR

    }

    // OTHERWISE SUBMIT USER REGISTRATION REQUEST 
    // AND RETURN ANY ERRORS

    craft()->users->saveUser($user);
    $this->redirectToPostedUrl();

}

**Tags:** plugin-development, users, plugins, registration, usermodel

> **Comments:**
> **username_1**: What if you changed the `&&` (and) to an `||` (or). So your statement would now be `if customField` is not empty OR customField2 is not empty`...  
> **username_0**: @username_1 good spot but it was a typo with the example, sorry. Edited the question. The validation part works it seems to be the saveUser part here that’s not checking out.  
> **username_2**: username_0, please re-read username_1's suggestion, I think it should in fact solve your problem. And @username_1, I think you should add it as a proper answer?!  
> **username_0**: @username_2 the conditional is not the problem here. I didn’t set the example properly.  

---

## 1 answers

### Answer by username_0 on 2016-02-19. Score: 2

Looks like I needed to set `$user->username = craft()->request->getPost('email');` for my attempt to work.


---

