Results 1 to 9 of 9

Thread: Adding a custom rgxp to an extended field in Members Module?

  1. #1
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Adding a custom rgxp to an extended field in Members Module?

    This kind of relates to another post of mine, but i'll ask it as a seperate question.
    I've added a field to the tl_member table.
    Is it possible to add a custom rgxp to this field?
    (I've tried various ways but i keep getting "cannot redeclare class tl_member", or the "Could not load class" from System/functions.php line 67)

  2. #2
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Adding a custom rgxp to an extended field in Members Module?

    Consulenza Contao CMS https://www.intco.it

  3. #3
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: Adding a custom rgxp to an extended field in Members Module?

    Thanks ga.n
    I already use a custom rgxp in my module, but adding one to the tl_member class is not the same somehow.
    I keep getting the errors I mentioned.
    I figure I need to extend the tl_member class somehow, but not sure how or where.

  4. #4
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Adding a custom rgxp to an extended field in Members Module?

    you do not to extend the tl_member class. Why do you think you need to extend it?

    please post the dca related entry for the field

    e.g.

    'mycustomfield' => array(
    ....
    ....
    )
    Consulenza Contao CMS https://www.intco.it

  5. #5
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: Adding a custom rgxp to an extended field in Members Module?

    Why do you think you need to extend it?
    Stupidity??? :D

    from my module in tl_member.php

    Code:
    // Modify the Members palette 
    $GLOBALS['TL_DCA']['tl_member']['palettes']['default'] = str_replace('company','schoolnzqaid,company',$GLOBALS['TL_DCA']['tl_member']['palettes']['default']);
    
    // Add the schoolnzqaid field to the BE  Members module
    $GLOBALS['TL_DCA']['tl_member']['fields']['schoolnzqaid'] = array
    (
    	'label'     => &$GLOBALS['TL_LANG']['tl_member']['schoolnzqaid'],
    	'inputType' => 'text',
    	'sorting'   => true,
    	'eval'      => array('mandatory'=>true, 'unique'=>true, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'address', 'tl_class'=>'w50', 'rgxp'=>'digit', 'maxlength'=>7)
    );

    Essentially I don't want the rgxp to be 'digit', but want to make a custom one that checks the input against another field in another table. (nzqaid in tl_school_schools)

  6. #6
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Adding a custom rgxp to an extended field in Members Module?

    as stated here: http://www.typolight.org/hooks.html#addCustomRegexp

    let me say you want to call you custom rgxp 'mydigit'

    put the following code inside the tl_module.php of your module:

    Code:
    class myDigitRgxp extends Backend {
    
        public function regexpHook($strRegexp, $varValue, Widget $objWidget) {
          
            if ($strRegExp != 'mydigit') {
                  /* this method will be called for *every* rgxp that can't be recognized by TL core so we need to check the rgxp name          */
                  return;
            }   
            
            /** $varValue contains the value of the field to be validated */
    
             // ....  your business logic .... 
    
            if ( ... something goes wrong ... ) {
                   /**
                        you can use the objWidget->addError method to notify TL that something goes wrong
                        @see http://api.typolight.org/Controller/Widget.html
                        maybe you want to change the error message :D
                   */
                   $objWidget->addError('hey, what are you doing?');
    
    
        }
    
    }

    then put in your config/config.php file the following entry
    Code:
    // don't miss the []
    $GLOBALS['TL_HOOKS']['addCustomRegexp'][]  = array('myDigitRgxp', 'regexpHook');
    Once done, you can use the mydigit regxp like the built-in ones
    Consulenza Contao CMS https://www.intco.it

  7. #7
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: Adding a custom rgxp to an extended field in Members Module?

    Thanks ga.n,
    I was trying to add the class to tl_member.php before, not tl_module.php
    I'm sure I'm closer.... but....same error...

    Fatal error: Could not load class tl_school_schools in /hsphere/local/home/xxxxx/xxxxxxxx.com/system/functions.php on line 67
    In my config I also have another custom rgxp, could the fact that there is 2 cause a conflict?

    from config.php
    Code:
    //HOOK TO Validate urls ...fees, website, approved_video_link
    $GLOBALS['TL_HOOKS']['addCustomRegexp'][] = array('tl_school_schools', 'addCustomRegexp');
    
    //for the tl_member custom rgxp on Registration to check schoolnzqaid against tl_school_schools nzqaid
    $GLOBALS['TL_HOOKS']['addCustomRegexp'][]  = array('nzqaidRgxp', 'checkNzqaid');
    and my rgxp added to tl_module.php
    Code:
    class nzqaidRgxp extends Backend 
    {
    
    	public function checkNzqaid($strRegexp, $varValue, Widget $objWidget)
    	{
            if ($strRegexp == 'nzqaid')		 
            {
    		//check $varValue against database
    		$objnzqaid = $this->Database->prepare("SELECT nzqaid"
    		                                  . " FROM tl_school_schools"
    		                                  . " WHERE nzqaid =? ")
    			     				   ->execute($varValue);
    		
    			if ($objnzqaid->numRows() < 1)
    			{
    			$objWidget->addError($objWidget->label . ' ...crap... ');
    			}
    
            return true;
    		}
    
        return false;
        }
    
    }
    and the new eval in tl_members.php
    Code:
    	'eval'      => array('mandatory'=>true, 'unique'=>true, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'address', 'tl_class'=>'w50', 'rgxp'=>'nzqaid', 'maxlength'=>7)

  8. #8
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Adding a custom rgxp to an extended field in Members Module?

    if you have two customRegExp you can put them in the same class and put the class in the "root" of your module

    e.g.

    Code:
    // in config.php
    
    //HOOK TO Validate urls ...fees, website, approved_video_link
    $GLOBALS['TL_HOOKS']['addCustomRegexp'][] = array('nzqaidRgxp', 'addCustomRegexp');
    
    //for the tl_member custom rgxp on Registration to check schoolnzqaid against tl_school_schools nzqaid
    $GLOBALS['TL_HOOKS']['addCustomRegexp'][]  = array('nzqaidRgxp', 'checkNzqaid');
    create a file named nzqaidRgxp.php inside the "root" directory of your module:

    e.g

    system/modules/your_module/nzqaidRgxp.php

    and put there the two methods, the above will benefit of the TL autoload

    or you can use the *same* hook for the two rgxp and, inside the method and test against the $strRegexp to apply the correct code through an if statement
    Consulenza Contao CMS https://www.intco.it

  9. #9
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: Adding a custom rgxp to an extended field in Members Module?

    Thank you (again) ga.n
    Mission accomplished.
    Both rgxps work, and signup is prevented if the input doesn't match.

    I used the one class, one function method you suggested,
    I've changed the names.
    So config needs one line:
    Code:
    $GLOBALS['TL_HOOKS']['addCustomRegexp'][] = array('CustomRgxp', 'checkInput');
    and my new CustomRgxp.php in the root contains

    Code:
    class CustomRgxp extends Backend
    {  
    
    //to check School signup tl_member schoolnzqaid against tl_school_schools nzqaid
    	public function checkInput($strRegexp, $varValue, Widget $objWidget)
    	{
            if ($strRegexp == 'nzqaid')		 
            {
    		//check $varValue against database
    		$objnzqaid = $this->Database->prepare("SELECT nzqaid"
    		                                  . " FROM tl_school_schools"
    		                                  . " WHERE nzqaid =? ")
    			     				   ->execute($varValue);
    					   
    		
    			if ($objnzqaid->numRows < 1)
    			{
    			$objWidget->addError($objWidget->label . ' ...f. off... ');
    			}
    
            return true;
    		}
    		
            if ($strRegexp == 'website')		 
            {
    
    			if (!filter_var($varValue, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED))
    			{
    			$objWidget->addError($objWidget->label . ' should be a FULL URL (eg: http://myschool.com/');
    			}
    
            return true;
    		}
    
        return false;
        }
    
    }
    Ciao, murray.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •