Re: [fw-general] Writing a validator for limit the number of selectable elements

2009-01-21 Thread fab2008


Emanuel-20 wrote:
> 
> If the name attribute of the input tag would be like: "myChecks[]" for
> all the n checkboxes then the result will come as an array, and u
> could use in your script:
> 
> if (!$myValidator->isValid(count($sentArray))) {
>   echo "The number of selected checkboxes must be at least x and at most
> y";
> } else {
>   //validate the contents of $sentArray and process its values
> }
> 

Yes, this solution works well, but it does not integrate in the Zend_Form
well. I'can also put this code in my script:

if (count($_POST['myCheck']) > x || count($_POST['myCheck']) < y) {
  echo "Something...";
  exit();
}

but this not show errors on form and it breaks the normal flow of my app.

What I love of Zend_Form component (using validators and filters) is the
oppurtunity to show error messages below the wrong value, and the
opportunity to made all the constraint checks with a single instruction in
the controller like this one:

if ($form->isValid($_POST)) {
  // here I can access valid and filtered values
  // without any additional check
}

If I using the above approach, I can not do this anymore.


Emanuel-20 wrote:
> 
> P.S. Don't write the custom validator unless the functionalities
> provided cannot be achieved using the existing ones, because the
> memory is still a limited resource nowadays and you might need it
> somewhere else.
> 

Yes, the memory is a problem with PHP, but I think that most of memory is
used by MVC architecture; the validators use only a minimum part of it. Like
everything is a matter of compromise between performance/memory usage/cpu
usage and code simplicity, readability and mantainability; I prefer the
second ones (within certain limits), because they are more important than
the other ones, otherwise we should keep program with assembler, because is
the more performant language.



-- 
View this message in context: 
http://www.nabble.com/Writing-a-validator-for-limit-the-number-of-selectable-elements-tp21511756p21585588.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Re: [fw-general] Writing a validator for limit the number of selectable elements

2009-01-20 Thread Emanuel
If the name attribute of the input tag would be like: "myChecks[]" for
all the n checkboxes then the result will come as an array, and u
could use in your script:

if (!$myValidator->isValid(count($sentArray))) {
  echo "The number of selected checkboxes must be at least x and at most y";
} else {
  //validate the contents of $sentArray and process its values
}

P.S. Don't write the custom validator unless the functionalities
provided cannot be achieved using the existing ones, because the
memory is still a limited resource nowadays and you might need it
somewhere else.

On Tue, Jan 20, 2009 at 21:09, fab2008  wrote:
>
> I haven't found any solution to my problem yet, anyone can help me?
>
> Thank you very much..
>
>
> fab2008 wrote:
>>
>> Hi all,
>>
>> I've the following situation: In a form i put a multicheckbox element with
>> a certain number of elements say n. I'want the user select a number of
>> elements between x and y with 0 <= x < y <= n
>>
>> For this i've just tried to write my custom validator extending the
>> Zend_Validate_Between class, overriding its isValid method with something
>> similar to this:
>>
>> public function isValid($value) {
>>   return parent::isValid(count($value));
>> }
>>
>> because I tought that $value is an array, but it's not the case, my
>> validator will be called once per selected checkbox with scalar values. So
>> I've tried a different approach:
>>
>> protected $count = 0;
>>
>> public function isValid($value) {
>>   if (++$this->count > $this->_max) {
>> return false;
>>   }
>>   return true;
>> }
>>
>> I'm unsure about correctness, it breaks if the same validator instance
>> will be attached to more than one multi element, and clearly it works only
>> for the max, but not for the min.
>>
>> Any suggestion?
>>
>>
>>
>
> --
> View this message in context: 
> http://www.nabble.com/Writing-a-validator-for-limit-the-number-of-selectable-elements-tp21511756p21569555.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
>


Re: [fw-general] Writing a validator for limit the number of selectable elements

2009-01-20 Thread fab2008

I haven't found any solution to my problem yet, anyone can help me?

Thank you very much..


fab2008 wrote:
> 
> Hi all, 
> 
> I've the following situation: In a form i put a multicheckbox element with
> a certain number of elements say n. I'want the user select a number of
> elements between x and y with 0 <= x < y <= n
> 
> For this i've just tried to write my custom validator extending the
> Zend_Validate_Between class, overriding its isValid method with something
> similar to this:
> 
> public function isValid($value) {
>   return parent::isValid(count($value));
> }
> 
> because I tought that $value is an array, but it's not the case, my
> validator will be called once per selected checkbox with scalar values. So
> I've tried a different approach:
> 
> protected $count = 0;
> 
> public function isValid($value) {
>   if (++$this->count > $this->_max) {
> return false;
>   }
>   return true;
> }
> 
> I'm unsure about correctness, it breaks if the same validator instance
> will be attached to more than one multi element, and clearly it works only
> for the max, but not for the min.
> 
> Any suggestion?
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Writing-a-validator-for-limit-the-number-of-selectable-elements-tp21511756p21569555.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Re: [fw-general] Writing a validator for limit the number of selectable elements

2009-01-17 Thread fab2008


Chris Weldon wrote:
> 
> How do you have the Multicheckbox element setup? How are you going
> about checking that the form is valid? A bit more code samples would
> prove useful...
> 

I've done a simple setup, this is, more or less, my setup:

Base class (in which I add common decorator and filters for all forms in
order to obtain consistent look and feel and behaviour for my app)

abstract class Form_Base extends Zend_Form {
/**
 * Form title
 * @var string
 */
protected $_title;

public function init() {
// set class for this form
$this->setAttrib('class', 'form');
// add forms css
$view = $this->getView();
$view->headLink()->appendStylesheet(
$view->baseUrl() . '/styles/form.css', 'all');
// set the form to be post
$this->setMethod('post');
// set translator
$this->setTranslator(

Zend_Registry::get('Zend_Translate')->getAdapter());
// call abstract method that init form elements
$this->initComponents();
// trim all values provided in form
foreach ($this->getElements() as $elt) {
$elt->addFilter('StringTrim');
}
$this->setDecorators(
array (
'FormElements', 
array (
'Description', 
array (

'tag' => 'dd', 

'escape' => false, 

'placement' => Zend_Form_Decorator_Abstract::PREPEND)), 
array (
'HtmlTag', 
array (

'tag' => 'dl', 

'class' => 'zend_form')), 
'Form'));
// custom render of the form
$titleTag = new Form_Decorator_Title();
$this->addDecorator($titleTag);
$this->addDecorator(array ('SurroundTag' => 'HtmlTag'), 
array ('tag' => 'div', 'class' => 'formdiv'));
}

protected function addSubmitButton($label = 'Send') {
$submit = new Zend_Form_Element_Submit('submitBtn', $label);
$this->addElement($submit);
$this->addDisplayGroup(array ('submitBtn'), 'buttons', 
array ('legend' => 'Send Form'));
}

// subclass will create form elements in this method
abstract protected function initComponents();
}


and this is the class in which I use the multicheckbox element:

class Form_Checkout extends Form_Base { 
protected function initComponents() {
// title
$this->setTitle('FORM_CHECKOUT_TITLE');

$selected = new Zend_Form_Element_MultiCheckbox('orders', 
array (
'separator' => '', 
'required' => true, 
'class' => 'checkbox'));

$this->addElement($selected);

$this->addSubmitButton('FORM_CHECKOUT_BUTTON');
}

public function addOrder(Request $request) {
$this->orders->addMultiOption($request->idorder,
$request->getDescription());
}   
}

In my controller I call addOrder method once for every order that could be
payed, but in a special case, I don't want that more than x orders could be
selected in the form as explained in my first message.

Thank you.


-- 
View this message in context: 
http://www.nabble.com/Writing-a-validator-for-limit-the-number-of-selectable-elements-tp21511756p21523746.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Re: [fw-general] Writing a validator for limit the number of selectable elements

2009-01-17 Thread Chris Weldon
How do you have the Multicheckbox element setup? How are you going
about checking that the form is valid? A bit more code samples would
prove useful...
--
Chris Weldon

On Sat, Jan 17, 2009 at 2:36 PM, fab2008  wrote:
>
> Nobody who can helps me?
>
>
> fab2008 wrote:
>>
>> Hi all,
>>
>> I've the following situation: In a form i put a multicheckbox element with
>> a certain number of elements say n. I'want the user select a number of
>> elements between x and y with 0 <= x < y <= n
>>
>> For this i've just tried to write my custom validator extending the
>> Zend_Validate_Between class, overriding its isValid method with something
>> similar to this:
>>
>> public function isValid($value) {
>>   return parent::isValid(count($value));
>> }
>>
>> because I tought that $value is an array, but it's not the case, my
>> validator will be called once per selected checkbox with scalar values. So
>> I've tried a different approach:
>>
>> protected $count = 0;
>>
>> public function isValid($value) {
>>   if (++$this->count > $this->_max) {
>> return false;
>>   }
>>   return true;
>> }
>>
>> I'm unsure about correctness, it breaks if the same validator instance
>> will be attached to more than one multi element, and clearly it works only
>> for the max, but not for the min.
>>
>> Any suggestion?
>>
>>
>>
>
> --
> View this message in context: 
> http://www.nabble.com/Writing-a-validator-for-limit-the-number-of-selectable-elements-tp21511756p21520370.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
>



-- 
Christopher Weldon
http://chrisweldon.net
ch...@chrisweldon.net


Re: [fw-general] Writing a validator for limit the number of selectable elements

2009-01-17 Thread fab2008

Nobody who can helps me?


fab2008 wrote:
> 
> Hi all, 
> 
> I've the following situation: In a form i put a multicheckbox element with
> a certain number of elements say n. I'want the user select a number of
> elements between x and y with 0 <= x < y <= n
> 
> For this i've just tried to write my custom validator extending the
> Zend_Validate_Between class, overriding its isValid method with something
> similar to this:
> 
> public function isValid($value) {
>   return parent::isValid(count($value));
> }
> 
> because I tought that $value is an array, but it's not the case, my
> validator will be called once per selected checkbox with scalar values. So
> I've tried a different approach:
> 
> protected $count = 0;
> 
> public function isValid($value) {
>   if (++$this->count > $this->_max) {
> return false;
>   }
>   return true;
> }
> 
> I'm unsure about correctness, it breaks if the same validator instance
> will be attached to more than one multi element, and clearly it works only
> for the max, but not for the min.
> 
> Any suggestion?
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Writing-a-validator-for-limit-the-number-of-selectable-elements-tp21511756p21520370.html
Sent from the Zend Framework mailing list archive at Nabble.com.



[fw-general] Writing a validator for limit the number of selectable elements

2009-01-16 Thread fab2008

Hi all, 

I've the following situation: In a form i put a multicheckbox element with a
certain number of elements say n. I'want the user select a number of
elements between x and y with 0 <= x < y <= n

For this i've just tried to write my custom validator extending the
Zend_Validate_Between class, overriding its isValid method with something
similar to this:

public function isValid($value) {
  return parent::isValid(count($value));
}

because I tought that $value is an array, but it's not the case, my
validator will be called once per selected checkbox with scalar values. So
I've tried a different approach:

protected $count = 0;

public function isValid($value) {
  if (++$this->count > $this->_max) {
return false;
  }
  return true;
}

I'm unsure about correctness, it breaks if the same validator instance will
be attached to more than one multi element, and clearly it works only for
the max, but not for the min.

Any suggestion?


-- 
View this message in context: 
http://www.nabble.com/Writing-a-validator-for-limit-the-number-of-selectable-elements-tp21511756p21511756.html
Sent from the Zend Framework mailing list archive at Nabble.com.