Support the ongoing development of Laravel.io →
posted 9 years ago
Validation
Last updated 1 year ago.
0

I know this is not the proper way of doing this, but:

if (Input::has(['a', 'b', 'c', 'd', 'e']))
		{
			$sum = 0;
			foreach(Input::only(['a', 'b', 'c', 'd', 'e']) as $k => $v) {
				$sum += (int)$v;
			}
		}
		if($sum === 18) {
			echo '$sum is: ' . $sum;
		}

There is a way to extend the Validate class, but I have no idea how can i iterate over all the matching inputs on that way, im still learning php and oop is new to me.

Btw you should also check for numeric inputs, if it is a string the between stuff will convert that character to an integer.

// Add your validation rules here
	public static $rules = [
		'a' => 'required|between:2,4|numeric',
		'b' => 'required|between:2,4|numeric',
		'c' => 'required|between:2,4|numeric',
		'd' => 'required|between:2,4|numeric',
		'e' => 'required|between:2,4|numeric',
	];
Last updated 1 year ago.
0

TheSpiritMolecule said:

I know this is not the proper way of doing this, but:

if (Input::has(['a', 'b', 'c', 'd', 'e']))
  	{
  		$sum = 0;
  		foreach(Input::only(['a', 'b', 'c', 'd', 'e']) as $k => $v) {
  			$sum += (int)$v;
  		}
  	}
  	if($sum === 18) {
  		echo '$sum is: ' . $sum;
  	}

There is a way to extend the Validate class, but I have no idea how can i iterate over all the matching inputs on that way, im still learning php and oop is new to me.

Thanks but that's not what I'd like to do, I would use extend Laravel validator, but I don't know how to pass multiple input values to a custom validator.

Btw you should also check for numeric inputs, if it is a string the between stuff will convert that character to an integer.

// Add your validation rules here
  public static $rules = [
  	'a' => 'required|between:2,4|numeric',
  	'b' => 'required|between:2,4|numeric',
  	'c' => 'required|between:2,4|numeric',
  	'd' => 'required|between:2,4|numeric',
  	'e' => 'required|between:2,4|numeric',
  ];

Thanks, I've fixed that.

Last updated 1 year ago.
0

There is a good example of how to extend Laravel validator at

http://culttt.com/2014/01/20/extending-laravel-4-validator/

Note that the custom validator gets passed the $data array. This gives you access to ALL the submitted form fields. So, your check would be :

if ($this->data['a']+$this->data['b']+$this->data['c']+$this->data['d']+$this->data['e']!==18){
 return false;
}

Associate the custom validator with one of the inputs, and you should be good to go.

Last updated 1 year ago.
0

mnshankar said:

There is a good example of how to extend Laravel validator at

http://culttt.com/2014/01/20/extending-laravel-4-validator/

Note that the custom validator gets passed the $data array. This gives you access to ALL the submitted form fields. So, your check would be :

if ($this->data['a']+$this->data['b']+$this->data['c']+$this->data['d']+$this->data['e']!==18){
return false;
}

Associate the custom validator with one of the inputs, and you should be good to go.

Many thanks!! That's exactly what I was looking for

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

thagul thagul Joined 1 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.