Thursday, July 17, 2014

Re: Data Validation: Checking if at least one field is populated and multiple rules not validating

Thanks very much Stephen. I kept at it and found the solution lay in removing the 'required' and 'allowEmpty' from the 'needOne' ruleset, and adding a 'required' => false in the view. 

Here's the working solution for any one else with this problem:

The model:
public $validate = array(
'last_name'=> array(
'needOne' => array (
'rule' => 'checkOne',
'message' => 'You must enter at least a contact name or email address.'
),
'alphaNumeric' => array(
'rule'   => 'alphaNumeric',
'message'  => 'Alphabets and numbers only',
'allowEmpty' => TRUE
),
'between' => array(
'rule'  => array('between', 2, 45),
'message' => 'Between 2 to 45 characters',
'allowEmpty' => TRUE
)
),
'email' => array(
'needOne' => array (
'rule' => 'checkOne',
'message' => 'You must enter at least a contact name or email address.'
),
'emailAddress' => array (
'rule' => 'email',
'message'  => 'A valid Email address is required',
'allowEmpty' => TRUE
)
)
);

// Ensure at least the last name or email field value is provided
public function checkOne($data) {
if(!empty($this->data[$this->alias]['last_name']) 
|| !empty($this->data[$this->alias]['email'])) {
return TRUE;
} else {
return FALSE;
}
}

The view/fields (I'm using Bootstrap):

echo $this->Form->input('last_name', array(
'required' => false,
'fieldset' => false,
'label' => false,
'before' => '<label class="control-label">Last Name <span class="one-required">*</span></label>',
'class' => 'form-control',
'placeholder' => 'Last Name',
'div' => 'form-group col-sm-12',
'error' => array(
'attributes' => array(
'wrap' => 'div', 
'class' => 'alert alert-danger'
)
)
)
);

echo $this->Form->input('email', array(
'required' => false,
'fieldset' => false,
'label' => false,
'before' => '<label class="control-label">Email <span class="one-required">*</span></label>',
'after' => '',
'class' => 'form-control',
'div' => 'form-group col-sm-12 col-xs-12',
'error' => array(
'attributes' => array(
'wrap' => 'div', 
'class' => 'alert alert-danger'
)
)
)
);

Thanks.

On Wednesday, 16 July 2014 00:44:50 UTC+10, seba wrote:
Folks,

I'm trying to ensure at least one of two fields (last_name or email) is being populated. Each field also has multiple rules. I'm using CakePHP version 2.4.2. 

The problem I have at the moment, after multiple permutations of updating and/or moving around the use 'last', 'allowEmpty', 'required', etc, is that the fields just aren't validating at all, or aren't executing all the rules when a field is populated.

Any advice on how to modify the code below to achieve the following behaviour is much appreciated:
1. One of the two fields must be populated;
2. The other rules attached to each field must validate as well (i.e. if a last name is passed, then it must be between 2 and 45 chars and alphanumeric only)

Here's the model code:

public $validate = array(
'last_name'=> array(
'needOne' => array (
'required' => FALSE,
'allowEmpty' => TRUE,
'last' => TRUE,
'rule' => array('checkOne','last_name'),
'message' => 'You must enter at least a contact name or email address.'
),
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'message'  => 'Alphabets and numbers only'
),
'between' => array(
'rule' => array('between', 2, 45),
'message' => 'Between 2 to 45 characters'
)
),
'email' => array(
'needOne' => array (
'required' => FALSE,
'allowEmpty' => TRUE,
'last' => TRUE,
'rule' => array('checkOne','email'),
'message' => 'You must enter at least a contact name or email address.'
),
'emailAddress' => array (
'last' => TRUE,
'rule' => array('email', FALSE),
'message'  => 'A valid Email address is required'
)
)
);
// Ensure at least the last name or email field value is provided
function checkOne() {
if(!empty($this->data[$this->User]['last_name']) || 
!empty($this->data[$this->User]['email'])){
return true;
} else {
return false;
}
}

Thanks in advance!

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: