扩展RequiredFields类并在SilverStripe中的另一个类中调用它

时间:2015-06-09 05:54:00

标签: validation silverstripe

我有两个课程:MyAccountPagePersonalData_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
    ));

}

当我在同一页面中编写PersonalData_Validator1课程时,它会起作用,但当我在另一页面中编写PersonalData_Validator1时,它无法正常工作。为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

这是我的PersonalData_Validator1类

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;
  }

 }