0

In my Controller I have some set of data, and some values can not be sent from client (it's not an error in my logic), and I don't modify corresponding fields in database. But I want to throw an error if the value is an empty string.

How can I to do it with standard validators?

Example:

I have table users with 2 fields: username and password. User in his profile can change any of them or both. Profile is written in ExtJS 4 with proxy configured with option writeAllFields: false, so client sends to server only modified fields.

On server side, if, for example, username not found via

  • $this->_request->getPost('username')
  • $this->_getParam('username')

... or something else (it's null), I do not want to validate it and modify it in database.

BUT if user typed an empty string in username field, I want to validate it and show an error to user with text like "username cannot be empty".

How can this be accomplished?

Guy Fawkes
  • 2,313
  • 2
  • 22
  • 39

1 Answers1

0

Personally I would filter the input and then validate it. But to achieve your objectives, this would probably do the trick, its straight from the docs.

$valid = new Zend_Validate_NotEmpty();
$value  = '';
$result = $valid->isValid($value);
// returns false

What drew010 mentions applies if you are using Zend Form to create the form. In that case setting the setRequired() validator to true on the element itself will display an error to the user if no input is supplied. If your not using Zend Form, then the above should point you in the right direction.

Asciant
  • 2,130
  • 1
  • 15
  • 26