B
B
Blud732014-03-04 17:45:25
Laravel
Blud73, 2014-03-04 17:45:25

What causes a validation error in Laravel?

I'm fighting for the dead hour. Tell.
There is a form validation function:

public function registration()
  {
      if($_POST['type'] === 'company'){
        $validator = Validator::make(
          array('email' => 'email|unique:users'),
          array('name' => 'required|min:3|max:20'),
          array('lastname' => 'required|min:3|max:20')
          );

      }elseif($_POST['type'] === 'user'){
        $validator = Validator::make(
          array('email' => array('email','unique:users')),
          array('name' => array('required')),
          array('lastname' => array('required', 'min:3', 'max:20'))
        );

      }

      if ($validator->fails()) {
          $messages = $validator->messages();
          print_r($messages);
          print_r($_POST);
          return View::make('user.registration_error', array('type' => $_POST['type'], 'data' => $_POST, 'error' => $messages));
        }
         echo 'lol';

  }

There is a form:
<form action="/registration" method="post" class="form-horizontal">
    <fieldset>
      <legend>Регистрация для компаний</legend>
      <div class="control-group">

        <label class="control-label" for="input01">Имя</label>
        <div class="controls">
          <input name="name" type="text" class="input-xlarge" id="input01">
        </div><br>

        <label class="control-label" for="input01">Фамилия</label>
        <div class="controls">
          <input name="lastname" type="text" class="input-xlarge" id="input01">
        </div><br>

        <label class="control-label" for="input01">E-mail</label>
        <div class="controls">
          <input name="email" type="email" class="input-xlarge" id="input01">
          <p class="help-block">На него прийдет код подтверждения.</p>
        </div>

        <label class="control-label" for="input01">Компания</label>
        <div class="controls">
          <input name="company-name" type="text" class="input-xlarge" id="input01">
          <p class="help-block">Название компании которую вы представляете</p>
        </div>
          <input name="type" type="hidden" value="company">
        <label class="control-label" for="input01">Пароль</label>
        <div class="controls">
        <input name="pass" type="password" class="input-xlarge" id="input01">
        </div> <br>

        <div class="controls">
          <input name="pass_confirmation" type="password" class="input-xlarge" id="input01">
          <p class="help-block">Еще раз</p>
        </div>

        <button class="btn btn-primary" type="submit">Зарегестрироваться</button>
      </div>
     </fieldset>
  </form>

The problem is that the Name field is always in error, it does not check further. Leave an empty field or filled in, it is always with an error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Grebenikov, 2014-03-12
@Blud73

Instead $_POST['type']use Input::get('type')
Not correctly use Validator.
Example of correct usage:

$validator = Validator::make(Input::all(), array(
    'email' => 'email|unique:users',
    'name' => 'required|min:3|max:20',
    'lastname' => 'required|min:3|max:20'
));

You can find out more here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question