简体   繁体   中英

check if value is null laravel 5

I'm trying to check if a set of fields, has at least one empty field. If there is empty field and alert should pop, if not it should do something else.

Code

public function updatePerfil() {
    $file = Input::file('imagem');
    $email = Input::get('email');
    $profileData = Input::except('_token');
    $validation = Validator::make($profileData, User::$profileData);
    if ($validation->passes()) {
        $update = Input::only('name','imagem','data_nascimiento');
        if ($file == null || $email == null) {
            User::where('id', Input::get('id'))->update($profileData);
            Session::flash('message', 'Perfil editado com sucesso');
            return view('backend/perfil.index'); 
        }
        $file = array_get($profileData,'imagem');
        $destinationPath = 'imagens/perfil/';
        $extension = $file->getClientOriginalExtension();
        $filename = Auth::user()->id . '.' . $extension;
        $profileData['imagem'] = $filename;
        Image::make($file)->resize(400, 400)->save($destinationPath.$filename);
        User::where('id', Input::get('id'))->update($profileData);
        Session::flash('message', 'Perfil editado com sucesso');
        return Redirect::to('backend/perfil');
    } else {
        return Redirect::to('backend/perfil')->withInput()->withErrors($validation);
    }
}

public static $profileData = array(
        'email' =>  'email|unique:users',
        'name' =>  'required|min:5|max:25',
        'data_nascimento' => 'date',
        'imagem' => 'image|max:1000|mimes:jpeg,jpg,png'
        );

Form

{!! Form::open(array('class' => 'form-horizontal', 'url' => 'backend/perfil', 'name' => 'updatePerfil', 'role' => 'form', 'files'=> true))!!}

    <input type="hidden" name="id" value="{{Auth::user()->id}}">

    <div class="row">
        <div class="col-md-3 col-lg-3"></div>
        <div class="col-md-7 col-lg-7">
            @if (count($errors) > 0)
                <div class="alert alert-danger" style="margin-top: 0px;">
                    <strong>Ups!</strong> Existe algum problema com o formulário.<br><br>
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif
        </div>
    </div>
    <div class="row">
        <div class="col-md-3 col-lg-3"></div>
        <div class="col-md-7 col-lg-7">
            @if (Session::has('message'))
                <div class="alert alert-success" style="margin-top: 0px;">
                    {{ Session::get('message') }}
                </div>
            @endif
        </div>
    </div>        
    <div class="row" style="margin-bottom: 20px;">
        <div class="col-md-3 col-lg-3"></div>
        <div class="col-md-2 col-lg-2">
            {!! Form::label('name', 'Utilizador', ['class' => 'label_perfil']) !!}
        </div>
        <div class="col-md-5 col-lg-5">
            {!! Form::text('name', Auth::user()->name, ['class' => 'form-control input-md' , 'placeholder' => 'Utilizador']) !!}
        </div>
    </div>
    <div class="row" style="margin-bottom: 20px;">
        <div class="col-md-3 col-lg-3"></div>
        <div class="col-md-2 col-lg-2">
            {!! Form::label('data_nascimento', 'Data de nascimento', ['class' => 'label_perfil']) !!}
        </div>
        <div class="col-md-5 col-lg-5">
            {!! Form::date('data_nascimento', Auth::user()->data_nascimento, ['class' => 'form-control input-md']) !!}
        </div>
    </div>
    <div class="row" style="margin-bottom: 20px;">
        <div class="col-md-3 col-lg-3"></div>
        <div class="col-md-2 col-lg-2">
            {!! Form::label('genero', 'Sexo', ['class' => 'label_perfil']) !!}
        </div>
        <div class="col-md-5 col-lg-5">
            {!! Form::select('genero', ['Masculino' => 'Masculino', 'Feminino' => 'Feminino'], Auth::user()->genero, ['class' => 'form-control input-md']) !!}
        </div>
    </div>
    <div class="row" style="margin-bottom: 20px;">
        <div class="col-md-3 col-lg-3"></div>
        <div class="col-md-2 col-lg-2">
            {!! Form::label('email', 'Email', ['class' => 'label_perfil']) !!}
        </div>
        <div class="col-md-5 col-lg-5">
            {!! Form::text('email', Auth::user()->email, ['class' => 'form-control input-md', 'placeholder' => 'Email']) !!}
        </div>
    </div>
    <div class="row" style="margin-bottom: 20px;">
        <div class="col-md-3 col-lg-3"></div>
        <div class="col-md-2 col-lg-2">
            {!! Form::label('imagem', 'Imagem', ['class' => 'label_perfil']) !!}
        </div>
        <div class="col-md-5 col-lg-5">
            {!! Form::file('imagem', ['class' => 'input-file']) !!}
        </div>
    </div>   
    <div class="row" style="margin-bottom: 20px; margin-top: 30px;">
        <div class="col-md-3 col-lg-3"></div>
        <div class="col-md-9 col-lg-9">
            {!! Form::submit('Alterar perfil', ['class' => 'btn btn-primary']) !!}
        </div>
    </div> 
{!! Form::close() !!}

When I try to update the email field without entering anything he actualizame in the database as empty field and I want to continue with what the user does. With the image works right. I do not know if you are fetching the get. Someone knows how to solve this problem?

You can use Validation of Laravel with a bit of laravel magic to check if email is not null, try this:

public function updatePerfil() {
    $profileData = Input::except('_token');
    $validation = Validator::make($profileData, User::$profileData);
    if ($validation->passes()) {
        $update = Input::only('name','imagem','data_nascimiento');
        if(Input::has('email')){
            $update['email'] = Input::get('email');
        }
        User::find(Input::get('id'))->update($update);
        Session::flash('message', 'Perfil editado com sucesso');
        return view('backend/perfil.index'); 
    } else {
        return Redirect::to('backend/perfil')->withInput()->withErrors($validation);
    }
}   

public static $profileData = [
    'email' =>  'sometimes|required|email|unique:users',
    'name' =>  'required|min:5|max:25',
    'data_nascimento' => 'date',
    'imagem' => 'image|max:1000|mimes:jpeg,jpg,png',
    'id'=>'required|integer'
];

Don't forget, $$ is variable of variable in php...

UPDATE Use this code instead:

public function updatePerfil(Request $request) {
    $rules = [
        'email' =>  'sometimes|required|email|unique:users',
        'name' =>  'required|min:5|max:25',
        'data_nascimento' => 'date',
        'imagem' => 'image|max:1000|mimes:jpeg,jpg,png',
        'id'=>'required|integer'
    ];
    $validation = Validator::make($request->except('_token'), $rules);
    if ($validation->passes()) {
        $update = $request->only('name','data_nascimiento');
        if($request->has('email')){
            $update['email'] = $request->input('email');
        }
        if($request->hasFile('imagem')){
            $file = $request->file('imagem');
            $destinationPath = 'imagens/perfil/';
            $extension =  $request->file('imagem')->getClientOriginalExtension();
            $fileName = Auth::user()->id . '.' . $extension;
            $request->file('imagem')->move($destinationPath, $fileName);
            $update['imagem'] = $fileName;
        }
        User::find($request->input('id'))->update($update);
        Session::flash('message', 'Perfil editado com sucesso');
        return view('backend/perfil.index'); 
    } else {
        return Redirect::to('backend/perfil')->withInput()->withErrors($validation);
    }
}

There is $$email on third line and in the if condition its $email. Please check if its a typo. Apart from this, isset() in php should help with checking with NULL values or to check if a value is set or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM