Does form validator implicitly set setRequired() to true?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Does form validator implicitly set setRequired() to true?

waigani-2
Hi,

I'm validating a form. Most of the validation needs to be of the following format: IF there is content in a feild THEN that content needs to be valid xyz.

It seems that if I put any kind of validator on a feild, the form fails if their is no content in that field. How do I get around this?

Thank you for your time (and who ever is going to ZendCon see you soon!),
Jess
Reply | Threaded
Open this post in threaded view
|

Re: Does form validator implicitly set setRequired() to true?

weierophinney
Administrator
-- Waigani <[hidden email]> wrote
(on Tuesday, 02 September 2008, 12:00 AM -0700):
> I'm validating a form. Most of the validation needs to be of the following
> format: IF there is content in a feild THEN that content needs to be valid
> xyz.
>
> It seems that if I put any kind of validator on a feild, the form fails if
> their is no content in that field. How do I get around this?
>
> Thank you for your time (and who ever is going to ZendCon see you soon!),

I'd like to see your code to see if it represents a reproduce case.

By default, elements are *not* required, meaning that if no value is
submitted, no validators should be run. This has worked perfectly for me
in every form I've written with Zend_Form -- perhaps others can chime in
here, though, if they have experienced what you describe?

--
Matthew Weier O'Phinney
Software Architect       | [hidden email]
Zend Framework           | http://framework.zend.com/
Reply | Threaded
Open this post in threaded view
|

Re: Does form validator implicitly set setRequired() to true?

waigani-2
Okay I'll debug my form and post post the bug if / when I find it.

On the same form I can't get my custom validator to work. The validtation is: if there is a start page, there needs to be an end page and vice versa. Here is the validator:

class My_Validator_PageNo extends Zend_Validate_Abstract
{
    const NO_END = 'noEnd';
    const NO_START = 'noStart';

    protected $_messageTemplates = array(
        self::NO_END => 'A start page has been entered, you must specify an end page',
                self::NO_START => 'An end page has been entered, you must specify a start page'
    );

    public function isValid($value, $context = null)
    {
         
        $value = (string) $value;
        $this->_setValue($value);
       
            if (!empty($context['startPage'])
                         &&  empty($context['endPage']))
            {
                $this->_error(self::NO_END);
                                return false;
            }
         else if (! empty($context['endPage'])
              &&  empty($context['startPage']))
            {
                $this->_error(self::NO_START);
                                return false;
            }
   
         
        return true;
    }
}

I'm testing with '1' as the input value for startPage. While debugging I found that $context was not set, not even an empty array. What an I missing?


Matthew Weier O'Phinney-3 wrote
-- Waigani <jesse.meek@otago.ac.nz> wrote
(on Tuesday, 02 September 2008, 12:00 AM -0700):
> I'm validating a form. Most of the validation needs to be of the following
> format: IF there is content in a feild THEN that content needs to be valid
> xyz.
>
> It seems that if I put any kind of validator on a feild, the form fails if
> their is no content in that field. How do I get around this?
>
> Thank you for your time (and who ever is going to ZendCon see you soon!),

I'd like to see your code to see if it represents a reproduce case.

By default, elements are *not* required, meaning that if no value is
submitted, no validators should be run. This has worked perfectly for me
in every form I've written with Zend_Form -- perhaps others can chime in
here, though, if they have experienced what you describe?

--
Matthew Weier O'Phinney
Software Architect       | matthew@zend.com
Zend Framework           | http://framework.zend.com/
Reply | Threaded
Open this post in threaded view
|

Re: Does form validator implicitly set setRequired() to true?

Michel Morelli
Waigani ha scritto:
> I'm testing with '1' as the input value for startPage. While debugging I
> found that $context was not set, not even an empty array. What an I missing?
>  

In italy we say "Tu sbagli candeggio" (for people +30 years old).

How do you call this Validator ?
You need to set  $context in the __construct() method.

try this:

class My_Validator_PageNo extends Zend_Validate_Abstract
{
    const NO_END = 'noEnd';
    const NO_START = 'noStart';

    private $_context;

    protected $_messageTemplates = array(
        self::NO_END => 'A start page has been entered, you must specify an
end page',
        self::NO_START => 'An end page has been entered, you must
specify a start
page'
    );

