Skip to content

Instantly share code, notes, and snippets.

@radzserg
Last active June 25, 2020 12:50
Show Gist options
  • Select an option

  • Save radzserg/655cecd056520bfa296ed2b8e3f91ea4 to your computer and use it in GitHub Desktop.

Select an option

Save radzserg/655cecd056520bfa296ed2b8e3f91ea4 to your computer and use it in GitHub Desktop.
yii validation rules example
<?php
class Yii1Model
{
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
// Required fields
array('customer', 'required', 'message' => Yii::t('rezdy', 'Please create or select a customer'), 'on' => 'create, update'),
// Safe on booking form
array('customer, customer[mailingAddress], additionalFields, paymentOption, payFullAmount', 'safe', 'on' => 'bookingForm, partners'),
array('customerAgreedToTermsAndConditions', 'safe', 'on' => 'bookingForm'),
// Safe on create and update
array('status, surcharge, sourceChannel, agentUserName', 'safe', 'on' => 'create, update'),
array('internalNotes, resellerComments, resellerReference, agentUserName', 'safe', 'on' => 'partners'),
// Safe on commissions report
array('startTime, endTime, dateRange, resellerId, reconciled, lastInvoice', 'safe', 'on' => 'commissionDetails'),
// Top level fields with children
array('customer, items, paymentDetails, creditCard', 'safe'),
array('status, searchString, startTime, endTime, dateRange, productId, paymentStatus, entityState', 'safe', 'on' => 'search'),
// Special fields
array('date', 'date'),
array('customer[phone], customer[mobile]', 'match', 'pattern' => '/^([0-9+() -])+$/'),
// Trim
array('comments, internalNotes, voucherCode, resellerComments, resellerReference', 'filter', 'filter' => [$this, 'sanitize']),
// Email validation
array('customer[email]', 'ext.validators.EmailValidator', 'allowEmpty' => true),
//Check for TC agreement
array('customerAgreedToTC', 'ext.validators.EConditionalValidator', 'conditionalRules' => array('companySettings[explicitAgreementToTermsAndPolicy]', 'compare', 'compareValue' => true), 'rule' => array('required'), 'on' => 'bookingForm'),
// Max length
array('email, customer[email]', 'length', 'max' => 254),
array('customer[phone]', 'length', 'max' => 25),
array('customer[firstName], customer[lastName], customer[mailingAddress][addressLine], customer[mailingAddress][addressLine2], customer[mailingAddress][city], customer[mailingAddress][state]', 'length', 'max' => 200),
array('customer[mailingAddress][countryCode]', 'length', 'max' => 4),
array('customer[mailingAddress][postCode]', 'length', 'max' => 10),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment