2013-07-31 2 views
2

젠드 Framework2 형태로 오류 메시지 여기

$form = $this->form; 
$form->setAttribute('action', $this->url('users', array('action' => 'login'))); 
$form->prepare(); 

echo $this->form()->openTag($form); 
echo $this->formRow($form->get('uname')); 
echo $this->formRow($form->get('pword')); 
echo $this->formElementerrors($form->get('pword')); 

echo $this->formSubmit($form->get('submit')); 

echo $this->form()->closeTag(); 

양식 문제없이하지만 함께 잘 작동 로그인 폼 아래

class LoginForm extends Form 
    { 
      public function __construct($name = null) 
      { 
       parent::__construct('login'); 
       $this->setAttribute('method', 'post'); 
       $this->add(array('name'=>'uname','attributes'=>array('type'=>'text',), 
        'options'=>array('label'=>'UserName'), 
       )); 

       $this->add(array('name'=>'pword','attributes'=>array('type'=>'password',), 
        'options'=>array('label'=>'Password'), 
       )); 

       $this->add(array('name'=>'submit','attribute'=>array('type'=>'submit', 
        'value' => 'Go', 
       'class'=>'submit','id'=>'submitbutton',) 
       )); 
      } 
    } 

를 만들려면 아래 내 코드 내 로그인 페이지에 대한 코드입니다 :

  1. 표시 소스가 보이는 경우 submit 값을 가져 오지 않습니다. value=''
  2. 오류 메시지를 표시하고 싶습니다. 어디에서 그런 식으로 살아야할까요? 시도해 보았습니다. echo $this->formElementerrors($form->getMessages('uname'));이 문제를 해결하기위한 제안이나 아이디어가 없었습니까? 다음은

$form = new LoginForm(); 
$request = $this->getRequest(); 

if ($request->isPost()) { 
    $post = $request->getPost(); 
    if($post->get('uname')=='') 
    { 
     $umessage='please enter username'; 
     return $this->redirect()->toRoute('users',array('action'=>'login')); 
    } 

    $this->db =$this->getServiceLocator()->get('db'); 
    $authAdapter = new AuthAdapter($this->db); 
    $authAdapter->setTableName('users') 
     ->setIdentityColumn('uname') 
     ->setCredentialColumn('pword'); 

    $authAdapter->setIdentity($post->get('uname')) 
     ->setCredential(md5($post->get('pword'))); 

    $authService = new AuthenticationService(); 
    $authService->setAdapter($authAdapter); 

    $result = $authService->authenticate(); 

    if ($result->isValid()) { 
     return $this->redirect()->toRoute('users'); 
    } else { 
     switch ($result->getCode()) { 
      case Result::FAILURE_IDENTITY_NOT_FOUND: 
       echo 'user name not valid dude'; 
       break; 

      case Result::FAILURE_CREDENTIAL_INVALID: 
       echo 'password incorrect'; 
       break; 

      case Result::SUCCESS: 
       echo 'login successfull'; 
       break; 
     } 

    } 
} 
$this->layout('layout/index'); 
return array('form' => $form); 

답변

3

당신의 구문은 개별 오류 메시지를 표시하는 잘못 ... 내 컨트롤러 코드,이 시도 :

변화 :

$this->formElementerrors($form->getMessages('uname')); // incorrect 
$this->formElementErrors($form->get('uname')); // correct 

예 :

<?php foreach($form->getElements() as $element): ?> 
    <?php if($element->getLabel()): ?> 
     <?php echo $this->formLabel($element) ?> 
    <?php endif ?> 
    <?php echo $this->formElementErrors($element, array('class' => 'inline')); ?> 
    <?php echo $this->formElement($element) ?> 
<?php endforeach ?> 

알림 getMessages()가 아닌 실제 요소를 전달하고 있습니다.

당신은 또한 당신의 버튼 정의의 유형이 (끝에 s 누락) :

attribute => attributes 

은 아래 참조 : 난 당신이 또한 설치가 가정입니다

$this->add(array(
    'name'=>'submit', 
    'attributes' => array(
     'type'=>'submit', 
     'value' => 'Go', 
     'class'=>'submit','id'=>'submitbutton', 
    ) 
)); 

귀하의 의견이 너무 필터?

$inputFilter->add($factory->createInput(array(
    'name'  => 'username', 
    'required' => true, 
    'filters' => array(
     array('name' => 'StripTags'), 
     array('name' => 'StringTrim'), 
    ), 
    'validators' => array(
     array(
      'name' => 'StringLength', 
      'options' => array(
       'encoding' => 'UTF-8', 
       'min'  => 1, 
       'max'  => 100, 
      ), 
     ), 
    ), 
))); 
+0

안녕하세요 앤드류 내 두 번째 문제는 해결되었지만 첫째 문제는 여전히 같은 ,, ... 난 이미'$ this-이> formElementerrors 시도 ($ 형상 -를>) ('UNAME'얻을); ' 그것은 오류 메시지를 표시하지 않습니다 .. : (심지어 일부 변수와 concated 시도했지만 변수 메시지가 표시되지 않습니다. – Friend

+0

올바른 camelCase, formElementerrors => formElementErrors를 사용하여 시도 해 봤나? ZF2가 대문자를 사용하여 해결하는지 확실하지 않은가요? 보기 헬퍼에, 그것은 경우가있을 수 있습니다. 거기에 어떤 문제가있는 경우 formRow 또한 오류가 표시되어야합니다 같아요. 유효성 검사/필터 설치가 올바르게 있습니까? 게시 할 수 있습니까? – Andrew

+0

난 입력 된 필터를 설치하지 않은 것 같아 .. 내가 확인하고 업데이 트하자 ...하지만 그 자체가 바로 처리해야 인증 플러그인을 사용하여? 제발 대체 된 질문에 의해 도대체 – Friend