Extend RequiredFields class and call it in another class in SilverStripe

140 views Asked by At

I have two classes: MyAccountPage and PersonalData_Validator1

class MyAccountPage extends Page {

    public function canCreate($member = null) {
        return false;
    }
}

class MyAccountPage_Controller extends Page_Controller {

    private static $allowed_actions = array(
        'editprofile'
    );

    public function EditProfileForm(){

        $firstName = new TextField('FirstName', 'First Name *');
        $firstName->addExtraClass('requiredField form-control');

        $surName = new TextField('Surname', 'Last Name *');
        $surName->addExtraClass('requiredField form-control');

...

    $validator = new PersonalData_Validator1(array(
        $firstName,
        $surName
    ));

}

When I write PersonalData_Validator1 class in the same page it works, but when I write PersonalData_Validator1 in another page it doesn't work. Why doesn't this work?

1

There are 1 answers

8
user3358839 On

this is my PersonalData_Validator1 class

class PersonalData_Validator1 extends RequiredFields {

public function php($data){
    $bRet = parent::php($data);

    $err = false;

 $invalid_char = array("'","\"","*","%", "=", "`","<",">","@","^","$","     (",")");

    $pcrePattern = '[A-Za-z0-9]';
    $firstName=$data['FirstName'];
    $lastName=$data['Surname'];
    $nic=$data['NIC'];

    if($bRet){
        $pregSafePattern = str_replace('/', '\\/', $pcrePattern);

        if((!preg_match('/' . $pcrePattern . '/i', $firstName)) ){
            $this->validationError('FirstName', 'First Name contains invalid characters', 'validation');
            $err = true;
            //$bRet = true;
        }



       else if(( !preg_match('/' . $pcrePattern . '/i', $lastName))){
            $this->validationError('Surname', 'Last name contains invalid characters', 'validation');
            $err = true;
        }



         else if(($nic && !preg_match('/' . $pcrePattern . '/i', $nic))){
            $this->validationError('NIC', 'NIC field contains invalid characters', 'validation');
             $err = true;
        }


    }

    if($err == 'true'){
        $bRet = true;
    }else{
        //return $bRet;
    }
    return $bRet;
  }

 }