    function __construct($context = null) {
        $this->_context = $context;
    }

    public function isValid($value)
    {
     
        $value = (string) $value;
        $this->_setValue($value);
     
            if (!empty($this->_context['startPage'])
             &&  empty($this->_context['endPage']))
            {
                $this->_error(self::NO_END);
                return false;
            }
         else if (! empty($this->_context['endPage'])
              &&  empty($this->_context['startPage']))
            {
                $this->_error(self::NO_START);
                return false;
            }
   
     
        return true;
    }
}

And call in this way : ->addValidator(new My_Validator_PageNo($context));

And I see that in your Validator you use only $context and not $value so
why this ?  Can you send the code where you set the Validator ?

And sorry for my bad english.

M.

--
Michel 'ZioBudda' Morelli                       [hidden email]
Consulenza sistemistica in ambito OpenSource.
Sviluppo applicazioni web dinamiche (LAMP+Ajax)
Telefono: 0200619074
Telefono Cell: +39-3939890025 --  Fax: +39-0291390660

http://www.ziobudda.net                         ICQ: 58351764  
http://www.ziobuddalabs.it                      Skype: zio_budda
http://www.ajaxblog.it        MSN: [hidden email]                  
                                                JABBER: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Does form validator implicitly set setRequired() to true?

waigani-2
I've added the bits. With the call, how do I create the $context variable?

Michel Morelli wrote
Waigani ha scritto:
> I'm testing with '1' as the input value for startPage. While debugging I
> found that $context was not set, not even an empty array. What an I missing?
>  

In italy we say "Tu sbagli candeggio" (for people +30 years old).

How do you call this Validator ?
You need to set  $context in the __construct() method.

try this:

class My_Validator_PageNo extends Zend_Validate_Abstract
{
    const NO_END = 'noEnd';
    const NO_START = 'noStart';

    private $_context;

    protected $_messageTemplates = array(
        self::NO_END => 'A start page has been entered, you must specify an
end page',
        self::NO_START => 'An end page has been entered, you must
specify a start
page'
    );

    function __construct($context = null) {
        $this->_context = $context;
    }

    public function isValid($value)
    {
     
        $value = (string) $value;
        $this->_setValue($value);
     
            if (!empty($this->_context['startPage'])
             &&  empty($this->_context['endPage']))
            {
                $this->_error(self::NO_END);
                return false;
            }
         else if (! empty($this->_context['endPage'])
              &&  empty($this->_context['startPage']))
            {
                $this->_error(self::NO_START);
                return false;
            }
   
     
        return true;
    }
}

And call in this way : ->addValidator(new My_Validator_PageNo($context));

And I see that in your Validator you use only $context and not $value so
why this ?  Can you send the code where you set the Validator ?

And sorry for my bad english.

M.

--
Michel 'ZioBudda' Morelli                       michel@ziobuddalabs.it
Consulenza sistemistica in ambito OpenSource.
Sviluppo applicazioni web dinamiche (LAMP+Ajax)
Telefono: 0200619074
Telefono Cell: +39-3939890025 --  Fax: +39-0291390660

http://www.ziobudda.net                         ICQ: 58351764  
http://www.ziobuddalabs.it                      Skype: zio_budda
http://www.ajaxblog.it        MSN: michel@ziobuddalabs.it                  
                                                JABBER: michel@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: Does form validator implicitly set setRequired() to true?

Michel Morelli
Waigani ha scritto:
> I've added the bits. With the call, how do I create the $context variable?
>
>  
How, in this moment do you call/invoke the validator ?
Where do you set/create the $context variable ?

If you do not respond me I can not respond to your questions.

M.

--
Michel 'ZioBudda' Morelli                       [hidden email]
Consulenza sistemistica in ambito OpenSource.
Sviluppo applicazioni web dinamiche (LAMP+Ajax)
Telefono: 0200619074
Telefono Cell: +39-3939890025 --  Fax: +39-0291390660

http://www.ziobudda.net                         ICQ: 58351764  
http://www.ziobuddalabs.it                      Skype: zio_budda
http://www.ajaxblog.it        MSN: [hidden email]                  
                                                JABBER: [hidden email]