Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternate loading algorithm to load personalized base classess #566

Open
jmjimenez opened this issue Feb 12, 2016 · 5 comments
Open

Alternate loading algorithm to load personalized base classess #566

jmjimenez opened this issue Feb 12, 2016 · 5 comments

Comments

@jmjimenez
Copy link

I don't know if it is a good idea. Let me explain my problem:
I would like to personalize the validators classes a bit. My main problem is that I have to modify TBaseValidator. As this is the base class for all Validators webcontrols, it will forced me modify all of them.
Instead of doing this, I would like the following:

  • Create my own TBaseValidator copy and modify it
  • When Validators webcontrol look for TBaseValidator --> I would like them to find my copy (instead of the "official" one in System.Web.UI.Webcontrols
    To achieve this, I think the appropriate point would be to modify PradoBase::using method. There should be a way to inform this method that when asked to load System.Web.UI.Webcontrols.TBaseValidator --> to load my copy at Application.Common.TBaseValidator
@ctrlaltca
Copy link
Member

Can you be more precise on what you're trying to achieve? in the past we added some facilities like that on other classes, from simple method overloading to full-blown adapter classes.

@jmjimenez
Copy link
Author

  • First, I would like my Validators to be labels. I can see that TBaseValidator extends from TLabel. But the problem is that you cannot set ForControl because it's forbidden. So, it always renders as a span.
  • Second, I can see in the TBaseValidator's contructor that it sets the ForeColor to red. I would like to set the color using css, but the 'red' color rendered in the style attribute has higher priority (this is the reason I would like to remove this 'red' color from TBaseValidator)

These are minor modifications and I can sort them out. But I wonder if there is a more elegant solution...
Thanks for your time and your effort

@javalizard
Copy link

By calling this:
$this->setViewState('ForControl',$value,'');
it is possible to short circuit the View State for the effects of setForControl without calling setForControl.

Furthermore, this is a use case for behaviors.

TBaseValidator.php changes:

protected function getTagName()
{
    if(($tag = $this->dyGetTagName()) !== null) return $tag;
    return ($this->getForControl()==='')?'span':'label';
    //or
    //return $this->dyDefaultTagName(($this->getForControl()==='')?'span':'label');
}
public function setForControl($value)
{
    if(($result = $this->dySetForControl($value)) !== $value)
    {
        if($result === true)
            parent::setForControl($value);
        return $result;
    }
    throw new TNotSupportedException('basevalidator_forcontrol_unsupported',get_class($this));
}

and then your new class:

class MyBaseValidatorBehavior extends TClassBehavior {
    public function dySetForControl($hostobject, $value, $callchain)
    {
        return $callchain-> dySetForControl (true);
    }
    public function dyGetTagName($hostobject, $value, $callchain)
    {
        return $callchain-> dyGetTagName('label');
    }
}

and somewhere in the code -before dealing with the templates- this would be called:

TBaseValidator:: attachClassBehavior('MyBaseValidator', 'MyBaseValidatorBehavior');

Then all your TBaseValidators would contain the necessary behavior as they are instanced.

@javalizard
Copy link

Another way:

class MyBaseValidatorBehavior extends TClassBehavior {
    public function attach($owner)
    {
       $owner->setViewState('ForControl', 1);
    }
}

along with
TBaseValidator:: attachClassBehavior('MyBaseValidator', 'MyBaseValidatorBehavior');

@javalizard
Copy link

Another change to TBaseValidator:

    public function __construct()
    {
        parent::__construct();
        $this->setForeColor($this->dyDefaultForeColor('red'));
    }

This would allow behaviors to modify the default ForeColor. The Class behaviors are added in the __construct method; then the dyDefaultForeColor overrides the input:

class MyBaseValidatorBehavior extends TClassBehavior {
    public function dyDefaultForeColor($hostobject, $value, $callchain)
    {
        return $callchain-> dyDefaultForeColor ('myColor'); // and/or ''
    }
}

There may be more necessary to get the Validators on CSS rather than a ForeColor. More looking into the issue would be needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants