Zend的形式addFilter StripTags不剥标签(Zend Form addFilter

2019-07-30 03:44发布

我需要一点点帮助清理东西了Zend_Form和添加过滤器的元素。 现在,我的印象是,当您添加过滤器的形式,当形式是滤波器作为控制器调度的一部分执行公布下。

但是我测试的形式我的恐惧,当过滤器StripTags似乎并没有被运行,我正在与数据的HTML标签中的数据。

我的表单元素看起来是这样的。

$address1 = new Zend_Form_Element_Textarea('address1');
    $address1->addFilter('StripTags')
        ->addFilter('StringTrim')            
        ->setAttrib('cols', 30)
        ->setAttrib('rows', 5)
        ->removeDecorator('DtDdWrapper')
        ->removeDecorator('label')
        ->removeDecorator('HtmlTag')

但是,如果我把文本区域中的一些数据,在它的HTML标签,然后检查的形式是有效利用

$formData = $this->_request->getPost();
if($form->isValid($formData){
    ...

数据回来,在它的标签。 它只有当我通过用strip_tags传递数据()功能除去。

我想我的问题是应该的StipTags过滤如果是这样,为什么不呢? 缺少什么我在这里。

Answer 1:

你没有你如何调用的isValid之后访问数据发布的代码。 IIRC如果您通过访问数据的过滤器才会生效$form->getValue('someElement')或类似的规定。



Answer 2:

抱歉,我知道我迟到了,但在任何情况下,面临一个同样的问题,

今天我面临这个问题,我发现一些方法来解决这个问题:

首先我的代码是:

  • 这是窗体

    类Application_Form_UserForm延伸Zend_Form的{

    公共功能的init(){

     /* Form Elements & Other Definitions Here ... */ $this->setMethod('POST'); $fname = new Zend_Form_Element_Text('fname'); $fname->setLabel('First Name: '); $fname->setAttribs(Array( 'placeholder'=>'Example: Eslam', 'class'=>'form-control' )); $fname->setRequired(); $fname->addValidator('StringLength', false, Array(4,20)); $fname->addFilter('StringTrim'); $fname->addFilter('StripTags'); $fname->removeDecorator('DtDdWrapper'); $fname->removeDecorator('label'); $fname->removeDecorator('HtmlTag'); $lname = new Zend_Form_Element_Text('lname'); $lname->setLabel('Last Name: '); $lname->setAttribs(Array( 'placeholder'=>'Example: Khoga', 'class'=>'form-control' )); $lname->setRequired(); $lname->addValidator('StringLength', false, Array(4,20)); $lname->addFilter('StringTrim'); $lname->addFilter('StripTags'); $lname->removeDecorator('DtDdWrapper'); $lname->removeDecorator('label'); $lname->removeDecorator('HtmlTag'); $email = new Zend_Form_Element_Text('email'); $email->setLabel('Email: '); $email->setAttribs(Array( 'placeholder'=>'Example@Example.com', 'class'=>'form-control' )); $email->setRequired(); $email->addValidator('StringLength', false, Array(5,250)); $email->addFilter('StringTrim'); $email->addFilter('StripTags'); $email->removeDecorator('DtDdWrapper'); $email->removeDecorator('label'); $email->removeDecorator('HtmlTag'); $gender = new Zend_Form_Element_Select('gender'); $gender->setRequired(); $gender->addMultiOption('male','Male')-> addMultiOption('female','Female')-> addMultiOption('none','Prefer not to mention'); $gender->setAttrib('class', 'form-control'); $track_obj = new Application_Model_Track(); $allTracks = $track_obj->listAll(); $track = new Zend_Form_element_Select('track'); foreach($allTracks as $key=>$value) { $track->addMultiOption($value['id'], $value['name']); } $submit= new Zend_Form_Element_Submit('submit'); $submit->setAttribs(array('class'=>'btn btn-success')); $reset= new Zend_Form_Element_Submit('reset'); $reset->setAttribs(array('class'=>'btn btn-danger')); $this->addElements(array( $fname, $lname, $email, $gender, $track, $submit, $reset )); 

    }

    }

  • 这是控制器

类UserController的延伸化Zend_Controller_Action {

public function init()
{
    /* Initialize action controller here */
}

public function indexAction()
{
    // action body
}

public function listAction()
{
    // action body
    $user_model = new Application_Model_User();
    $this->view->users = $user_model->listUsers();

    $track_form = new Application_Form_Track();
    $this->view->track_form = $track_form;
    $track_model = new Application_Model_Track();
    $request = $this->getRequest();
    if($request->isPost())
    {
        if($track_form->isValid($request->getPost())){
            $track_model-> addTrack($request->getParams());
            $this->redirect('/user/add');
        }
    }
}

public function detailsAction()
{
    // action body
    $user_model = new Application_Model_User();
    $us_id = $this->_request->getParam("uid");
    $user = $user_model->userDetails($us_id);
    $trackModel = new Application_Model_Track();
    $track = $trackModel->getTrackName($user[0]['track']);
    $user[0]['track'] = $track[0]['name'];
    $this->view->user = $user[0];
}

public function deleteAction()
{
    // action body
    $user_model = new Application_Model_User();
    $us_id = $this->_request->getParam("uid");
    $user_model->deleteUser($us_id);
    $this->redirect("/user/list");
}

public function addAction()
{
    // action body
    $form = new Application_Form_UserForm();
    $request = $this->getRequest();
    if($request->isPost()){
        if($form->isValid($request->getPost())){
            /*echo "<pre>";
            print_r($form);
            echo "</pre>";
            exit;*/
            $userData['fname'] = $form->getValue('fname');
            $userData['lname'] = $form->getValue('lname');
            $userData['email'] = $form->getValue('email');
            $userData['gender'] = $form->getValue('gender');
            $userData['track'] = $form->getValue('track');
            $user_model = new Application_Model_User();
            $user_model-> addNewUser($userData);
            $this->redirect('/user/list');
        }
    }
    $this->view->user_form = $form;
}

public function editAction()
{
    // action body
    $form = new Application_Form_UserForm();
    $user_model = new Application_Model_User ();
    $id = $this->_request->getParam('uid');
    $user_data = $user_model-> userDetails($id)[0];
    $form->populate($user_data);
    $this->view->userName = $user_data['fname']." ".$user_data['lname'];
    $this->view->user_form = $form;
    $request = $this->getRequest();
    if($request->isPost()){
        if($form->isValid($request->getPost())){
            $userData['fname'] = $form->getValue('fname');
            $userData['lname'] = $form->getValue('lname');
            $userData['email'] = $form->getValue('email');
            $userData['gender'] = $form->getValue('gender');
            $userData['track'] = $form->getValue('track');
            $user_model-> updateUser($id, $userData);
            $this->redirect('/user/list');
        }
    }
}


}

第一个解决方案:

我用表单类表单元素滤波器,但我在控制器检索从$形式的对象数据,作为我发现方法

addFilter()方法

在$ _POST数组值不改变,因此,我已检索从$形式对象中的数据,然后通过它作为阵列的模型。

第二个解决方案:

我已经尝试通过创建从过滤器类对象来应用过滤器上的值在控制器,而不是在形式和应用所需的滤波器

方案三:

是使用方法

在addValidator()

正则表达式的影响在$ _ POST值。



文章来源: Zend Form addFilter StripTags not stripping tags