Monday, July 22, 2013

Overriding the ZF1 Zend_Form Validator's setRequired message

The Zend_Form_Element's isRequired() method uses the Zend_Validate_NotEmpty validator under the hood and thus uses its message template for outputs.

For simple Zend_Form_Element_Text override you can just do this:

$e = new Zend_Form_Element_Text('text_element');
$e->setRequired(true) // needs to be true
  ->addValidators(array('NotEmpty')); // needs to be explicitly set
$e->getValidator('NotEmpty')
  ->setMessage(
      'This is my new message that you entered an empty string!',
      Zend_Validate_NotEmpty::IS_EMPTY
  );

For Multi elements though, you need to set the NotEmpty's breakChainOnFailure to true as below:

$e = new Zend_Form_Element_MultiCheckbox('text_element');
$e->setRequired(true); // needs to be true
  ->addValidators(array(
      array('NotEmpty', true) // notice "true" is set for breakChain option
  ));

$e->getValidator('NotEmpty')
  ->setMessage(
      'This is my new message that you entered an empty string!',
      Zend_Validate_NotEmpty::IS_EMPTY
  );

Friday, July 12, 2013

Awesome Example of ZF1 Zend_Form Rendering With Groupings and Fieldsets

http://zendgeek.blogspot.com/2009/07/zend-form-display-groups-decorators.html

Using An Existing Method on a Mocked Object

Problem: You want to use an existing method in a class for unit testing in a mocked object but cannot use it because the method is returning an error when called.

Example:
class A
{
    public function methodA()
    {
        return "Hello";
    }
}

Unit test:
$mock = $this->getMockBuilder('A')
             ->getMock();
$mock->methodA(); // returns an error

Solution: 
$mock = $this->getMockBuilder('A')
             ->setMethod(null) // the trick is explicitly using a null
             ->getMock();
$mock->methodA(); // returns "Hello"

For the curious, what will happen if you pass an empty array:
$mock = $this->getMockBuilder('A')
             ->setMethod(array())
             ->getMock();
$mock->methodA(); // returns null

You shouldnt really do this at all because you can just instantiate class A after but its just a good thing to know!

Friday, July 5, 2013

JQuery UI Model with submit form on confirm

I was having frustrating problem using jquery's submit button inside a Jquery ui modal box when a user submit. the problem occurs when you explicitly set the submit() to return false that you cannot do anything after it even explicitly saying submit(function (){return true;})

http://www.jensbits.com/2009/08/10/modal-confirmation-dialog-on-form-submit-javascript-jquery-ui-and-thickbox-varieties/

solution is to NOT use the submit function at all after confirm:

    $( "#dialog-confirm" ).dialog({
        resizable: false,
        autoOpen: false,
        height: 140,
        modal: true,
        buttons: {
            "Delete pendings": function() {
                document.scheduled_release_form.submit();
            },
            Cancel: function() {
                $( this ).dialog("close");
            }
        }
    });

<form action="" method="post" name="scheduled_release_form" id="scheduled_release_form">
<input type="button" name="submit_update" value="submit update" id="submit_update"><input type="button" name="submit_delete" value="delete pendings" id="submit_delete">
</form>

Mac vagrant port forwarding

Just reposting this blog for reference.

http://www.dmuth.org/node/1404/web-development-port-80-and-443-vagrant

If you have a vagrant instance that is running on 8080 for web services and you want to forward 80 traffic to it so you will not be appending ports to your url like http://localhost:8080 but instead just use http://localhost do the following bellow:

sudo ipfw add 100 fwd 127.0.0.1,8080 tcp from any to me 80
sudo ipfw add 101 fwd 127.0.0.1,8443 tcp from any to me 443

Monday, April 15, 2013

NOTE: SCP syntax

scp -rp ./copy-me/* franz@192.168.1.1:/path/to/location/copy/