How to validate a date without day with sfForm? - validation

I'm creating a payment form with symfony 1.4 , and my form has a date widget defined like this, so that the user can select the expiration date of his credit card:
new sfWidgetFormDate(array(
'format' => '%month%/%year%',
'years' => array_combine(range(date('Y'), date('Y') + 5), range(date('Y'), date('Y') + 5))
Notice the absence of %day% in the format like in most payment forms.
Now my problem is that sfValidatorDate requires the 'day' field not to be empty. To work around this, I created a custom validator using a callback, which works well:
public function validateExpirationDate($validator, $value)
{
$value['day'] = '15';
$dateValidator = new sfValidatorDate(array(
'date_format' => '#(?P<day>\d{2})(?P<month>\d{2})(?P<year>\d{2})#',
'required' => false,
'min' => strtotime('first day of this month')));
$dateValidator->clean($value);
return $value;
}
I feel there might be a simpler way to achieve this. What do you think? Have you already solved this problem in a cleaner way?

How do you store the date? If you just store month and year as integers or strings, then you can just make 2 choice widgets. But if you store it as datetime (timestamp), then you need a valid date anyway. This means that you need to automatically assign values to 'day' (usually first or last day of the month).
class YourForm extends BaseYourForm
{
public function configure()
{
$this->widgetSchema['date'] = new sfWidgetFormDate(array(
'format' => '%month%/%year%'
));
$this->validatorSchema['date'] = new myValidatorDate(array(
'day_default' => 1
));
}
}
class myValidatorDate extends sfValidatorDate
{
protected function configure($options = array(), $messages = array())
{
$this->addOption('day_default', 1);
parent::configure($options, $messages);
}
protected function doClean($value)
{
if (!isset($value['day']))
{
$value['day'] = $this->getOption('day_default');
}
return parent::doClean($value);
}
}

There's no need to use a custom validation class: you can simply override the tainted values passed to your bind() method:
<?php
// in your form class
public function bind(array $taintedValues = null, array $taintedFiles = null)
{
$taintedValues['date']['day'] = 1;
return parent::bind($taintedValues, $taintedFiles);
}

I used simplest way, for validate credit card expiration day:
$post_data = $request->getParameter('my_form');
$post_data['card_exp']['day'] = 1; //sets the first day of the month
$this->form->bind($post_data);
Hope this helps somebody.

I solve this first in the form class
$year = range(date('Y'), date('Y') - 50);
$this->widgetSchema['date'] = new sfWidgetFormDate(array(
'format' => '%year%',
'years' => array_combine($year, $year),
'can_be_empty' => false
));
Next...
public function bind(array $taintedValues = null){
$taintedValues['date']['day'] = '01';
$taintedValues['date']['month'] = '01';
parent::bind($taintedValues);
}
The field in the database is date type DATE.

Related

yii2 custom validation not working

I need to compare 2 attribute value in the model and only if first value is lower than second value form can validate.I try with below code but it not worked.
controller
public function actionOpanningBalance(){
$model = new Bill();
if ($model->load(Yii::$app->request->post())) {
$model->created_at = \Yii::$app->user->identity->id;
$model->save();
}else{
return $this->render('OpanningBalance', [
'model' => $model,
]);
}
}
Model
public function rules()
{
return [
[['outlet_id', 'sr_id', 'bill_number', 'bill_date', 'created_at', 'created_date','bill_amount','credit_amount'], 'required'],
[['outlet_id', 'sr_id', 'created_at', 'updated_at'], 'integer'],
[['bill_date', 'd_slip_date', 'cheque_date', 'created_date', 'updated_date','status'], 'safe'],
[['bill_amount', 'cash_amount', 'cheque_amount', 'credit_amount'], 'number'],
[['comment'], 'string'],
['credit_amount',function compareValue($attribute,$param){
if($this->$attribute > $this->bill_amount){
$this->addError($attribute, 'Credit amount should less than Bill amount');
}],
[['bill_number', 'd_slip_no', 'bank', 'branch'], 'string', 'max' => 225],
[['cheque_number'], 'string', 'max' => 100],
[['bill_number'], 'unique']
];
}
}
It's going in to the validator function but not add the error like i wanted
$this->addError($attribute, 'Credit amount should less than Bill amount');
anyone can help me with this?
If the validation is not adding any error, it's most likely being skipped. The issue is most likely becasue of default rules behaviour whereby it skips empty or already error given values as per here: https://www.yiiframework.com/doc/guide/2.0/en/input-validation#inline-validators
Specifically:
By default, inline validators will not be applied if their associated attributes receive empty inputs or if they have already failed some validation rules. If you want to make sure a rule is always applied, you may configure the skipOnEmpty and/or skipOnError properties to be false in the rule declarations.
So you would need to set up the skipOnEmpty or skipOnError values depending on what works for you:
[
['country', 'validateCountry', 'skipOnEmpty' => false, 'skipOnError' => false],
]
Try this:
public function actionOpanningBalance(){
$model = new Bill();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->created_at = \Yii::$app->user->identity->id;
$model->save();
}else{
return $this->render('OpanningBalance', [
'model' => $model,
]);
}
}
For Validation
You can use anonymous function :
['credit_amount',function ($attribute, $params) {
if ($this->$attribute > $this->bill_amount)) {
$this->addError($attribute, 'Credit amount should less than Bill amount.');
return false;
}
}],
you can use like this below answer is also write
public function rules(){
return [
['credit_amount','custom_function_validation', 'on' =>'scenario'];
}
public function custom_function_validation($attribute){
// add custom validation
if ($this->$attribute < $this->cash_amount)
$this->addError($attribute,'Credit amount should less than Bill amount.');
}
I've made custom_function_validation working using 3rd params like this:
public function is18yo($attribute, $params, $validator)
{
$dobDate = new DateTime($this->$attribute);
$now = new DateTime();
if ($now->diff($dobDate)->y < 18) {
$validator->addError($this, $attribute, 'At least 18 years old');
return false;
}
}
This is a back end validation and it will trigger on submit only. So you can try something like this inside your validation function.
if (!$this->hasErrors()) {
// Your validation code goes here.
}
If you check the basic Yii2 app generated you can see that example in file models/LoginForm.php, there is a function named validatePassword.
Validation will trigger only after submitting the form.

Validate single rows only when they are present Laravel

So i have a model named Customer.
The db the Customer looks like this:
id, name, lastName, personal, address, zip, location, phones, emails updated_at, created_at
Emails and Phones is special rows because they are store as an json object example
['john#doe.com', 'some#othermail.com', 'more#mails.com']
I use the Customer Model to store the validation rules and custom messages like this
<?php
class Customer extends BaseModel
{
public function validationRules()
{
return array(
'name' => 'required|max:255',
'lastName' =>'max:255',
'personal'=> 'integer',
'location' => 'max:255',
'address' => 'max:255',
'zip' => 'required|integer',
'phones' => 'betweenOrArray:8,10|required_without:emails',
'emails' => 'emailOrArray'
);
}
public function validationMessages()
{
// returns Validation Messages (its too much to write down)
}
}
The OrArray Rule is found here https://stackoverflow.com/a/18163546/1430587
I call them through my controller like this
public function store()
{
$customer = new Customer;
$messages = $customer->validationMessages();
$rules = $customer->validationRules();
$input['name'] = Input::get('name');
$input['lastName'] = Input::get('lastName');
$input['personal'] = preg_replace("/[^0-9]/", "", Input::get('personal'));
$input['location'] = Input::get('location');
$input['address'] = Input::get('address');
$input['zip'] = Input::get('zip');
$input['emails'] = Input::get('emails');
$input['phones'] = Input::get('phones');
foreach($input['phones'] as $i => $value)
{
$input['phones'][$i] = preg_replace("/[^0-9]/", "", $value);
}
$validator = Validator::make($input, $rules, $messages);
}
This all works just fine, but I want to be able to PUT/PATCH request to update a single row.
But the validationRules has Required on certain fields so when its not present i cant update that single row. Without getting an error that the other fields (witch I'm not posting) is required.
How would I best approach this ?
You should get that instance of the model that represent the row you want to edit, that's why the resource controller's update method has a parameter that is the resource you want to edit.
public function update($resourceId) {
$customer = Customer::where('id', '=', $resourceId);
}
Now this customer has all the attributes you set before, so you can access them like:
$customer->name;
$customer->lastName;
So when you valide the values you can use the existing values in your validator where the input is empty:
$input['name'] = (Input::get('name')) ? (Input::get('name')) : $customer->name;
Or a prettier solution with the elvis operator:
$input['name'] = (Input::get('name')) ?: $customer->name;
I came up with another solution to this problem that works very well and its much cleaner.
$customer = Customer::find($id);
$input = Input::except('_method', '_token');
$customer->fill($input);

codeigniter datamapper get_rules not called after save()

I'm using Datamapper ORM for CodeIgniter I have rules 'serialized' and get_rules 'unserialized' for a field in my model. This field will store serialized data and when I retrieve back, get_rules will unserialize it.
However, after calling save(), I'm trying to re-access the field, but it still return serialized string, instead of array.
Is there any way to re-call or refresh my object so that the get_rules is called again and the field now return array?
Here's my model:
class User extends DataMapper{
public $validation = array(
'password' => array(
'label' => 'Password',
'rules' => array('encrypt')
),
'preferences' => array(
'rules' => array('serialize'),
'get_rules'=> array('unserialize')
)
);
function __construct($id = NULL)
{
parent::__construct($id);
}
function post_model_init($from_cache = FALSE)
{
}
public function _encrypt($field)
{
if (!empty($this->{$field}))
{
$this->{$field} = md5($this->{$field});
}
}
}
Datamapper ORM, afaik, will only use the get_rules when actually performing a get(). A few things you could try:
Given the following
$a = new Fruit();
$a->name = 'grapes';
$a->colors = serialize(array("purple","green"));
$a->save();
1. Create a new datamapper object and re-fetch:
$b = new Fruit();
$b->where('id', $a->id)->get();
$colors = $b->colors;
2. unserialize() the field yourself...
$colors = unserialize($a->colors);
3. You might even be able to use get_clone()
//not tested...
$b = $a->get_clone();
$colors = $b->colors;
This has been fixed here: https://bitbucket.org/wanwizard/datamapper/commits/db6ad5f2e10650b0c00c8ef9b7176d49a8e85163
Get the latest Datamapper library from bitbucket.

Zend_Validate_Between on Another Value

What is the best way to validate a number range based on the value of another form element? If the user selects "percentage" as a discount type, the discount amount should be between 0 and 100, and not 140! The problem seems to be passing in another form element value.
Also, I've viewed other resources, one dealing with a similar topic, but perhaps not all the way relevant.
How to validate a field of Zend_Form based on the value of another field?
Form.php
$isValid = new Application_Model_Validate();
$discount = $this->createElement('text', 'discount')
->setLabel('Discount Amount')
->setDescription("Enter an amount in the format \"200.00\" ")
->setRequired(true)
->setDecorators(array('Description', 'ViewHelper', 'Errors',
array('HTMLTag', array('tag' => 'dd')),
array('Label', array('tag' => 'dt'))));
$discount->addValidators(array(array('Float')), $isValid->isValid(new Zend_Validate_Between(array('min' => '0', 'max' => '100')), $discountType));
$this->addElement($discount);
Application_Model_Validate.php
Require_once 'Zend/Validate/Abstract.php';
class Application_Model_Validate extends Zend_Validate_Abstract
{
/*
* Validation failure message key
*/
const INVALID_PERCENTAGE = 'InvalidPercentage';
/*
* Validation failure message template definitions
*/
protected $_messageTemplates = array(
self::INVALID_PERCENTAGE => 'Please enter a percentage greater than 0 and up to 100.'
);
protected $_percentageOption;
protected $_percentageValue;
/*
* Defined by Zend_Validate_Interface
* Validate the percentage parameters
*/
public function isValid($value, $context = null)
{
$this->_setValue($value);
/*
* If context key is valid, return true
*/
if(is_array($context))
{
if (isset($context['percentage']) && ($value))
{
return true;
}
}
$this->_error(self::INVALID_PERCENTAGE);
return false;
}
If you need anymore information, just say.
I modified your code a bit and added a drop down box:
Form:
$this->addElement('select', 'discounttype');
$this->getElement('discounttype')
->addMultiOptions(
array('percentage' => 'percentage', 'other' => 'other')
);
$discount = $this->createElement('text', 'discount')
->setLabel('Discount Amount')
->setRequired(true);
$discount->addValidators(
array('Float', new Application_Model_Validate(0, 140))
);
$this->addElement($discount);
Validator:
<?php
class Application_Model_Validate extends Zend_Validate_Between
{
public function isValid($value, $context = null)
{
$this->_setValue($value);
if ($context['discounttype'] == 'percentage') {
$this->setMax(100);
}
return parent::isValid($value, $context);
}
}
Now, if you validate the form in your controller using $form->isValid($this->getRequest()->getParams()), it will take an input between 0 and 100 if in the drop down box 'percentage' is selected and an input between 0 and 140 otherwise.
An alternative solution:
//controller validation
$discountValidate = new Application_Model_Validate();
$discountValidate->_checkDiscount($data['discountType'], $data['discount']);
if ($discountValidate->isValid())
{
//do
}
else
{
$element = $form->getElement('discount');
$element->addError($discountValidate->getError());
}

cakephp validation response returning data to controller

Hi i have made a custom validation in the model. How can i access the result($visitor) from this in the controller?
model:
<?php
class Visitors extends AppModel
{
var $name = 'Visitors';
var $validate = array(
'xxx' => array(
'rule' => array('checkxxx'),
'message' => 'yyy.'
)
);
function checkIxxx($check){
$visitor = $this->find('first', array('conditions' => $check));
return $visitor;
}
}
?>
in my controller i want this:
function start() {
$this->Visitors->set($this->data);
if($this->Visitors->validates())
{
if($this->Visitors->xxx->type == 'value') //this is a value from the $visitor array in the model**
{
//do something
}
}
is this possible?
Updated to be a relevant answer, apologies.
//Model
var myField = 'invalid';
function myValidation($var){
if($var === true){
// Passed your validation test
$this->myField = 'valid';
}else{
$this->myField = 'invalid';
}
}
// Controller
$this->Model->set($this->data);
$this->Model->validates($this->data);
if($this->Model->myfield == 'valid'){
// Field has passed validation
}
You will want to use
$this->Model->invalidFields()
PS: You dont follow cake conventions
the model should be "Visitor" (singular)

Resources