如何在 codeigniter 中显示特定字段的表单验证消息

How to show form validation message for particular field in codeigniter

这里我想显示不同字段的验证消息

$this->form_validation->set_rules('email','Email','trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');

if ($this->form_validation->run('email') == FALSE)
        {
            print_r('Enter email');
        }


if ($this->form_validation->run('password') == FALSE)
        {
            print_r('Enter password ');
        }

如果我提供密码并将电子邮件字段留空,则两封邮件都显示为静止状态api

谁能帮帮我

根据您的要求

if i give password and leave email field empty, both messages are showing in rest api

只需在您的输入中添加所需的属性。

<form action="https://whosebug.com/login" method="post" accept-charset="utf-8">


      <p>
          <input class="email_input" type="email" name="email" value="" placeholder="yourmail@gmail.com" required>
      </p>
      

      <p>
          <input class="password_input" type="password" name="password" value="" placeholder="Your password" required>
      </p>

      <p>
           <input class="enter-button" type="submit" name="btnSubmit" value="ENTER">
      </p>

      </form>

对于你的情况,我认为这会很好

$this->form_validation->set_rules('email','Email','trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->run();
if (form_error('email'))
{
    echo 'Enter email';
    exit;
}
if (form_error('password'))
{
    echo 'Enter password';
    exit;
}
if($this->form_validation->run()==FALSE){
             $errors = $this->form_validation->error_array();
             echo json_encode($errors);
        }

以上代码也可以正常工作,它还在 rest server codeigniter 中显示了正确的错误消息